jsondoc 0.1.2 → 0.1.3

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: 2f86e271afdb795782a2b537a51ec4328ce9ebf8
4
- data.tar.gz: f07965f57bcc460746c469cf7fefc61f8644afd4
3
+ metadata.gz: 2c654bdf353b981573a429df4c118bd4fd063e94
4
+ data.tar.gz: bd60cca64677d5810f68975d39de4a65c460cb1e
5
5
  SHA512:
6
- metadata.gz: d2bee4d62b7e41ccebeb823b57c0dc09d1663a8ecbae8618b9c85199e036f05b45f386cdb14ce8841afcd6cadc35a95a00cd5ffaf69a096a16b5464e232be320
7
- data.tar.gz: d83d68b2c3ff8c19aa94c55ea8ae3f87976aa40c27c6acc11bec872cc91483b68a641692971678a191f5c2bbbc08fdd04d0227037b1b9276ce94ce11e780a6b4
6
+ metadata.gz: e04c5cbde046644d5787950015328a5632fd3c56351ea74fb53a8192bf692439c952b371c4103d9da488d86d430bc33f020869d559adb2aca558eb9a11cb3132
7
+ data.tar.gz: e041d19abc0ca4c18d18aedba3e9e9a9b6be21a4204275537c2c7657264e48755037944bbe574291c7011d321afe3eaca446a7094c0ad7472bd469534795329f
@@ -1,26 +1,28 @@
1
1
  CHANGELOG
2
2
  ---------
3
+ - **2016-08-23**: 0.1.3
4
+ - Remove hard coded `require 'json'`
5
+ - **2016-02-05**: 0.1.2
6
+ - Add missing test file
7
+ - **2016-02-04**: 0.1.1
8
+ - Add initialize opts hash
9
+ - Add `attr_reader :dDocument`
10
+ - Add `bUseDeepKeys` to make deep key splitting optional
3
11
  - **2014-08-14**: 0.1.0
4
12
  - Support retrieval of properties from nested objects using period delmiter
5
-
6
13
  - **2014-06-05**: 0.0.6
7
- - Return self from loadHash()
8
-
14
+ - Return self from `loadHash()`
9
15
  - **2014-04-17**: 0.0.5
10
-
11
16
  - **2014-03-16**: 0.0.4
12
17
  - Changed Attr methods to Prop methods to represent JSON schema property
13
18
  - Added Attr methods as aliases
14
19
  - Added ability to load initial values hash
15
20
  - Added key validation for #getProp() in strict mode
16
- - Added attr_accessor for bIsStrict
21
+ - Added `attr_accessor :bIsStrict`
17
22
  - #push(Prop\Attr) will now only act on arrays
18
-
19
23
  - **2014-02-12**: 0.0.3
20
24
  - Added cpAttr() method
21
-
22
25
  - **2014-01-31**: 0.0.2
23
- - README bugfix
24
-
26
+ - `README` bugfix
25
27
  - **2014-01-28**: 0.0.1
26
- - Initial release
28
+ - Initial release
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rake'
2
2
  require 'rake/testtask'
3
3
 
4
4
  desc 'Default: run unit tests.'
5
- task :default => :test
5
+ task default: :test
6
6
 
7
7
  desc 'Test the library.'
8
8
  Rake::TestTask.new do |t|
@@ -13,7 +13,8 @@ end
13
13
 
14
14
  desc 'Generate YARD documentation.'
15
15
  task :gendoc do
16
- #puts 'yard doc generation disabled until JRuby build native extensions for redcarpet or yard removes the dependency.'
17
- system "yardoc"
18
- system "yard stats --list-undoc"
19
- end
16
+ # puts 'yard doc generation disabled until JRuby build \
17
+ # native extensions for redcarpet or yard removes the dependency.'
18
+ system 'yardoc'
19
+ system 'yard stats --list-undoc'
20
+ end
@@ -1,5 +1,5 @@
1
1
  module JsonDoc
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
 
4
4
  autoload :Document, 'jsondoc/document'
5
- end
5
+ end
@@ -1,5 +1,3 @@
1
- require 'json'
2
-
3
1
  class Symbol
4
2
  def split(separator)
5
3
  to_s.split(separator).map(&:to_sym)
