Class: JsonDoc::Document
- Inherits:
-
Object
- Object
- JsonDoc::Document
- Defined in:
- lib/jsondoc/document.rb
Instance Method Summary collapse
- #asHash ⇒ Object
- #asJson ⇒ Object
- #cpAttr(yKeySrc = nil, yKeyDest = nil) ⇒ Object
- #fromDict(dDocument = nil) ⇒ Object
- #fromJson(jDocument = nil) ⇒ Object
- #getAttr(yKey = nil) ⇒ Object
- #getDefaultDocument ⇒ Object
- #getDefaultSchema ⇒ Object
- #getDescArrayForProperties(aCols = nil) ⇒ Object
- #getDescStringForProperties(aCols = nil, sDelimiter = nil) ⇒ Object
- #getValArrayForProperties(aCols = nil) ⇒ Object
- #getValStringForProperties(aCols = nil, sDelimiter = nil) ⇒ Object
-
#initialize(dSchema = nil, bDefaultifyDoc = false, bIsStrict = true) ⇒ Document
constructor
A new instance of Document.
- #pushAttr(yKey = nil, xxVal = nil) ⇒ Object
- #setAttr(yKey = nil, xxVal = nil) ⇒ Object
- #sortKeys ⇒ Object
- #validateKey(yKey = nil) ⇒ Object
Constructor Details
#initialize(dSchema = nil, bDefaultifyDoc = false, bIsStrict = true) ⇒ Document
Returns a new instance of Document.
6 7 8 9 10 11 |
# File 'lib/jsondoc/document.rb', line 6 def initialize(dSchema=nil,bDefaultifyDoc=false,bIsStrict=true) @dSchema = dSchema || self.getDefaultSchema() @bDefaultifyDoc = bDefaultifyDoc ? true : false @bIsStrict = bIsStrict ? true : false @dDocument = self.getDefaultDocument() end |
Instance Method Details
#asHash ⇒ Object
105 106 107 |
# File 'lib/jsondoc/document.rb', line 105 def asHash() return @dDocument end |
#asJson ⇒ Object
109 110 111 |
# File 'lib/jsondoc/document.rb', line 109 def asJson() return JSON.dump( self.asHash() ) end |
#cpAttr(yKeySrc = nil, yKeyDest = nil) ⇒ Object
81 82 83 84 85 |
# File 'lib/jsondoc/document.rb', line 81 def cpAttr(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
101 102 103 |
# File 'lib/jsondoc/document.rb', line 101 def fromDict(dDocument=nil) @dDocument = dDocument if dDocument.is_a?(Hash) end |
#fromJson(jDocument = nil) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/jsondoc/document.rb', line 94 def fromJson(jDocument=nil) if jDocument.kind_of?(String) @dDocument = JSON.load(jDocument) end return self end |
#getAttr(yKey = nil) ⇒ Object
35 36 37 38 39 |
# File 'lib/jsondoc/document.rb', line 35 def getAttr(yKey=nil) yKey = yKey.to_sym if yKey.kind_of?(String) xxVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil return xxVal end |
#getDefaultDocument ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jsondoc/document.rb', line 23 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
13 14 15 16 17 18 19 20 21 |
# File 'lib/jsondoc/document.rb', line 13 def getDefaultSchema() dSchema = { :type => '', :properties => { :id => { :default => '', :description => 'Doc Id', :type => 'string' } } } return dSchema end |
#getDescArrayForProperties(aCols = nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/jsondoc/document.rb', line 142 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 = nil) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/jsondoc/document.rb', line 134 def getDescStringForProperties(aCols=nil,sDelimiter=nil) sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0 aVals = self.getDescArrayForProperties(aCols) sVals = aVals.join(sDelimiter) #sVals = aVals.join("\t") return sVals end |
#getValArrayForProperties(aCols = nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/jsondoc/document.rb', line 121 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 = nil) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/jsondoc/document.rb', line 113 def getValStringForProperties(aCols=nil,sDelimiter=nil) sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0 aVals = self.getValArrayForProperties(aCols) sVals = aVals.join(sDelimiter) #sVals = aVals.join("\t") return sVals end |
#pushAttr(yKey = nil, xxVal = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jsondoc/document.rb', line 63 def pushAttr(yKey=nil,xxVal=nil) yKey = yKey.to_sym if yKey.kind_of?(String) self.validateKey(yKey) sType = @dSchema[:properties][yKey].has_key?(:type) ? @dSchema[:properties][yKey][:type] : nil if sType == 'array' if @dDocument.has_key?(yKey) @dDocument[yKey].push(xxVal) else @dDocument[yKey] = [xxVal] end else @dDocument[yKey] = xxVal end end |
#setAttr(yKey = nil, xxVal = nil) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/jsondoc/document.rb', line 55 def setAttr(yKey=nil,xxVal=nil) yKey = yKey.to_sym if yKey.kind_of?(String) self.validateKey(yKey) @dDocument[yKey] = xxVal end |
#sortKeys ⇒ Object
87 88 89 90 91 92 |
# File 'lib/jsondoc/document.rb', line 87 def sortKeys() @dDocument.keys.sort! #dProperties = @dDocument[:properties] #dProperties = dProperties.sort #@dDocument[:properties] = dProperties end |
#validateKey(yKey = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jsondoc/document.rb', line 41 def validateKey(yKey=nil) if yKey.nil? raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]" end bKeyExists = @dSchema.has_key?(:properties) && @dSchema[:properties].has_key?(yKey) ? true : false if @bIsStrict && ! bKeyExists raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}" end return true end |