Class: JsonDoc::Document
- Inherits:
-
Object
- Object
- JsonDoc::Document
- Defined in:
- lib/jsondoc/document.rb
Instance Attribute Summary collapse
-
#bIsStrict ⇒ Object
Returns the value of attribute bIsStrict.
-
#bUseDeepKeys ⇒ Object
Returns the value of attribute bUseDeepKeys.
-
#bUseKeyAsDesc ⇒ Object
Returns the value of attribute bUseKeyAsDesc.
-
#dDocument ⇒ Object
readonly
Returns the value of attribute dDocument.
Instance Method Summary collapse
- #asHash ⇒ Object
- #asJson ⇒ Object
- #cpProp(yKeySrc = nil, yKeyDest = nil) ⇒ Object (also: #cpAttr)
- #fromDict(dDocument = nil) ⇒ Object
- #fromJson(jDocument = nil) ⇒ Object
- #getDefaultDocument ⇒ Object
- #getDefaultSchema ⇒ Object
- #getDescArrayForProperties(aCols = nil) ⇒ Object
- #getDescStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object
- #getProp(yKey = nil) ⇒ Object (also: #getAttr)
- #getPropRecurse(aKeys = [], dDoc = nil) ⇒ Object
- #getPropSingle(yKey = nil) ⇒ Object
- #getValArrayForProperties(aCols = nil, xxNil = '') ⇒ Object
- #getValStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object
-
#initialize(dValues = nil, dSchema = nil, bDefaultifyDoc = false, bIsStrict = true, opts = {}) ⇒ Document
constructor
A new instance of Document.
- #loadHash(dValues = nil) ⇒ Object
- #pushProp(yKey = nil, xxVal = nil) ⇒ Object (also: #pushAttr)
- #setProp(yKey = nil, xxVal = nil) ⇒ Object (also: #setAttr)
- #sortKeys ⇒ Object
- #validateKey(yKey = nil) ⇒ Object
Constructor Details
#initialize(dValues = nil, dSchema = nil, bDefaultifyDoc = false, bIsStrict = true, opts = {}) ⇒ Document
Returns a new instance of Document.
15 16 17 18 19 20 21 22 23 |
# File 'lib/jsondoc/document.rb', line 15 def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true,opts={}) @dSchema = dSchema || self.getDefaultSchema() @bDefaultifyDoc = bDefaultifyDoc ? true : false @bIsStrict = bIsStrict ? true : false @bUseKeyAsDesc = false @bUseDeepKeys = opts.key?(:bUseDeepKeys) ? opts[:bUseDeepKeys] : true @dDocument = self.getDefaultDocument() self.loadHash(dValues) if dValues.is_a?(Hash) end |
Instance Attribute Details
#bIsStrict ⇒ Object
Returns the value of attribute bIsStrict.
11 12 13 |
# File 'lib/jsondoc/document.rb', line 11 def bIsStrict @bIsStrict end |
#bUseDeepKeys ⇒ Object
Returns the value of attribute bUseDeepKeys.
13 14 15 |
# File 'lib/jsondoc/document.rb', line 13 def bUseDeepKeys @bUseDeepKeys end |
#bUseKeyAsDesc ⇒ Object
Returns the value of attribute bUseKeyAsDesc.
12 13 14 |
# File 'lib/jsondoc/document.rb', line 12 def bUseKeyAsDesc @bUseKeyAsDesc end |
#dDocument ⇒ Object (readonly)
Returns the value of attribute dDocument.
9 10 11 |
# File 'lib/jsondoc/document.rb', line 9 def dDocument @dDocument end |
Instance Method Details
#asHash ⇒ Object
155 156 157 |
# File 'lib/jsondoc/document.rb', line 155 def asHash @dDocument end |
#asJson ⇒ Object
159 160 161 |
# File 'lib/jsondoc/document.rb', line 159 def asJson JSON.dump( self.asHash() ) end |
#cpProp(yKeySrc = nil, yKeyDest = nil) ⇒ Object Also known as: cpAttr
134 135 136 137 138 |
# File 'lib/jsondoc/document.rb', line 134 def cpProp(yKeySrc = nil, yKeyDest = nil) yKeySrc = yKeySrc.to_sym if yKeySrc.is_a?(String) yKeyDest = yKeyDest.to_sym if yKeyDest.is_a?(String) self.setAttr(yKeyDest, self.getAttr(yKeySrc)) end |
#fromDict(dDocument = nil) ⇒ Object
151 152 153 |
# File 'lib/jsondoc/document.rb', line 151 def fromDict(dDocument = nil) @dDocument = dDocument if dDocument.is_a?(Hash) end |
#fromJson(jDocument = nil) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/jsondoc/document.rb', line 144 def fromJson(jDocument = nil) if jDocument.is_a?(String) @dDocument = JSON.load(jDocument) end return self end |
#getDefaultDocument ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jsondoc/document.rb', line 34 def getDefaultDocument dDocument = {} if @bDefaultifyDoc && @dSchema.key?(:properties) @dSchema[:properties].keys.each do |yKey| dProperty = @dSchema[:properties][yKey] xxVal = dProperty.key?(:default) ? dProperty[:default] : '' dDocument[yKey] = xxVal end end dDocument end |
#getDefaultSchema ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/jsondoc/document.rb', line 25 def getDefaultSchema { type: '', properties: { id: {default: '', description: 'Doc Id', type: 'string'} } } end |
#getDescArrayForProperties(aCols = nil) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/jsondoc/document.rb', line 193 def getDescArrayForProperties(aCols = nil) aVals = [] return aVals if aCols.nil? aCols.each do |yKey| yKey = yKey.to_sym if yKey.is_a? String xxVal = ( @dSchema.key?(:properties) \ && @dSchema[:properties].key?(yKey) \ && @dSchema[:properties][yKey].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 aVals end |
#getDescStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object
187 188 189 190 191 |
# File 'lib/jsondoc/document.rb', line 187 def getDescStringForProperties(aCols = nil,sDelimiter = "\t") sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0 aVals = self.getDescArrayForProperties(aCols) aVals.join(sDelimiter) end |
#getProp(yKey = nil) ⇒ Object Also known as: getAttr
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jsondoc/document.rb', line 58 def getProp(yKey = nil) raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil? yKey = yKey.to_sym if yKey.is_a?(String) if @bUseDeepKeys aKeys = yKey.split('.') # = yKey.to_s.split('.').map(&:to_sym) dDoc = @dDocument xxVal = getPropRecurse(aKeys.clone,dDoc) return xxVal end return @dDocument.key?(yKey) ? @dDocument[yKey] : nil end |
#getPropRecurse(aKeys = [], dDoc = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/jsondoc/document.rb', line 73 def getPropRecurse(aKeys = [], dDoc = nil) yKey = aKeys.shift if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.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
88 89 90 91 92 93 94 95 96 |
# File 'lib/jsondoc/document.rb', line 88 def getPropSingle(yKey = nil) raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil? yKey = yKey.to_sym if yKey.is_a?(String) xxVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil if xxVal.nil? && @bIsStrict self.validateKey(yKey) end xxVal end |
#getValArrayForProperties(aCols = nil, xxNil = '') ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/jsondoc/document.rb', line 169 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.is_a? String xxVal = getProp( yKey ) #xVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil xxVal = xxNil if xxVal.nil? aVals.push xxVal end aVals end |
#getValStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object
163 164 165 166 167 |
# File 'lib/jsondoc/document.rb', line 163 def getValStringForProperties(aCols = nil, sDelimiter = "\t") sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0 aVals = self.getValArrayForProperties(aCols) return aVals.join(sDelimiter) 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 self end |
#pushProp(yKey = nil, xxVal = nil) ⇒ Object Also known as: pushAttr
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/jsondoc/document.rb', line 119 def pushProp(yKey = nil, xxVal = nil) yKey = yKey.to_sym if yKey.is_a?(String) self.validateKey(yKey) if @dDocument.key?(yKey) if @dDocument[yKey].is_a?(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
111 112 113 114 115 116 117 |
# File 'lib/jsondoc/document.rb', line 111 def setProp(yKey = nil, xxVal = nil) yKey = yKey.to_sym if yKey.is_a?(String) self.validateKey(yKey) @dDocument[yKey] = xxVal end |
#sortKeys ⇒ Object
140 141 142 |
# File 'lib/jsondoc/document.rb', line 140 def sortKeys @dDocument.keys.sort! end |
#validateKey(yKey = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/jsondoc/document.rb', line 98 def validateKey(yKey = nil) raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]" if yKey.nil? return true unless @bIsStrict bKeyExists = @dSchema.key?(:properties) \ && @dSchema[:properties].key?(yKey) ? true : false raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}" unless bKeyExists return true end |