@@ -17,36 +15,35 @@ module JsonDoc
17
15
  def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true,opts={})
18
16
  @dSchema = dSchema || self.getDefaultSchema()
19
17
  @bDefaultifyDoc = bDefaultifyDoc ? true : false
20
- @bIsStrict = bIsStrict ? true : false
18
+ @bIsStrict = bIsStrict ? true : false
21
19
  @bUseKeyAsDesc = false
22
20
  @bUseDeepKeys = opts.key?(:bUseDeepKeys) ? opts[:bUseDeepKeys] : true
23
21
  @dDocument = self.getDefaultDocument()
24
22
  self.loadHash(dValues) if dValues.is_a?(Hash)
25
23
  end
26
24
 
27
- def getDefaultSchema()
28
- dSchema = {
29
- :type => '',
30
- :properties => {
31
- :id => { :default => '', :description => 'Doc Id', :type => 'string' }
25
+ def getDefaultSchema
26
+ {
27
+ type: '',
28
+ properties: {
29
+ id: {default: '', description: 'Doc Id', type: 'string'}
32
30
  }
33
31
  }
34
- return dSchema
35
32
  end
36
33
 
37
- def getDefaultDocument()
34
+ def getDefaultDocument
38
35
  dDocument = {}
39
36
  if @bDefaultifyDoc && @dSchema.key?(:properties)
40
37
  @dSchema[:properties].keys.each do |yKey|
41
38
  dProperty = @dSchema[:properties][yKey]
42
- xxVal = dProperty.key?(:default) ? dProperty[:default] : ''
39
+ xxVal = dProperty.key?(:default) ? dProperty[:default] : ''
43
40
  dDocument[yKey] = xxVal
44
41
  end
45
42
  end
46
- return dDocument
43
+ dDocument
47
44
  end
48
45
 
49
- def loadHash(dValues=nil)
46
+ def loadHash(dValues = nil)
50
47
  if dValues.nil?
51
48
  return
52
49
  elsif ! dValues.is_a?(Hash)
@@ -55,18 +52,16 @@ module JsonDoc
55
52
  dValues.each do |yKey,xxVal|
56
53
  self.setProp(yKey,xxVal)
57
54
  end
58
- return self
55
+ self
59
56
  end
60
57
 
61
- def getProp(yKey=nil)
62
- if yKey.nil?
63
- raise ArgumentError, 'E_BAD_KEY__IS_NIL'
64
- end
65
- yKey = yKey.to_sym if yKey.kind_of?(String)
58
+ def getProp(yKey = nil)
59
+ raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil?
60
+
61
+ yKey = yKey.to_sym if yKey.is_a?(String)
66
62
 
67
63
  if @bUseDeepKeys
68
- aKeys = yKey.split('.')
69
- #aKeys = yKey.to_s.split('.').map(&:to_sym)
64
+ aKeys = yKey.split('.') # = yKey.to_s.split('.').map(&:to_sym)
70
65
 
71
66
  dDoc = @dDocument
72
67
  xxVal = getPropRecurse(aKeys.clone,dDoc)
@@ -75,7 +70,7 @@ module JsonDoc
75
70
  return @dDocument.key?(yKey) ? @dDocument[yKey] : nil
76
71
  end
77
72
 
78
- def getPropRecurse(aKeys=[],dDoc=nil)
73
+ def getPropRecurse(aKeys = [], dDoc = nil)
79
74
  yKey = aKeys.shift
80
75
  if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.key?( yKey )
81
76
  return nil
@@ -90,50 +85,44 @@ module JsonDoc
90
85
  end
91
86
  end
92
87
 
93
- def getPropSingle(yKey=nil)
94
- if yKey.nil?
95
- raise ArgumentError, 'E_BAD_KEY__IS_NIL'
96
- end
97
- yKey = yKey.to_sym if yKey.kind_of?(String)
88
+ def getPropSingle(yKey = nil)
89
+ raise ArgumentError, 'E_BAD_KEY__IS_NIL' if yKey.nil?
90
+ yKey = yKey.to_sym if yKey.is_a?(String)
98
91
  xxVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
99
92
  if xxVal.nil? && @bIsStrict
100
93
  self.validateKey(yKey)
101
94
  end
102
- return xxVal
95
+ xxVal
103
96
  end
104
97
 
105
- def validateKey(yKey=nil)
106
- if yKey.nil?
107
- raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]"
108
- end
98
+ def validateKey(yKey = nil)
99
+ raise ArgumentError, "E_BAD_KEY__IS_NIL [#{yKey.to_s}]" if yKey.nil?
109
100
 
110
101
  return true unless @bIsStrict
111
102
 
112
- bKeyExists = @dSchema.key?(:properties) && @dSchema[:properties].key?(yKey) ? true : false
103
+ bKeyExists = @dSchema.key?(:properties) \
104
+ && @dSchema[:properties].key?(yKey) ? true : false
113
105
 
114
- unless bKeyExists
115
- raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}"
116
- end
106
+ raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}" unless bKeyExists
117
107
 
