jsondoc 0.1.0 → 0.1.1

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: d9ae715ba08f04872418899fac3fa5d7b3a23b36
4
- data.tar.gz: 6ef1bef9564fb2a12691cd6c9505371b340d8f8b
3
+ metadata.gz: 83ebca74e6671c4cdfeba514cd26772c8d881486
4
+ data.tar.gz: 28a88d4a291196859188f5625bf0808b28d1046c
5
5
  SHA512:
6
- metadata.gz: 979581a45e0904ceb0470926aa791a13ae4d0f35c92d1f4031e8219d7f46f2fe207b07b99dcff2fbb21ed7cc5a7342d2a6ca8da13d64df43d50b930eaf5f3789
7
- data.tar.gz: 2f3e6c4c0a8d8bfee3f5d25264b91a6b8be8a7263472fa583bf622f24f116d76901256b60254ca9eab1baa5fd8c93af1ecfe33c130152b7c24dd6c865c96bfd6
6
+ metadata.gz: c38b03a3c8882a8676d8f9d54f7609d26aa314d27ca36cd1a59b55217d5d52c0ad2479140e0df3b36a4e437ee95469d45a9fcfaeb0086d9a462b80c32f477a0c
7
+ data.tar.gz: 9c157d451a958e6beec2e835c416869e9cba7c34f28515ed004ad4555ebd54a33901117bba67f59249797eb434e1b938d832b22d8e496ed16096f0f841c86fbc
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 John Wang
1
+ Copyright (c) 2014-2016 John Wang
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -2,19 +2,29 @@ JsonDoc
2
2
  =======
3
3
 
4
4
  [![Gem Version][gem-version-svg]][gem-version-link]
5
+ [![Build Status][build-status-svg]][build-status-link]
6
+ [![Coverage Status][coverage-status-svg]][coverage-status-link]
5
7
  [![Dependency Status][dependency-status-svg]][dependency-status-link]
8
+ [![Code Climate][codeclimate-status-svg]][codeclimate-status-link]
6
9
  [![Scrutinizer Code Quality][scrutinizer-status-svg]][scrutinizer-status-link]
7
10
  [![Downloads][downloads-svg]][downloads-link]
8
11
  [![Docs][docs-rubydoc-svg]][docs-rubydoc-link]
9
12
  [![License][license-svg]][license-link]
10
13
 
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.
14
+ 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
15
 
13
16
  ## Installation
14
17
 
15
- ### Gem Installation
18
+ ### Via Bundler
16
19
 
17
- Download and install jsondoc with the following:
20
+ Add `jsondoc` to `Gemfile` and then run `bundle`:
21
+
22
+ ```sh
23
+ $ echo "gem 'jsondoc'" >> Gemfile
24
+ $ bundle
25
+ ```
26
+
27
+ ### Via RubyGems
18
28
 
19
29
  ```sh
20
30
  $ gem install jsondoc
@@ -25,20 +35,22 @@ $ gem install jsondoc
25
35
  ```ruby
26
36
  require 'jsondoc'
27
37
 
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 => [] }
38
+ my_data = {}
39
+
40
+ my_schema = {
41
+ type: 'My Document Type',
42
+ properties: {
43
+ first_name: {type: 'string', description: 'First Name', default: ''},
44
+ last_name: {type: 'string', description: 'Last Name', default: ''},
45
+ email_addresses: {type: 'array' , description: 'Email Addresses', default: []}
34
46
  }
35
47
  }
36
48
 
37
- thisUser = JsonDoc::Document.new( mySchema )
38
- thisUser.setAttr( :first_name, 'John' )
39
- thisUser.setAttr( :last_name, 'Doe' )
49
+ thisUser = JsonDoc::Document.new(my_data, my_schema)
50
+ thisUser.setAttr(:first_name, 'John')
51
+ thisUser.setAttr(:last_name, 'Doe')
40
52
 
41
- first_name = thisUser.getAttr( :first_name )
53
+ first_name = thisUser.getAttr(:first_name)
42
54
 
43
55
  thisUserHash = thisUser.asHash
44
56
  thisUserJson = thisUser.asJson
@@ -46,10 +58,10 @@ thisUserJson = thisUser.asJson
46
58
  descs = thisUser.getDescArrayForProperties( [:first_name,:last_name] )
47
59
  values = thisUser.getValArrayForProperties( [:first_name,:last_name] )
48
60
 
49
- thisUser.pushAttr( :email_addresses, 'john@example.com' )
50
- thisUser.pushAttr( :email_addresses, 'john.doe@example.com' )
61
+ thisUser.pushAttr(:email_addresses, 'john@example.com')
62
+ thisUser.pushAttr(:email_addresses, 'john.doe@example.com')
51
63
 
