jsondoc 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a67112a09bdcadf7b01a4da9f764afcc2b997718
4
- data.tar.gz: c16488cb845e829d9d40b1bce7ebc7ee136219dc
3
+ metadata.gz: d9ae715ba08f04872418899fac3fa5d7b3a23b36
4
+ data.tar.gz: 6ef1bef9564fb2a12691cd6c9505371b340d8f8b
5
5
  SHA512:
6
- metadata.gz: 8f81b7d01dd4ae7d0b9eb27913fcd726ef1ed09b1283673dccda60858a348e5caa60fdee3cb9a960f6cda8755f4f47246f31b84815d76399986b721111f2cb67
7
- data.tar.gz: 5a6fef198638b13bfedceb507f33459aed78353179763f928f171be7429358f8625b058d03a08d5ea11ca9ac6a6ca868d35569cd48b4b11af8647dcce64da8a2
6
+ metadata.gz: 979581a45e0904ceb0470926aa791a13ae4d0f35c92d1f4031e8219d7f46f2fe207b07b99dcff2fbb21ed7cc5a7342d2a6ca8da13d64df43d50b930eaf5f3789
7
+ data.tar.gz: 2f3e6c4c0a8d8bfee3f5d25264b91a6b8be8a7263472fa583bf622f24f116d76901256b60254ca9eab1baa5fd8c93af1ecfe33c130152b7c24dd6c865c96bfd6
@@ -1,5 +1,13 @@
1
1
  CHANGELOG
2
2
  ---------
3
+ - **2014-08-14**: 0.1.0
4
+ - Support retrieval of properties from nested objects using period delmiter
5
+
6
+ - **2014-06-05**: 0.0.6
7
+ - Return self from loadHash()
8
+
9
+ - **2014-04-17**: 0.0.5
10
+
3
11
  - **2014-03-16**: 0.0.4
4
12
  - Changed Attr methods to Prop methods to represent JSON schema property
5
13
  - Added Attr methods as aliases
@@ -0,0 +1,98 @@
1
+ JsonDoc
2
+ =======
3
+
4
+ [![Gem Version][gem-version-svg]][gem-version-link]
5
+ [![Dependency Status][dependency-status-svg]][dependency-status-link]
6
+ [![Scrutinizer Code Quality][scrutinizer-status-svg]][scrutinizer-status-link]
7
+ [![Downloads][downloads-svg]][downloads-link]
8
+ [![Docs][docs-rubydoc-svg]][docs-rubydoc-link]
9
+ [![License][license-svg]][license-link]
10
+
11
+ Generic JSON document base class to set/get document attributes based on JSON Schema, dump as JSON and support building of CSV and Excel workbooks. Subclasses can be built with additional functionality, e.g. using the setAttr method. Primary use cases include being used with parsers to create JSON documents and to create CSV/Excel reports.
12
+
13
+ ## Installation
14
+
15
+ ### Gem Installation
16
+
17
+ Download and install jsondoc with the following:
18
+
19
+ ```sh
20
+ $ gem install jsondoc
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'jsondoc'
27
+
28
+ mySchema = {
29
+ :type => 'My Document Type',
30
+ :properties => {
31
+ :first_name => { :type => 'string', :description => 'First Name', :default => '' },
32
+ :last_name => { :type => 'string', :description => 'Last Name', :default => '' },
33
+ :email_addresses => { :type => 'array' , :description => 'Email Addresses', :default => [] }
34
+ }
35
+ }
36
+
37
+ thisUser = JsonDoc::Document.new( mySchema )
38
+ thisUser.setAttr( :first_name, 'John' )
39
+ thisUser.setAttr( :last_name, 'Doe' )
40
+
41
+ first_name = thisUser.getAttr( :first_name )
42
+
43
+ thisUserHash = thisUser.asHash
44
+ thisUserJson = thisUser.asJson
45
+
46
+ descs = thisUser.getDescArrayForProperties( [:first_name,:last_name] )
47
+ values = thisUser.getValArrayForProperties( [:first_name,:last_name] )
48
+
49
+ thisUser.pushAttr( :email_addresses, 'john@example.com' )
50
+ thisUser.pushAttr( :email_addresses, 'john.doe@example.com' )
51
+
52
+ thisUser.cpAttr( :first_name, :last_name )
53
+ ```
54
+
55
+ ## Notes
56
+
57
+ ### Schema Validation
58
+
59
+ Schema validation is not provided in this version.
60
+
61
+ ## Links
62
+
63
+ 1. JSON
64
+ * http://www.json.org/
65
+ 2. JSON Schema
66
+ * http://json-schema.org/
67
+
68
+ ### Problems, Comments, Suggestions?
69
+
70
+ All of the above are most welcome. mailto:johncwang@gmail.com
71
+
72
+ ## Credits
73
+
74
+ John Wang - http://johnwang.com
75
+
76
+ ## License
77
+
78
+ JsonDoc is available under an MIT-style license.
79
+
80
+ :include: MIT-LICENSE
81
+
82
+ ## Warranty
83
+
84
+ This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.
85
+
86
+ [gem-version-svg]: https://badge.fury.io/rb/jsondoc.svg
87
+ [gem-version-link]: http://badge.fury.io/rb/jsondoc
88
+ [downloads-svg]: http://ruby-gem-downloads-badge.herokuapp.com/jsondoc
89
+ [downloads-link]: https://rubygems.org/gems/jsondoc
90
+ [dependency-status-svg]: https://gemnasium.com/grokify/jsondoc-ruby.svg
91
+ [dependency-status-link]: https://gemnasium.com/grokify/jsondoc-ruby
92
+ [scrutinizer-status-svg]: https://scrutinizer-ci.com/g/grokify/jsondoc-ruby/badges/quality-score.png?b=master
93
+ [scrutinizer-status-link]: https://scrutinizer-ci.com/g/grokify/jsondoc-ruby/?branch=master
94
+ [docs-rubydoc-svg]: https://img.shields.io/badge/docs-rubydoc-blue.svg
95
+ [docs-rubydoc-link]: http://www.rubydoc.info/gems/jsondoc/
96
+ [license-svg]: https://img.shields.io/badge/license-MIT-blue.svg
97
+ [license-link]: https://github.com/grokify/jsondoc-ruby/blob/master/LICENSE.txt
98
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.1.0
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jsondoc'
3
- s.version = '0.0.4'
4
- s.date = '2014-03-16'
3
+ s.version = '0.1.0'
4
+ s.date = '2014-08-14'
5
5
  s.summary = 'JsonDoc'