118
108
  return true
119
109
  end
120
110
 
121
- def setProp(yKey=nil,xxVal=nil)
122
- yKey = yKey.to_sym if yKey.kind_of?(String)
111
+ def setProp(yKey = nil, xxVal = nil)
112
+ yKey = yKey.to_sym if yKey.is_a?(String)
123
113
 
124
114
  self.validateKey(yKey)
125
115
 
126
116
  @dDocument[yKey] = xxVal
127
117
  end
128
118
 
129
- def pushProp(yKey=nil,xxVal=nil)
130
- yKey = yKey.to_sym if yKey.kind_of?(String)
131
-
119
+ def pushProp(yKey = nil, xxVal = nil)
120
+ yKey = yKey.to_sym if yKey.is_a?(String)
132
121
  self.validateKey(yKey)
133
122
 
134
123
  if @dDocument.key?(yKey)
135
- if @dDocument[yKey].kind_of?(Array)
136
- @dDocument[yKey].push(xxVal)
124
+ if @dDocument[yKey].is_a?(Array)
125
+ @dDocument[yKey].push xxVal
137
126
  else
138
127
  raise RuntimeError, 'E_PROPERTY_IS_NOT_ARRAY'
139
128
  end
@@ -142,43 +131,42 @@ module JsonDoc
142
131
  end
143
132
  end
144
133
 
145
- def cpProp(yKeySrc=nil,yKeyDest=nil)
146
- yKeySrc = yKeySrc.to_sym if yKeySrc.kind_of?(String)
147
- yKeyDest = yKeyDest.to_sym if yKeyDest.kind_of?(String)
134
+ def cpProp(yKeySrc = nil, yKeyDest = nil)
135
+ yKeySrc = yKeySrc.to_sym if yKeySrc.is_a?(String)
136
+ yKeyDest = yKeyDest.to_sym if yKeyDest.is_a?(String)
148
137
  self.setAttr(yKeyDest, self.getAttr(yKeySrc))
149
138
  end
150
139
 
151
- def sortKeys()
140
+ def sortKeys
152
141
  @dDocument.keys.sort!
153
142
  end
154
143
 
155
- def fromJson(jDocument=nil)
156
- if jDocument.kind_of?(String)
144
+ def fromJson(jDocument = nil)
145
+ if jDocument.is_a?(String)
157
146
  @dDocument = JSON.load(jDocument)
158
147
  end
159
148
  return self
160
149
  end
161
150
 
162
- def fromDict(dDocument=nil)
151
+ def fromDict(dDocument = nil)
163
152
  @dDocument = dDocument if dDocument.is_a?(Hash)
164
153
  end
165
154
 
166
- def asHash()
167
- return @dDocument
155
+ def asHash
156
+ @dDocument
168
157
  end
169
158
 
170
- def asJson()
171
- return JSON.dump( self.asHash() )
159
+ def asJson
160
+ JSON.dump( self.asHash() )
172
161
  end
173
162
 
174
- def getValStringForProperties(aCols=nil,sDelimiter="\t")
175
- sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
163
+ def getValStringForProperties(aCols = nil, sDelimiter = "\t")
164
+ sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0
176
165
  aVals = self.getValArrayForProperties(aCols)
177
- sVals = aVals.join(sDelimiter)
178
- return sVals
166
+ return aVals.join(sDelimiter)
179
167
  end
180
168
 
181
- def getValArrayForProperties(aCols=nil,xxNil='')
169
+ def getValArrayForProperties(aCols = nil, xxNil = '')
182
170
  aVals = []
