Class: JsonDoc::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/jsondoc/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dValues = nil, dSchema = nil, bDefaultifyDoc = false, bIsStrict = true) ⇒ Document

Returns a new instance of Document.



15
16
17
18
19
20
21
22
# File 'lib/jsondoc/document.rb', line 15

def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true)
  @dSchema        = dSchema || self.getDefaultSchema()
  @bDefaultifyDoc = bDefaultifyDoc ? true : false
  @bIsStrict      = bIsStrict      ? true : false
  @bUseKeyAsDesc  = false
  @dDocument      = self.getDefaultDocument()
  self.loadHash(dValues) if dValues.is_a?(Hash)
end

Instance Attribute Details

#bIsStrictObject

Returns the value of attribute bIsStrict.



12
13
14
# File 'lib/jsondoc/document.rb', line 12

def bIsStrict
  @bIsStrict
end

#bUseKeyAsDescObject

Returns the value of attribute bUseKeyAsDesc.



13
14
15
# File 'lib/jsondoc/document.rb', line 13

def bUseKeyAsDesc
  @bUseKeyAsDesc
end

Instance Method Details

#asHashObject



159
160
161
# File 'lib/jsondoc/document.rb', line 159

def asHash()
  return @dDocument
end

#asJsonObject



163
164
165
# File 'lib/jsondoc/document.rb', line 163

def asJson()
  return JSON.dump( self.asHash() )
end

#cpProp(yKeySrc = nil, yKeyDest = nil) ⇒ Object Also known as: cpAttr



138
139
140
141
142
# File 'lib/jsondoc/document.rb', line 138

def cpProp(yKeySrc=nil,yKeyDest=nil)
  yKeySrc  = yKeySrc.to_sym  if yKeySrc.kind_of?(String)
  yKeyDest = yKeyDest.to_sym if yKeyDest.kind_of?(String)
  self.setAttr(yKeyDest, self.getAttr(yKeySrc))
end

#fromDict(dDocument = nil) ⇒ Object



155
156
157
# File 'lib/jsondoc/document.rb', line 155

def fromDict(dDocument=nil)
  @dDocument = dDocument if dDocument.is_a?(Hash)
end

#fromJson(jDocument = nil) ⇒ Object



148
149
150
151
152
153
# File 'lib/jsondoc/document.rb', line 148

def fromJson(jDocument=nil)
  if jDocument.kind_of?(String)
    @dDocument = JSON.load(jDocument)
  end
  return self
end

#getDefaultDocumentObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jsondoc/document.rb', line 34

def getDefaultDocument()
  dDocument = {}
  if @bDefaultifyDoc && @dSchema.has_key?(:properties)
    @dSchema[:properties].keys.each do |yKey|
      dProperty = @dSchema[:properties][yKey]
      xxVal     = dProperty.has_key?(:default) ? dProperty[:default] : ''
      dDocument[yKey] = xxVal
    end
  end
  return dDocument
end

#getDefaultSchemaObject



24
25
26
27
28
29
30
31
32
# File 'lib/jsondoc/document.rb', line 24

def getDefaultSchema()
  dSchema       =  {
    :type       => '',
    :properties => {
      :id       => { :default => '', :description => 'Doc Id', :type => 'string' }
    }
  }
  return dSchema
end

#getDescArrayForProperties(aCols = nil) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/jsondoc/document.rb', line 201

def getDescArrayForProperties(aCols=nil)
  aVals = []
  return aVals if aCols.nil?
  aCols.each do |yKey|

    yKey  = yKey.to_sym if yKey.kind_of?(String)
    xxVal = (
      @dSchema.has_key?(:properties)                          \
      && @dSchema[:properties].has_key?(yKey)                 \
      && @dSchema[:properties][yKey].has_key?(:description)   \
      && @dSchema[:properties][yKey][:description].length > 0
    ) \
      ? @dSchema[:properties][yKey][:description] : yKey.to_s

    xxVal = xxVal.to_s unless xxVal.is_a?(String)

    aVals.push( xxVal )
  end
  return aVals
end

#getDescStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object



194
195
196
197
198
199
# File 'lib/jsondoc/document.rb', line 194

def getDescStringForProperties(aCols=nil,sDelimiter="\t")
  sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
  aVals = self.getDescArrayForProperties(aCols)
  sVals = aVals.join(sDelimiter)
  return sVals