6
6
  s.description = 'A base document object'
7
7
  s.authors = ['John Wang']
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.files = [
10
10
  'CHANGELOG.md',
11
11
  'LICENSE',
12
- 'README.rdoc',
12
+ 'README.md',
13
13
  'Rakefile',
14
14
  'VERSION',
15
15
  'jsondoc.gemspec',
@@ -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
@@ -1,16 +1,24 @@
1
1
  require 'json'
2
2
 
3
+ class Symbol
4
+ def split(separator)
5
+ to_s.split(separator).map(&:to_sym)
6
+ end
7
+ end
8
+
3
9
  module JsonDoc
4
10
  class Document
5
11
 
6
12
  attr_accessor :bIsStrict
13
+ attr_accessor :bUseKeyAsDesc
7
14
 
8
15
  def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true)
9
16
  @dSchema = dSchema || self.getDefaultSchema()
10
17
  @bDefaultifyDoc = bDefaultifyDoc ? true : false
11
18
  @bIsStrict = bIsStrict ? true : false
19
+ @bUseKeyAsDesc = false
12
20
  @dDocument = self.getDefaultDocument()
13
- self.loadInitialValues(dValues)
21
+ self.loadHash(dValues) if dValues.is_a?(Hash)
14
22
  end
15
23
 
16
24
  def getDefaultSchema()
@@ -35,7 +43,7 @@ module JsonDoc
35
43
  return dDocument
36
44
  end
37
45
 
38
- def loadInitialValues(dValues=nil)
46
+ def loadHash(dValues=nil)
39
47
  if dValues.nil?
40
48
  return
41
49
  elsif ! dValues.is_a?(Hash)
@@ -44,9 +52,38 @@ module JsonDoc
44
52
  dValues.each do |yKey,xxVal|
45
53
  self.setProp(yKey,xxVal)
46
54
  end
55
+ return self
47
56
  end
48
57
 
49
58
  def getProp(yKey=nil)
59
+ if yKey.nil?
60
+ raise ArgumentError, 'E_BAD_KEY__IS_NIL'
61
+ end
62
+ yKey = yKey.to_sym if yKey.kind_of?(String)
63
+ aKeys = yKey.split('.')
64
+ #aKeys = yKey.to_s.split('.').map(&:to_sym)
65
+
66
+ dDoc = @dDocument
67
+ xxVal = getPropRecurse(aKeys.clone,dDoc)
68
+ return xxVal
69
+ end
70
+
71
+ def getPropRecurse(aKeys=[],dDoc=nil)
72
+ yKey = aKeys.shift
73
+ if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.has_key?( yKey )
74
+ return nil
75
+ end
76
+ xxVal = dDoc[ yKey ]
77
+ if aKeys.length == 0
78
+ return xxVal
79
+ elsif dDoc.is_a?(Hash)
80
+ return getPropRecurse( aKeys, xxVal )
81
+ else
82
+ raise ArgumentError, "E_BAD_VAL__IS_NOT_HASH"
83
+ end
84
+ end
85
+
86
+ def getPropSingle(yKey=nil)
50
87
  if yKey.nil?
51
88
  raise ArgumentError, 'E_BAD_KEY__IS_NIL'
52
89
  end
@@ -134,15 +171,22 @@ module JsonDoc
134
171
  return sVals
135
172
  end
136
173
 
