jsondoc 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d31ab6bc50e452d8df5d5ffe3eec2517671353b
4
- data.tar.gz: 6ef7c6ae540f56e4ad608e7e1952ed1d5185d43d
3
+ metadata.gz: a67112a09bdcadf7b01a4da9f764afcc2b997718
4
+ data.tar.gz: c16488cb845e829d9d40b1bce7ebc7ee136219dc
5
5
  SHA512:
6
- metadata.gz: 594bb9c373b34df5a94df646882a41b197e4f3b5b62e4df0c8164aefff6833e1da0f3c14a73608dadfa43a3ce15b56e6c7d11f2bc0aebbdfd03bb5d1d2e8f146
7
- data.tar.gz: 5f3ff7c0a942cf38d6cb258fc39ca98c515e588dcf8bb65b571a5450c66e8df2fe11a6fee4e0fbb1f357ecd0f453ca44b1836171bc98b63bd527424ff606f74c
6
+ metadata.gz: 8f81b7d01dd4ae7d0b9eb27913fcd726ef1ed09b1283673dccda60858a348e5caa60fdee3cb9a960f6cda8755f4f47246f31b84815d76399986b721111f2cb67
7
+ data.tar.gz: 5a6fef198638b13bfedceb507f33459aed78353179763f928f171be7429358f8625b058d03a08d5ea11ca9ac6a6ca868d35569cd48b4b11af8647dcce64da8a2
@@ -0,0 +1,18 @@
1
+ CHANGELOG
2
+ ---------
3
+ - **2014-03-16**: 0.0.4
4
+ - Changed Attr methods to Prop methods to represent JSON schema property
5
+ - Added Attr methods as aliases
6
+ - Added ability to load initial values hash
7
+ - Added key validation for #getProp() in strict mode
8
+ - Added attr_accessor for bIsStrict
9
+ - #push(Prop\Attr) will now only act on arrays
10
+
11
+ - **2014-02-12**: 0.0.3
12
+ - Added cpAttr() method
13
+
14
+ - **2014-01-31**: 0.0.2
15
+ - README bugfix
16
+
17
+ - **2014-01-28**: 0.0.1
18
+ - Initial release
File without changes
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jsondoc'
3
- s.version = '0.0.3'
4
- s.date = '2014-02-12'
3
+ s.version = '0.0.4'
4
+ s.date = '2014-03-16'
5
5
  s.summary = 'JsonDoc'
6
6
  s.description = 'A base document object'
7
7
  s.authors = ['John Wang']
8
8
  s.email = 'john@johnwang.com'
9
9
  s.files = [
10
- 'CHANGELOG',
11
- 'MIT-LICENSE',
10
+ 'CHANGELOG.md',
11
+ 'LICENSE',
12
12
  'README.rdoc',
13
13
  'Rakefile',
14
14
  'VERSION',
@@ -19,4 +19,4 @@ Gem::Specification.new do |s|
19
19
  ]
20
20
  s.homepage = 'http://johnwang.com/'
21
21
  s.license = 'MIT'
22
- end
22
+ end
@@ -3,11 +3,14 @@ require 'json'
3
3
  module JsonDoc
4
4
  class Document
5
5
 
6
- def initialize(dSchema=nil,bDefaultifyDoc=false,bIsStrict=true)
6
+ attr_accessor :bIsStrict
7
+
8
+ def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true)
7
9
  @dSchema = dSchema || self.getDefaultSchema()
8
10
  @bDefaultifyDoc = bDefaultifyDoc ? true : false
9
11
  @bIsStrict = bIsStrict ? true : false
10
12
  @dDocument = self.getDefaultDocument()
13
+ self.loadInitialValues(dValues)
11
14
  end
12
15
 
13
16
  def getDefaultSchema()
@@ -32,9 +35,26 @@ module JsonDoc
32
35
  return dDocument
33
36
  end
34
37
 
35
- def getAttr(yKey=nil)
38
+ def loadInitialValues(dValues=nil)
39
+ if dValues.nil?
40
+ return
41
+ elsif ! dValues.is_a?(Hash)
42
+ raise ArgumentError, 'E_INITIAL_VALUES_IS_NOT_A_HASH'
43
+ end
44
+ dValues.each do |yKey,xxVal|
45
+ self.setProp(yKey,xxVal)
46
+ end
47
+ end
48
+
49
+ def getProp(yKey=nil)
50
+ if yKey.nil?
51
+ raise ArgumentError, 'E_BAD_KEY__IS_NIL'
52
+ end
36
53
  yKey = yKey.to_sym if yKey.kind_of?(String)
37
54
  xxVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
55
+ if xxVal.nil? && @bIsStrict
56
+ self.validateKey(yKey)
57
+ end
38
58
  return xxVal
39
59
  end
40
60
 