183
171
  return aVals if aCols.nil?
184
172
 
@@ -187,30 +175,26 @@ module JsonDoc
187
175
  end
188
176
 
189
177
  aCols.each do |yKey|
190
-
191
- yKey = yKey.to_sym if yKey.kind_of?(String)
178
+ yKey = yKey.to_sym if yKey.is_a? String
192
179
  xxVal = getProp( yKey )
193
180
  #xVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
194
181
  xxVal = xxNil if xxVal.nil?
195
- aVals.push( xxVal )
196
-
182
+ aVals.push xxVal
197
183
  end
198
- return aVals
184
+ aVals
199
185
  end
200
186
 
201
- def getDescStringForProperties(aCols=nil,sDelimiter="\t")
202
- sDelimiter = "\t" unless sDelimiter.kind_of?(String) && sDelimiter.length>0
187
+ def getDescStringForProperties(aCols = nil,sDelimiter = "\t")
188
+ sDelimiter = "\t" unless sDelimiter.is_a?(String) && sDelimiter.length>0
203
189
  aVals = self.getDescArrayForProperties(aCols)
204
- sVals = aVals.join(sDelimiter)
205
- return sVals
190
+ aVals.join(sDelimiter)
206
191
  end
207
192
 
208
- def getDescArrayForProperties(aCols=nil)
193
+ def getDescArrayForProperties(aCols = nil)
209
194
  aVals = []
210
195
  return aVals if aCols.nil?
211
196
  aCols.each do |yKey|
212
-
213
- yKey = yKey.to_sym if yKey.kind_of?(String)
197
+ yKey = yKey.to_sym if yKey.is_a? String
214
198
  xxVal = (
215
199
  @dSchema.key?(:properties) \
216
200
  && @dSchema[:properties].key?(yKey) \
@@ -219,11 +203,11 @@ module JsonDoc
219
203
  ) \
220
204
  ? @dSchema[:properties][yKey][:description] : yKey.to_s
221
205
 
222
- xxVal = xxVal.to_s unless xxVal.is_a?(String)
206
+ xxVal = xxVal.to_s unless xxVal.is_a? String
223
207
 
224
- aVals.push( xxVal )
208
+ aVals.push xxVal
225
209
  end
226
- return aVals
210
+ aVals
227
211
  end
228
212
 
229
213
  alias_method :setAttr , :setProp
@@ -231,4 +215,4 @@ module JsonDoc
231
215
  alias_method :pushAttr, :pushProp
232
216
  alias_method :cpAttr , :cpProp
233
217
  end
234
- end
218
+ end
@@ -4,4 +4,4 @@ Coveralls.wear!
4
4
  require 'test/unit'
5
5
  require 'jsondoc'
6
6
 
7
- doc = JsonDoc::Document.new({},{},true,true)
7
+ JsonDoc::Document.new({},{},true,true)
@@ -3,13 +3,13 @@ require './test/test_base.rb'
3
3
  class JsonDocTest < Test::Unit::TestCase
4
4
  def testSetup
5
5
 
6
- schemaTest = {
7
- :type => 'TestDocument',
8
- :properties => {
9
- :id => { :default => 'abc', :type => 'string', :description => 'Id' },
10
- :foo => { :default => 'bar', :type => 'string', :description => 'Foo' },
11
- :hello => { :default => 'world', :type => 'string', :description => 'Hello' },
12
- :array => { :default => ['foo'], :type => 'array', :description => 'Array' }
6
+ schemaTest = {
7
+ type: 'TestDocument',
8
+ properties: {
9
+ id: { default: 'abc', type: 'string', description: 'Id' },
10
+ foo: { default: 'bar', type: 'string', description: 'Foo' },
11
+ hello: { default: 'world', type: 'string', description: 'Hello' },
12
+ array: { default: ['foo'], type: 'array', description: 'Array' }
13
13
  }
14
14
  }
15
15
 
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.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Wang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-05 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A base document object
14
14
  email: johncwang@gmail.com
@@ -45,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
45
  version: '0'
46
46
  requirements: []
47
47
  rubyforge_project:
48
- rubygems_version: 2.4.8
48
+ rubygems_version: 2.5.1
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: JsonDoc