end

#getProp(yKey = nil) ⇒ Object Also known as: getAttr



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jsondoc/document.rb', line 58

def getProp(yKey=nil)
  if yKey.nil?
    raise ArgumentError, 'E_BAD_KEY__IS_NIL'
  end
  yKey  = yKey.to_sym if yKey.kind_of?(String)
  aKeys = yKey.split('.')
  #aKeys = yKey.to_s.split('.').map(&:to_sym)

  dDoc  = @dDocument
  xxVal = getPropRecurse(aKeys.clone,dDoc)
  return xxVal
end

#getPropRecurse(aKeys = [], dDoc = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jsondoc/document.rb', line 71

def getPropRecurse(aKeys=[],dDoc=nil)
  yKey = aKeys.shift
  if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.has_key?( yKey )
    return nil
  end
  xxVal = dDoc[ yKey ]
  if aKeys.length == 0
    return xxVal
  elsif dDoc.is_a?(Hash)
    return getPropRecurse( aKeys, xxVal )
  else
    raise ArgumentError, "E_BAD_VAL__IS_NOT_HASH"
  end
end

#getPropSingle(yKey = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jsondoc/document.rb', line 86

def getPropSingle(yKey=nil)
  if yKey.nil?
    raise ArgumentError, 'E_BAD_KEY__IS_NIL'
  end
  yKey  = yKey.to_sym if yKey.kind_of?(String)
  xxVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
  if xxVal.nil? && @bIsStrict
    self.validateKey(yKey)
  end
  return xxVal
end

#getValArrayForProperties(aCols = nil, xxNil = '') ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/jsondoc/document.rb', line 174

def getValArrayForProperties(aCols=nil,xxNil='')
  aVals = []
  return aVals if aCols.nil?

  if @bUseKeyAsDesc
    asVals = aCols.map {|x| x.to_s }
  end

  aCols.each do |yKey|

    yKey  = yKey.to_sym if yKey.kind_of?(String)
    xxVal = getProp( yKey )
    #xVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
    xxVal = xxNil if xxVal.nil?
    aVals.push( xxVal )

  end
  return aVals
end

#getValStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object



167
168
169
170
171
172
# File 'lib/jsondoc/document.rb', line 167

def getValStringForProperties(aCols=nil,sDelimiter="\t")
  sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
  aVals = self.getValArrayForProperties(aCols)
  sVals = aVals.join(sDelimiter)
  return sVals
end

#loadHash(dValues = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jsondoc/document.rb', line 46

def loadHash(dValues=nil)
  if dValues.nil?
    return
  elsif ! dValues.is_a?(Hash)
    raise ArgumentError, 'E_INITIAL_VALUES_IS_NOT_A_HASH'
  end
  dValues.each do |yKey,xxVal|
    self.setProp(yKey,xxVal)
  end
  return self
end

#pushProp(yKey = nil, xxVal = nil) ⇒ Object Also known as: pushAttr



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/jsondoc/document.rb', line 122

def pushProp(yKey=nil,xxVal=nil)
  yKey = yKey.to_sym if yKey.kind_of?(String)

  self.validateKey(yKey)

  if @dDocument.has_key?(yKey)
    if @dDocument[yKey].kind_of?(Array)
      @dDocument[yKey].push(xxVal)
    else
      raise RuntimeError, 'E_PROPERTY_IS_NOT_ARRAY'
    end
  else
    @dDocument[yKey] = [xxVal]
  end
end

#setProp(yKey = nil, xxVal = nil) ⇒ Object Also known as: setAttr



114
115
116
117
118
119
120
# File 'lib/jsondoc/document.rb', line 114

def setProp(yKey=nil,xxVal=nil)
  yKey = yKey.to_sym if yKey.kind_of?(String)

  self.validateKey(yKey)

  @dDocument[yKey] = xxVal
end

#sortKeysObject



144
145
146
# File 'lib/jsondoc/document.rb', line 144

def sortKeys()
  @dDocument.keys.sort!
end

#validateKey(yKey = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jsondoc/document.rb', line 98

def validateKey(yKey=nil)
  if yKey.nil?
    raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]"
  end

  return true unless @bIsStrict

  bKeyExists = @dSchema.has_key?(:properties) && @dSchema[:properties].has_key?(yKey) ? true : false

  unless bKeyExists
    raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}"
  end

  return true
end