@@ -43,16 +63,18 @@ module JsonDoc
43
63
  raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]"
44
64
  end
45
65
 
66
+ return true unless @bIsStrict
67
+
46
68
  bKeyExists = @dSchema.has_key?(:properties) && @dSchema[:properties].has_key?(yKey) ? true : false
47
69
 
48
- if @bIsStrict && ! bKeyExists
70
+ unless bKeyExists
49
71
  raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}"
50
72
  end
51
73
 
52
74
  return true
53
75
  end
54
76
 
55
- def setAttr(yKey=nil,xxVal=nil)
77
+ def setProp(yKey=nil,xxVal=nil)
56
78
  yKey = yKey.to_sym if yKey.kind_of?(String)
57
79
 
58
80
  self.validateKey(yKey)
@@ -60,25 +82,23 @@ module JsonDoc
60
82
  @dDocument[yKey] = xxVal
61
83
  end
62
84
 
63
- def pushAttr(yKey=nil,xxVal=nil)
85
+ def pushProp(yKey=nil,xxVal=nil)
64
86
  yKey = yKey.to_sym if yKey.kind_of?(String)
65
87
 
66
88
  self.validateKey(yKey)
67
89
 
68
- sType = @dSchema[:properties][yKey].has_key?(:type) ? @dSchema[:properties][yKey][:type] : nil
69
-
70
- if sType == 'array'
71
- if @dDocument.has_key?(yKey)
90
+ if @dDocument.has_key?(yKey)
91
+ if @dDocument[yKey].kind_of?(Array)
72
92
  @dDocument[yKey].push(xxVal)
73
93
  else
74
- @dDocument[yKey] = [xxVal]
94
+ raise RuntimeError, 'E_PROPERTY_IS_NOT_ARRAY'
75
95
  end
76
96
  else
77
- @dDocument[yKey] = xxVal
97
+ @dDocument[yKey] = [xxVal]
78
98
  end
79
99
  end
80
100
 
81
- def cpAttr(yKeySrc=nil,yKeyDest=nil)
101
+ def cpProp(yKeySrc=nil,yKeyDest=nil)
82
102
  yKeySrc = yKeySrc.to_sym if yKeySrc.kind_of?(String)
83
103
  yKeyDest = yKeyDest.to_sym if yKeyDest.kind_of?(String)
84
104
  self.setAttr(yKeyDest, self.getAttr(yKeySrc))
@@ -86,9 +106,6 @@ module JsonDoc
86
106
 
87
107
  def sortKeys()
88
108
  @dDocument.keys.sort!
89
- #dProperties = @dDocument[:properties]
90
- #dProperties = dProperties.sort
91
- #@dDocument[:properties] = dProperties
92
109
  end
93
110
 
94
111
  def fromJson(jDocument=nil)
@@ -110,11 +127,10 @@ module JsonDoc
110
127
  return JSON.dump( self.asHash() )
111
128
  end
112
129
 
113
- def getValStringForProperties(aCols=nil,sDelimiter=nil)
130
+ def getValStringForProperties(aCols=nil,sDelimiter="\t")
114
131
  sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
115
132
  aVals = self.getValArrayForProperties(aCols)
116
133
  sVals = aVals.join(sDelimiter)
117
- #sVals = aVals.join("\t")
118
134
  return sVals
119
135
  end
120
136
 
@@ -131,11 +147,10 @@ module JsonDoc
131
147
  return aVals
132
148
  end
133
149
 
134
- def getDescStringForProperties(aCols=nil,sDelimiter=nil)
150
+ def getDescStringForProperties(aCols=nil,sDelimiter="\t")
135
151
  sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
136
152
  aVals = self.getDescArrayForProperties(aCols)
137
153
  sVals = aVals.join(sDelimiter)
138
- #sVals = aVals.join("\t")
139
154
  return sVals
140
155
  end
141
156
 
@@ -153,5 +168,10 @@ module JsonDoc
153
168
  return aVals
154
169
  end
155
170
 
171
+ alias_method :setAttr , :setProp
172
+ alias_method :getAttr , :getProp
173
+ alias_method :pushAttr, :pushProp
174
+ alias_method :cpAttr , :cpProp
175
+
156
176
  end
157
177
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsondoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Wang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-12 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A base document object
14
14
  email: john@johnwang.com
@@ -16,8 +16,8 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - CHANGELOG
20
- - MIT-LICENSE
19
+ - CHANGELOG.md
20
+ - LICENSE
21
21
  - README.rdoc
22
22
  - Rakefile
23
23
  - VERSION
data/CHANGELOG DELETED
@@ -1,8 +0,0 @@
1
- = 0.0.3
2
- - Added cpAttr() method
3
-
4
- = 0.0.2
5
- - README bugfix
6
-
7
- = 0.0.1
8
- - Initial release