137
- def getValArrayForProperties(aCols=nil)
174
+ def getValArrayForProperties(aCols=nil,xxNil='')
138
175
  aVals = []
139
176
  return aVals if aCols.nil?
177
+
178
+ if @bUseKeyAsDesc
179
+ asVals = aCols.map {|x| x.to_s }
180
+ end
181
+
140
182
  aCols.each do |yKey|
141
183
 
142
- yKey = yKey.to_sym if yKey.kind_of?(String)
143
- xVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
184
+ yKey = yKey.to_sym if yKey.kind_of?(String)
185
+ xxVal = getProp( yKey )
186
+ #xVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
187
+ xxVal = xxNil if xxVal.nil?
188
+ aVals.push( xxVal )
144
189
 
145
- aVals.push( xVal )
146
190
  end
147
191
  return aVals
148
192
  end
@@ -160,8 +204,15 @@ module JsonDoc
160
204
  aCols.each do |yKey|
161
205
 
162
206
  yKey = yKey.to_sym if yKey.kind_of?(String)
163
- xxVal = ( @dSchema[:properties].has_key?(yKey) && @dSchema[:properties][yKey].has_key?(:description) && @dSchema[:properties][yKey][:description].length > 0 ) \
164
- ? @dSchema[:properties][yKey][:description] : yKey.to_s
207
+ xxVal = (
208
+ @dSchema.has_key?(:properties) \
209
+ && @dSchema[:properties].has_key?(yKey) \
210
+ && @dSchema[:properties][yKey].has_key?(:description) \
211
+ && @dSchema[:properties][yKey][:description].length > 0
212
+ ) \
213
+ ? @dSchema[:properties][yKey][:description] : yKey.to_s
214
+
215
+ xxVal = xxVal.to_s unless xxVal.is_a?(String)
165
216
 
166
217
  aVals.push( xxVal )
167
218
  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.4
4
+ version: 0.1.0
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-03-16 00:00:00.000000000 Z
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A base document object
14
14
  email: john@johnwang.com
@@ -18,7 +18,7 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - CHANGELOG.md
20
20
  - LICENSE
21
- - README.rdoc
21
+ - README.md
22
22
  - Rakefile
23
23
  - VERSION
24
24
  - jsondoc.gemspec
@@ -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.2.1
48
+ rubygems_version: 2.4.8
49
49
  signing_key:
50
50
  specification_version: 4
51
51
  summary: JsonDoc
@@ -1,75 +0,0 @@
1
- = JsonDoc
2
-
3
- Generic JSON document base class to set/get document attributes based on JSON Schema, dump as JSON and support building of CSV and Excel workbooks. Subclasses can be built with additional functionality, e.g. using the setAttr method. Primary use cases include being used with parsers to create JSON documents and to create CSV/Excel reports.
4
-
5
- == Installation
6
-
7
- === Gem Installation
8
-
9
- Download and install jsondoc with the following:
10
-
11
- gem install jsondoc
12
-
13
- == Usage
14
-
15
- require 'jsondoc'
16
-
17
- mySchema = {
18
- :type => 'My Document Type',
19
- :properties => {
20
- :first_name => { :type => 'string', :description => 'First Name', :default => '' },
21
- :last_name => { :type => 'string', :description => 'Last Name', :default => '' },
22
- :email_addresses => { :type => 'array' , :description => 'Email Addresses', :default => [] }
23
- }
24
- }
25
-
26
- thisUser = JsonDoc::Document.new( mySchema )
27
- thisUser.setAttr( :first_name, 'John' )
28
- thisUser.setAttr( :last_name, 'Doe' )
29
-
30
- first_name = thisUser.getAttr( :first_name )
31
-
32
- thisUserHash = thisUser.asHash
33
- thisUserJson = thisUser.asJson
34
-
35
- descs = thisUser.getDescArrayForProperties( [:first_name,:last_name] )
36
- values = thisUser.getValArrayForProperties( [:first_name,:last_name] )
37
-
38
- thisUser.pushAttr( :email_addresses, 'john@example.com' )
39
- thisUser.pushAttr( :email_addresses, 'john.doe@example.com' )
40
-
41
- thisUser.cpAttr( :first_name, :last_name )
42
-
43
- == Notes
44
-
45
- === Schema Validation
46
-
47
- Schema validation is not provided in this version.
48
-
49
- == Links
50
-
51
- JSON
52
-
53
- http://www.json.org/
54
-
55
- JSON Schema
56
-
57
- http://json-schema.org/
58
-
59
- == Problems, Comments, Suggestions?
60
-
61
- All of the above are most welcome. mailto:johncwang@gmail.com
62
-
63
- == Credits
64
-
65
- John Wang - http://johnwang.com
66
-
67
- == License
68
-
69
- JsonDoc is available under an MIT-style license.
70
-
71
- :include: MIT-LICENSE
72
-
73
- == Warranty
74
-
75
- This software is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantibility and fitness for a particular purpose.