Completed
Push — master ( 43d660...2f3132 )
by John
47s
created

Document.getDefaultSchema()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
1
require 'json'
2
3
class Symbol
4
  def split(separator)
5
    to_s.split(separator).map(&:to_sym)
6
  end
7
end
8
9
module JsonDoc
10
  class Document
11
    attr_accessor :bIsStrict
12
    attr_accessor :bUseKeyAsDesc
13
    attr_accessor :bUseDeepKeys
14
15
    def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true)
16
      @dSchema        = dSchema || self.getDefaultSchema()
17
      @bDefaultifyDoc = bDefaultifyDoc ? true : false
18
      @bIsStrict      = bIsStrict      ? true : false
19
      @bUseKeyAsDesc  = false
20
      @bUseDeepKeys   = true
21
      @dDocument      = self.getDefaultDocument()
22
      self.loadHash(dValues) if dValues.is_a?(Hash)
23
    end
24
25
    def getDefaultSchema()
26
      dSchema       =  {
27
        :type       => '',
28
        :properties => {
29
          :id       => { :default => '', :description => 'Doc Id', :type => 'string' }
30
        }
31
      }
32
      return dSchema
33
    end
34
35
    def getDefaultDocument()
36
      dDocument = {}
37
      if @bDefaultifyDoc && @dSchema.key?(:properties)
38
        @dSchema[:properties].keys.each do |yKey|
39
          dProperty = @dSchema[:properties][yKey]
40
          xxVal     = dProperty.key?(:default) ? dProperty[:default] : ''
41
          dDocument[yKey] = xxVal
42
        end
43
      end
44
      return dDocument
45
    end
46
47
    def loadHash(dValues=nil)
48
      if dValues.nil?
49
        return
50
      elsif ! dValues.is_a?(Hash)
51
        raise ArgumentError, 'E_INITIAL_VALUES_IS_NOT_A_HASH'
52
      end
53
      dValues.each do |yKey,xxVal|
54
        self.setProp(yKey,xxVal)
55
      end
56
      return self
57
    end
58
59
    def getProp(yKey=nil)
60
      if yKey.nil?
61
        raise ArgumentError, 'E_BAD_KEY__IS_NIL'
62
      end
63
      yKey  = yKey.to_sym if yKey.kind_of?(String)
64
65
      if @bUseDeepKeys
66
        aKeys = yKey.split('.')
67
        #aKeys = yKey.to_s.split('.').map(&:to_sym)
68
69
        dDoc  = @dDocument
70
        xxVal = getPropRecurse(aKeys.clone,dDoc)
71
        return xxVal
72
      end
73
      return @dDocument.key?(yKey) ? @dDocument[yKey] : nil
74
    end
75
76
    def getPropRecurse(aKeys=[],dDoc=nil)
77
      yKey = aKeys.shift
78
      if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.key?( yKey )
79
        return nil
80
      end
81
      xxVal = dDoc[ yKey ]
82
      if aKeys.length == 0
83
        return xxVal
84
      elsif dDoc.is_a?(Hash)
85
        return getPropRecurse( aKeys, xxVal )
86
      else
87
        raise ArgumentError, "E_BAD_VAL__IS_NOT_HASH"
88
      end
89
    end
90
91
    def getPropSingle(yKey=nil)
92
      if yKey.nil?
93
        raise ArgumentError, 'E_BAD_KEY__IS_NIL'
94
      end
95
      yKey  = yKey.to_sym if yKey.kind_of?(String)
96
      xxVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
97
      if xxVal.nil? && @bIsStrict
98
        self.validateKey(yKey)
99
      end
100
      return xxVal
101
    end
102
103
    def validateKey(yKey=nil)
104
      if yKey.nil?
105
        raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]"
106
      end
107
108
      return true unless @bIsStrict
109
110
      bKeyExists = @dSchema.key?(:properties) && @dSchema[:properties].key?(yKey) ? true : false
