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.
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)
- #getValArrayForProperties(aCols = nil) ⇒ Object
- #getValStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object
-
#initialize(dValues = nil, dSchema = nil, bDefaultifyDoc = false, bIsStrict = true) ⇒ Document
constructor
A new instance of Document.
- #loadInitialValues(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) ⇒ Document
Returns a new instance of Document.
8 9 10 11 12 13 14 |
# File 'lib/jsondoc/document.rb', line 8 def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true) @dSchema = dSchema || self.getDefaultSchema() @bDefaultifyDoc = bDefaultifyDoc ? true : false @bIsStrict = bIsStrict ? true : false @dDocument = self.getDefaultDocument() self.loadInitialValues(dValues) end |
Instance Attribute Details
#bIsStrict ⇒ Object
Returns the value of attribute bIsStrict.
6 7 8 |
# File 'lib/jsondoc/document.rb', line 6 def bIsStrict @bIsStrict end |
Instance Method Details
#asHash ⇒ Object
122 123 124 |
# File 'lib/jsondoc/document.rb', line 122 def asHash() return @dDocument end |
#asJson ⇒ Object
126 127 128 |
# File 'lib/jsondoc/document.rb', line 126 def asJson() return JSON.dump( self.asHash() ) end |
#cpProp(yKeySrc = nil, yKeyDest = nil) ⇒ Object Also known as: cpAttr
101 102 103 104 105 |
# File 'lib/jsondoc/document.rb', line 101 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
118 119 120 |
# File 'lib/jsondoc/document.rb', line 118 def fromDict(dDocument=nil) @dDocument = dDocument if dDocument.is_a?(Hash) end |
#fromJson(jDocument = nil) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/jsondoc/document.rb', line 111 def fromJson(jDocument=nil) if jDocument.kind_of?(String) @dDocument = JSON.load(jDocument) end return self end |
#getDefaultDocument ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/jsondoc/document.rb', line 26 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 |
#getDefaultSchema ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/jsondoc/document.rb', line 16 def getDefaultSchema() dSchema = { :type => '', :properties => { :id => { :default => '', :description => 'Doc Id', :type => 'string' } } } return dSchema end |
#getDescArrayForProperties(aCols = nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/jsondoc/document.rb', line 157 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[:properties].has_key?(yKey) && @dSchema[:properties][yKey].has_key?(:description) && @dSchema[:properties][yKey][:description].length > 0 ) \ ? @dSchema[:properties][yKey][:description] : yKey.to_s aVals.push( xxVal ) end return aVals end |
#getDescStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object
150 151 152 153 154 155 |
# File 'lib/jsondoc/document.rb', line 150 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
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jsondoc/document.rb', line 49 def getProp(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) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/jsondoc/document.rb', line 137 def getValArrayForProperties(aCols=nil) aVals = [] return aVals if aCols.nil? aCols.each do |yKey| yKey = yKey.to_sym if yKey.kind_of?(String) xVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil aVals.push( xVal ) end return aVals end |
#getValStringForProperties(aCols = nil, sDelimiter = "\t") ⇒ Object
130 131 132 133 134 135 |
# File 'lib/jsondoc/document.rb', line 130 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 |
#loadInitialValues(dValues = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/jsondoc/document.rb', line 38 def loadInitialValues(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 end |
#pushProp(yKey = nil, xxVal = nil) ⇒ Object Also known as: pushAttr
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jsondoc/document.rb', line 85 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
77 78 79 80 81 82 83 |
# File 'lib/jsondoc/document.rb', line 77 def setProp(yKey=nil,xxVal=nil) yKey = yKey.to_sym if yKey.kind_of?(String) self.validateKey(yKey) @dDocument[yKey] = xxVal end |
#sortKeys ⇒ Object
107 108 109 |
# File 'lib/jsondoc/document.rb', line 107 def sortKeys() @dDocument.keys.sort! end |
#validateKey(yKey = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jsondoc/document.rb', line 61 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 |