52
- thisUser.cpAttr( :first_name, :last_name )
64
+ thisUser.cpAttr(:first_name, :last_name)
53
65
  ```
54
66
 
55
67
  ## Notes
@@ -65,34 +77,27 @@ Schema validation is not provided in this version.
65
77
  2. JSON Schema
66
78
  * http://json-schema.org/
67
79
 
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
80
  ## License
77
81
 
78
- JsonDoc is available under an MIT-style license.
79
-
80
- :include: MIT-LICENSE
82
+ JsonDoc is available under an MIT-style license. See [LICENSE.txt](LICENSE.txt) for details.
81
83
 
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.
84
+ JsonDoc © 2014-2016 by John Wang
85
85
 
86
86
  [gem-version-svg]: https://badge.fury.io/rb/jsondoc.svg
87
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
88
+ [build-status-svg]: https://api.travis-ci.org/grokify/jsondoc-ruby.svg?branch=master
89
+ [build-status-link]: https://travis-ci.org/grokify/jsondoc-ruby
90
+ [coverage-status-svg]: https://coveralls.io/repos/grokify/jsondoc-ruby/badge.svg?branch=master
91
+ [coverage-status-link]: https://coveralls.io/r/grokify/jsondoc-ruby?branch=master
90
92
  [dependency-status-svg]: https://gemnasium.com/grokify/jsondoc-ruby.svg
91
93
  [dependency-status-link]: https://gemnasium.com/grokify/jsondoc-ruby
94
+ [codeclimate-status-svg]: https://codeclimate.com/github/grokify/jsondoc-ruby/badges/gpa.svg
95
+ [codeclimate-status-link]: https://codeclimate.com/github/grokify/jsondoc-ruby
92
96
  [scrutinizer-status-svg]: https://scrutinizer-ci.com/g/grokify/jsondoc-ruby/badges/quality-score.png?b=master
93
97
  [scrutinizer-status-link]: https://scrutinizer-ci.com/g/grokify/jsondoc-ruby/?branch=master
98
+ [downloads-svg]: http://ruby-gem-downloads-badge.herokuapp.com/jsondoc
99
+ [downloads-link]: https://rubygems.org/gems/jsondoc
94
100
  [docs-rubydoc-svg]: https://img.shields.io/badge/docs-rubydoc-blue.svg
95
101
  [docs-rubydoc-link]: http://www.rubydoc.info/gems/jsondoc/
96
102
  [license-svg]: https://img.shields.io/badge/license-MIT-blue.svg
97
103
  [license-link]: https://github.com/grokify/jsondoc-ruby/blob/master/LICENSE.txt
98
-
data/Rakefile CHANGED
@@ -1,22 +1,19 @@
1
1
  require 'rake'
2
2
  require 'rake/testtask'
3
- require 'rdoc/task'
4
3
 
5
4
  desc 'Default: run unit tests.'
6
5
  task :default => :test
7
6
 
8
- desc 'Test the JsonDoc library.'
7
+ desc 'Test the library.'
9
8
  Rake::TestTask.new do |t|
10
9
  t.libs << 'lib'
11
10
  t.pattern = 'test/**/test_*.rb'
12
11
  t.verbose = false
13
12
  end
14
13
 
15
- desc 'Generate RDoc documentation.'
16
- RDoc::Task.new do |rdoc|
17
- rdoc.rdoc_dir = 'rdoc'
18
- rdoc.title = 'jsondoc'
19
- rdoc.options << '--line-numbers' << '--inline-source'
20
- rdoc.rdoc_files.include('README.rdoc')
21
- rdoc.rdoc_files.include('lib/**/*.rb')
14
+ desc 'Generate YARD documentation.'
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"
22
19
  end
@@ -1,22 +1,26 @@
1
+ lib = 'jsondoc'
2
+ lib_file = File.expand_path("../lib/#{lib}.rb", __FILE__)
3
+ File.read(lib_file) =~ /\bVERSION\s*=\s*["'](.+?)["']/
4
+ version = $1
5
+
1
6
  Gem::Specification.new do |s|
2
7
  s.name = 'jsondoc'
3
- s.version = '0.1.0'
4
- s.date = '2014-08-14'
8
+ s.version = version
9
+ s.date = '2016-02-04'
5
10
  s.summary = 'JsonDoc'
6
11
  s.description = 'A base document object'
7
12
  s.authors = ['John Wang']
8
- s.email = 'john@johnwang.com'
13
+ s.email = 'johncwang@gmail.com'
14
+ s.homepage = 'https://github.com/grokify/'
15
+ s.licenses = ['MIT']
9
16
  s.files = [
10
17
  'CHANGELOG.md',
11
- 'LICENSE',
18
+ 'LICENSE.txt',
12
19
  'README.md',
13
20
  'Rakefile',
14
- 'VERSION',
15
21
  'jsondoc.gemspec',
16
22
  'lib/jsondoc.rb',
17
23
  'lib/jsondoc/document.rb',
18
24
  'test/test_setup.rb'
19
- ]
20
- s.homepage = 'http://johnwang.com/'
21
- s.license = 'MIT'
25
+ ]
22
26
  end
@@ -1,3 +1,5 @@
1
1
  module JsonDoc
2
+ VERSION = '0.1.1'
3
+
2
4
  autoload :Document, 'jsondoc/document'
3
5
  end
@@ -8,15 +8,18 @@ end
8
8
 
9
9
  module JsonDoc
10
10
  class Document
11
+ attr_reader :dDocument
11
12
 
12
13
  attr_accessor :bIsStrict
13
14
  attr_accessor :bUseKeyAsDesc
15
+ attr_accessor :bUseDeepKeys
14
16
 
15
- def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true)
17
+ def initialize(dValues=nil,dSchema=nil,bDefaultifyDoc=false,bIsStrict=true,opts={})
16
18
  @dSchema = dSchema || self.getDefaultSchema()
17
19
  @bDefaultifyDoc = bDefaultifyDoc ? true : false
18
20
  @bIsStrict = bIsStrict ? true : false
19
21
  @bUseKeyAsDesc = false
22
+ @bUseDeepKeys = opts.key?(:bUseDeepKeys) ? opts[:bUseDeepKeys] : true
20
23
  @dDocument = self.getDefaultDocument()
21
24
  self.loadHash(dValues) if dValues.is_a?(Hash)
22
25
  end
@@ -33,10 +36,10 @@ module JsonDoc
33
36
 
34
37
  def getDefaultDocument()
35
38
  dDocument = {}
36
- if @bDefaultifyDoc && @dSchema.has_key?(:properties)
39
+ if @bDefaultifyDoc && @dSchema.key?(:properties)
37
40
  @dSchema[:properties].keys.each do |yKey|
38
41
  dProperty = @dSchema[:properties][yKey]
39
- xxVal = dProperty.has_key?(:default) ? dProperty[:default] : ''
42
+ xxVal = dProperty.key?(:default) ? dProperty[:default] : ''
40
43
  dDocument[yKey] = xxVal
41
44
  end
42
45
  end
@@ -60,17 +63,21 @@ module JsonDoc
60
63
  raise ArgumentError, 'E_BAD_KEY__IS_NIL'
61
64
  end
62
65
  yKey = yKey.to_sym if yKey.kind_of?(String)
63
- aKeys = yKey.split('.')
64
- #aKeys = yKey.to_s.split('.').map(&:to_sym)
65
66
 
66
- dDoc = @dDocument
67
- xxVal = getPropRecurse(aKeys.clone,dDoc)
68
- return xxVal
67
+ if @bUseDeepKeys
68
+ aKeys = yKey.split('.')
69
+ #aKeys = yKey.to_s.split('.').map(&:to_sym)
70
+
71
+ dDoc = @dDocument
72
+ xxVal = getPropRecurse(aKeys.clone,dDoc)
73
+ return xxVal
74
+ end
75
+ return @dDocument.key?(yKey) ? @dDocument[yKey] : nil
69
76
  end
70
77
 
71
78
  def getPropRecurse(aKeys=[],dDoc=nil)
72
79
  yKey = aKeys.shift
73
- if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.has_key?( yKey )
80
+ if ! yKey.is_a?(Symbol) || yKey.length<1 || ! dDoc.key?( yKey )
74
81
  return nil
75
82
  end
76
83
  xxVal = dDoc[ yKey ]
@@ -88,7 +95,7 @@ module JsonDoc
88
95
  raise ArgumentError, 'E_BAD_KEY__IS_NIL'
89
96
  end
90
97
  yKey = yKey.to_sym if yKey.kind_of?(String)
91
- xxVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
98
+ xxVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
92
99
  if xxVal.nil? && @bIsStrict
93
100
  self.validateKey(yKey)
94
101
  end
@@ -102,7 +109,7 @@ module JsonDoc
102
109
 
103
110
  return true unless @bIsStrict
104
111
 
105
- bKeyExists = @dSchema.has_key?(:properties) && @dSchema[:properties].has_key?(yKey) ? true : false
112
+ bKeyExists = @dSchema.key?(:properties) && @dSchema[:properties].key?(yKey) ? true : false
106
113
 
107
114
  unless bKeyExists
108
115
  raise ArgumentError, "E_UNKNOWN_KEY__STRICT #{yKey.to_s}"
@@ -124,7 +131,7 @@ module JsonDoc
124
131
 
125
132
  self.validateKey(yKey)
126
133
 
127
- if @dDocument.has_key?(yKey)
134
+ if @dDocument.key?(yKey)
128
135
  if @dDocument[yKey].kind_of?(Array)
129
136
  @dDocument[yKey].push(xxVal)
130
137
  else
@@ -183,7 +190,7 @@ module JsonDoc
183
190
 
184
191
  yKey = yKey.to_sym if yKey.kind_of?(String)
185
192
  xxVal = getProp( yKey )
186
- #xVal = @dDocument.has_key?(yKey) ? @dDocument[yKey] : nil
193
+ #xVal = @dDocument.key?(yKey) ? @dDocument[yKey] : nil
187
194
  xxVal = xxNil if xxVal.nil?
188
195
  aVals.push( xxVal )
189
196
 
@@ -205,9 +212,9 @@ module JsonDoc
205
212
 
206
213
  yKey = yKey.to_sym if yKey.kind_of?(String)
207
214
  xxVal = (
208
- @dSchema.has_key?(:properties) \
209
- && @dSchema[:properties].has_key?(yKey) \
210
- && @dSchema[:properties][yKey].has_key?(:description) \
215
+ @dSchema.key?(:properties) \
216
+ && @dSchema[:properties].key?(yKey) \
217
+ && @dSchema[:properties][yKey].key?(:description) \
211
218
  && @dSchema[:properties][yKey][:description].length > 0
212
219
  ) \
213
220
  ? @dSchema[:properties][yKey][:description] : yKey.to_s
@@ -223,6 +230,5 @@ module JsonDoc
223
230
  alias_method :getAttr , :getProp
224
231
  alias_method :pushAttr, :pushProp
225
232
  alias_method :cpAttr , :cpProp
226
-
227
233
  end
228
234
  end
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'jsondoc'
1
+ require './test/test_base.rb'
3
2
 
4
3
  class JsonDocTest < Test::Unit::TestCase
5
4
  def testSetup
@@ -14,7 +13,7 @@ class JsonDocTest < Test::Unit::TestCase
14
13
  }
15
14
  }
16
15
 
17
- jDoc = JsonDoc::Document.new(schemaTest,true,true)
16
+ jDoc = JsonDoc::Document.new({},schemaTest,true,true)
18
17
 
19
18
  assert_equal 'abc' , jDoc.getAttr(:id)
20
19
  assert_equal "abc\tbar" , jDoc.getValStringForProperties([:id,:foo])
@@ -41,4 +40,22 @@ class JsonDocTest < Test::Unit::TestCase
41
40
  assert_equal jDoc.getAttr(:id), jDoc.getAttr(:foo)
42
41
 
43
42
  end
44
- end
43
+
44
+ def testNested()
45
+ data = {
46
+ foo: {
47
+ bar: { baz: 10 }
48
+ }
49
+ }
50
+ doc = JsonDoc::Document.new(data,{},false,false)
51
+ assert_equal 10, doc.getAttr('foo.bar.baz')
52
+ end
53
+
54
+ def testNotNested()
55
+ doc2 = JsonDoc::Document.new({},{},false,false)
56
+ doc2.bUseDeepKeys = false
57
+ doc2.setAttr('foo.bar.baz', 'deadbeef')
58
+
59
+ assert_equal 'deadbeef', doc2.dDocument[:'foo.bar.baz']
60
+ end
61
+ end
metadata CHANGED
@@ -1,31 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsondoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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-08-14 00:00:00.000000000 Z
11
+ date: 2016-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A base document object
14
- email: john@johnwang.com
14
+ email: johncwang@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - CHANGELOG.md
20
- - LICENSE
20
+ - LICENSE.txt
21
21
  - README.md
22
22
  - Rakefile
23
- - VERSION
24
23
  - jsondoc.gemspec
25
24
  - lib/jsondoc.rb
26
25
  - lib/jsondoc/document.rb
27
26
  - test/test_setup.rb
28
- homepage: http://johnwang.com/
27
+ homepage: https://github.com/grokify/
29
28
  licenses:
30
29
  - MIT
31
30
  metadata: {}
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.0