111
112
      unless bKeyExists
113
        raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}"
114
      end
115
116
      return true
117
    end
118
119
    def setProp(yKey=nil,xxVal=nil)
120
      yKey = yKey.to_sym if yKey.kind_of?(String)
121
122
      self.validateKey(yKey)
123
124
      @dDocument[yKey] = xxVal
125
    end
126
127
    def pushProp(yKey=nil,xxVal=nil)
128
      yKey = yKey.to_sym if yKey.kind_of?(String)
129
130
      self.validateKey(yKey)
131
132
      if @dDocument.key?(yKey)
133
        if @dDocument[yKey].kind_of?(Array)
134
          @dDocument[yKey].push(xxVal)
135
        else
136
          raise RuntimeError, 'E_PROPERTY_IS_NOT_ARRAY'
137
        end
138
      else
139
        @dDocument[yKey] = [xxVal]
140
      end
141
    end
142
143
    def cpProp(yKeySrc=nil,yKeyDest=nil)
144
      yKeySrc  = yKeySrc.to_sym  if yKeySrc.kind_of?(String)
145
      yKeyDest = yKeyDest.to_sym if yKeyDest.kind_of?(String)
146
      self.setAttr(yKeyDest, self.getAttr(yKeySrc))
147
    end
148
149
    def sortKeys()
150
      @dDocument.keys.sort!
151
    end
152
153
    def fromJson(jDocument=nil)
154
      if jDocument.kind_of?(String)
155
        @dDocument = JSON.load(jDocument)
156
      end
157
      return self
158
    end
159
160
    def fromDict(dDocument=nil)
161
      @dDocument = dDocument if dDocument.is_a?(Hash)
162
    end
163
164
    def asHash()
165
      return @dDocument
166
    end
167
168
    def asJson()
169
      return JSON.dump( self.asHash() )
170
    end
171
172
    def getValStringForProperties(aCols=nil,sDelimiter="\t")
173
      sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
174
      aVals = self.getValArrayForProperties(aCols)
175
      sVals = aVals.join(sDelimiter)
176
      return sVals
177
    end
178
179
    def getValArrayForProperties(aCols=nil,xxNil='')
180
      aVals = []
181
      return aVals if aCols.nil?
182
183
      if @bUseKeyAsDesc
184
        asVals = aCols.map {|x| x.to_s }
185
      end
186
187
      aCols.each do |yKey|
188
189
        yKey  = yKey.to_sym if yKey.kind_of?(String)
190
        xxVal = getProp( yKey )
191
        #xVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
192
        xxVal = xxNil if xxVal.nil?
193
        aVals.push( xxVal )
194
195
      end
196
      return aVals
197
    end
198
199
    def getDescStringForProperties(aCols=nil,sDelimiter="\t")
200
      sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
201
      aVals = self.getDescArrayForProperties(aCols)
202
      sVals = aVals.join(sDelimiter)
203
      return sVals
204
    end
205
206
    def getDescArrayForProperties(aCols=nil)
207
      aVals = []
208
      return aVals if aCols.nil?
209
      aCols.each do |yKey|
210
211
        yKey  = yKey.to_sym if yKey.kind_of?(String)
212
        xxVal = (
213
          @dSchema.key?(:properties)                          \
214
          && @dSchema[:properties].key?(yKey)                 \
215
          && @dSchema[:properties][yKey].key?(:description)   \
216
          && @dSchema[:properties][yKey][:description].length > 0
217
        ) \
218
          ? @dSchema[:properties][yKey][:description] : yKey.to_s
219
220
        xxVal = xxVal.to_s unless xxVal.is_a?(String)
221
222
        aVals.push( xxVal )
223
      end
224
      return aVals
225
    end
226
227
    alias_method :setAttr , :setProp
228
    alias_method :getAttr , :getProp
229
    alias_method :pushAttr, :pushProp
230
    alias_method :cpAttr  , :cpProp
231
  end
232
end