lazy_doc 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjE5Nzc1Y2ZiNTYzN2Q3ZGZmZjJlYTMxYzdlOGJkMGNjYTUwZDgzMw==
4
+ NGFlOWEyOWNhZWExN2I2ZmI4MGEwZTFjZDcxZDdhYjM4ZWRlNzVkMg==
5
5
  data.tar.gz: !binary |-
6
- MzczZTY4YTI0N2Y3OGU0YjYwMTQwYWM1MjkwMzczMzg0MWZlYTYyYg==
6
+ ZDg0OWU4OTNjNDQ3NDk2N2VjNTY2YzkxYjkwMmQ3ZGRiY2MwMGNiMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjI3NTY4MmU5MmY5OThjMzNhZDc2OGM2OTU2NTViYmYwNDFiMTkyMDgxZDZk
10
- NDY1ZjIyZTlmMTVkYmNhYzkxMzBlZWRlNWU5MjRjYWQ0MWEwNDFkOTMyNGM5
11
- ODhmMDg2ZTRmN2RiNmQ2MzcxMDNhNzMxNGY0MGNhZTc3ZDRhYTk=
9
+ M2ZjZDY4ZGVlNjczOTA4NzU0MDFkZTQwY2M0M2I3OTVkZGZhMTZmNGJiNzVh
10
+ N2ZiZmI5ZmY0YTViMDcxMjI4YTMzYzIwNTUxNDkzODhkZjA0MGFkNjYyYjdi
11
+ OTY2YTI2NDdhNmM3MDk0MDdiMTc3YTE2NjhkY2EyODFkZTlmM2I=
12
12
  data.tar.gz: !binary |-
13
- YWU5MmEyNTA3N2M2MTA3MGI1NDk5NjBiZTMyYzRkYTEwYmFlNDM1NTUwNGU5
14
- NjUzZjBiNjI1ODA0MDIyMmQ5YzE3OTZlYTI2YzFmNGViNWMyNWQzZDI3YzJm
15
- MjNhOWRlY2ZkOGE0NzU5MjI1ZWU0NWMwODU1N2FjMGE0MTNjZDk=
13
+ MWU3NzkxMzViMTllNmFlM2I1YzM3MDIwMDgyZTU2OTVlNWQxZjMwZmJkZjcx
14
+ YWEwNTBlMTI5MjVkMTM1OTA2NTVjMTExYWJjMjRkNDFhYmQ1OGE3ZTE2Njc4
15
+ YWZjZjk4MTYxMWM3M2JmMDRlMWUzZjlkNDNiNzViOWFmMTk0NWE=
data/README.md CHANGED
@@ -55,6 +55,28 @@ puts user.address
55
55
  puts user.job
56
56
  ```
57
57
 
58
+ ## Collections
59
+
60
+ When the document has a top-level array, use the `LazyDoc::Collection` class to transform it into an array of LazyDoc objects.
61
+
62
+ usage:
63
+ ```ruby
64
+ class User
65
+ include LazyDoc::DSL
66
+
67
+ access :name
68
+
69
+ # more...
70
+ end
71
+
72
+ json = '[{"name": "Brian"}, {"name": "Chris"}]'
73
+ users = LazyDoc::Collection.build(json, User)
74
+
75
+ users.each do |user|
76
+ puts user.name
77
+ end
78
+ ```
79
+
58
80
  ## Contributing
59
81
 
60
82
  1. Fork it
@@ -1,6 +1,8 @@
1
1
  require 'lazy_doc/attribute_not_found_error'
2
2
  require 'lazy_doc/camelizable_string'
3
+ require 'lazy_doc/parse_if_necessary'
3
4
  require 'lazy_doc/commands'
4
- require 'lazy_doc/dsl'
5
+ require 'lazy_doc/collection'
5
6
  require 'lazy_doc/memoizer'
7
+ require 'lazy_doc/dsl'
6
8
  require 'lazy_doc/version'
@@ -0,0 +1,12 @@
1
+ module LazyDoc
2
+ class Collection
3
+ extend ParseIfNecessary
4
+
5
+ def self.build(document, element_class)
6
+ as_class_command = Commands::AsClassCommand.new(element_class)
7
+ collection = as_class_command.execute(parse_if_necessary(document))
8
+
9
+ [collection].flatten
10
+ end
11
+ end
12
+ end
@@ -11,9 +11,9 @@ module LazyDoc::Commands
11
11
  value
12
12
  else
13
13
  if value.is_a? Array
14
- value.map { |element| klass.new(element.to_json) }
14
+ value.map { |element| klass.new(element) }
15
15
  else
16
- klass.new(value.to_json)
16
+ klass.new(value)
17
17
  end
18
18
  end
19
19
 
@@ -1,23 +1,23 @@
1
- require 'json'
2
-
3
1
  module LazyDoc
4
2
  module DSL
3
+ include ParseIfNecessary
4
+
5
5
  def self.included(base)
6
6
  base.extend ClassMethods
7
7
  end
8
8
 
9
- def lazily_embed(json)
10
- @_embedded_doc_source = json
9
+ def lazily_embed(document)
10
+ @embedded_doc_source = document
11
11
  end
12
12
 
13
13
  private
14
14
 
15
15
  def memoizer
16
- @_memoizer ||= Memoizer.new
16
+ @memoizer ||= Memoizer.new
17
17
  end
18
18
 
19
19
  def embedded_doc
20
- @_embedded_doc ||= JSON.parse(@_embedded_doc_source)
20
+ @embedded_doc ||= parse_if_necessary(@embedded_doc_source)
21
21
  end
22
22
 
23
23
 
@@ -0,0 +1,15 @@
1
+ require 'json'
2
+
3
+ module LazyDoc
4
+ module ParseIfNecessary
5
+ def parse_if_necessary(document)
6
+ already_parsed?(document) ? document : JSON.parse(document)
7
+ end
8
+
9
+ private
10
+
11
+ def already_parsed?(document)
12
+ document.is_a?(Hash) || document.is_a?(Array)
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module LazyDoc
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -0,0 +1,38 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe 'behavior when the JSON is an array' do
4
+ class LotrCharacter
5
+ include LazyDoc::DSL
6
+
7
+ access :name, :race
8
+
9
+ def initialize(json)
10
+ lazily_embed(json)
11
+ end
12
+ end
13
+
14
+ let(:json_file) { File.read(File.join(File.dirname(__FILE__), 'support/lotr_characters.json')) }
15
+
16
+ subject(:users) { LazyDoc::Collection.build(json_file, LotrCharacter) }
17
+
18
+ context 'frodo' do
19
+ let(:frodo) { users[0] }
20
+
21
+ specify { expect(frodo.name).to eq('Frodo Baggins') }
22
+ specify { expect(frodo.race).to eq('Hobbit') }
23
+ end
24
+
25
+ context 'sam' do
26
+ let(:sam) { users[1] }
27
+
28
+ specify { expect(sam.name).to eq('Samwise Gamgee') }
29
+ specify { expect(sam.race).to eq('Hobbit') }
30
+ end
31
+
32
+ context 'gandolf' do
33
+ let(:gandolf) { users[2] }
34
+
35
+ specify { expect(gandolf.name).to eq('Gandolf the Grey') }
36
+ specify { expect(gandolf.race).to eq('Wizard') }
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ [
2
+ {
3
+ "name": "Frodo Baggins",
4
+ "race": "Hobbit"
5
+ },
6
+ {
7
+ "name": "Samwise Gamgee",
8
+ "race": "Hobbit"
9
+ },
10
+ {
11
+ "name": "Gandolf the Grey",
12
+ "race": "Wizard"
13
+ }
14
+ ]
@@ -0,0 +1,34 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ module LazyDoc
4
+ describe Collection do
5
+ class FooBar
6
+ include LazyDoc::DSL
7
+
8
+ access :foo
9
+
10
+ def initialize(json)
11
+ lazily_embed(json)
12
+ end
13
+ end
14
+
15
+ let(:json) { '[{"foo": "bar"}, {"foo": "blarg"}, {"foo": "wibble"}]' }
16
+
17
+ let(:collection) { LazyDoc::Collection.build(json, FooBar) }
18
+
19
+ it 'is an Enumerable' do
20
+ collection.each do |foo_bar|
21
+ expect(foo_bar).to be_a FooBar
22
+ end
23
+ end
24
+
25
+ context 'when the expected document collection is actually a single element' do
26
+ let(:json) { '{"foo": "bar"}' }
27
+
28
+ specify { expect(collection).to have(1).element }
29
+ specify { expect(collection.first).to be_a FooBar }
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -30,10 +30,10 @@ module LazyDoc
30
30
  expect(as_class_value).to eq(value)
31
31
  end
32
32
 
33
- it 'passes the value as json to the specified class' do
33
+ it 'passes the value to the specified class' do
34
34
  as_class_command = Commands::AsClassCommand.new(Foo)
35
35
 
36
- Foo.should_receive(:new).with("{\"foo\":\"bar\"}")
36
+ Foo.should_receive(:new).with(value)
37
37
 
38
38
  as_class_command.execute(value)
39
39
  end
@@ -49,6 +49,14 @@ module LazyDoc
49
49
  expect { test_find.singleton_class.access :foo, :blarg, as: Foo}.to raise_error(ArgumentError, 'Options provided for multiple attributes')
50
50
  end
51
51
 
52
+ it 'can access properties when the supplied document is already a hash' do
53
+ test_find.singleton_class.access :foo, :blarg
54
+ test_find.lazily_embed(JSON.parse(json))
55
+
56
+ expect(test_find.foo).to eq("bar")
57
+ expect(test_find.blarg).to eq("wibble")
58
+ end
59
+
52
60
  context 'via' do
53
61
  it 'defines a method that accesses a named json attribute' do
54
62
  test_find.singleton_class.access :my_foo, via: :foo
@@ -0,0 +1,38 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ module LazyDoc
4
+ describe ParseIfNecessary do
5
+ class TestParseIfNecessary
6
+ include ParseIfNecessary
7
+ end
8
+
9
+ let(:test_parser) { TestParseIfNecessary.new }
10
+
11
+ it 'parses a JSON string' do
12
+ hash = test_parser.parse_if_necessary('{"foo":"bar"}')
13
+
14
+ expect(hash).to be_a Hash
15
+ expect(hash['foo']).to eq('bar')
16
+ end
17
+
18
+ it 'does not parse a Hash' do
19
+ JSON.should_not_receive(:parse)
20
+
21
+ hash = test_parser.parse_if_necessary({foo: 'bar'})
22
+
23
+ expect(hash).to be_a Hash
24
+ expect(hash[:foo]).to eq('bar')
25
+ end
26
+
27
+ it 'does not parse an Array' do
28
+ JSON.should_not_receive(:parse)
29
+
30
+ array = test_parser.parse_if_necessary([{foo: 'bar'}, {foo: 'bang'}])
31
+
32
+ expect(array).to be_an Array
33
+ expect(array[0]).to eq({foo: 'bar'})
34
+ expect(array[1]).to eq({foo: 'bang'})
35
+ end
36
+
37
+ end
38
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_doc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Oglesby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-20 00:00:00.000000000 Z
11
+ date: 2013-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -59,6 +59,7 @@ files:
59
59
  - lib/lazy_doc.rb
60
60
  - lib/lazy_doc/attribute_not_found_error.rb
61
61
  - lib/lazy_doc/camelizable_string.rb
62
+ - lib/lazy_doc/collection.rb
62
63
  - lib/lazy_doc/commands.rb
63
64
  - lib/lazy_doc/commands/as_class_command.rb
64
65
  - lib/lazy_doc/commands/default_value_command.rb
@@ -67,10 +68,14 @@ files:
67
68
  - lib/lazy_doc/commands/via_command.rb
68
69
  - lib/lazy_doc/dsl.rb
69
70
  - lib/lazy_doc/memoizer.rb
71
+ - lib/lazy_doc/parse_if_necessary.rb
70
72
  - lib/lazy_doc/version.rb
71
73
  - spec/acceptance/basic_behavior_spec.rb
74
+ - spec/acceptance/collections_spec.rb
75
+ - spec/acceptance/support/lotr_characters.json
72
76
  - spec/acceptance/support/user.json
73
77
  - spec/lib/lazy_doc/camelizable_string_spec.rb
78
+ - spec/lib/lazy_doc/collection_spec.rb
74
79
  - spec/lib/lazy_doc/commands/as_class_command_spec.rb
75
80
  - spec/lib/lazy_doc/commands/default_value_command_spec.rb
76
81
  - spec/lib/lazy_doc/commands/extract_command_spec.rb
@@ -79,6 +84,7 @@ files:
79
84
  - spec/lib/lazy_doc/commands_spec.rb
80
85
  - spec/lib/lazy_doc/dsl_spec.rb
81
86
  - spec/lib/lazy_doc/memoizer_spec.rb
87
+ - spec/lib/lazy_doc/parse_if_necessary_spec.rb
82
88
  - spec/spec_helper.rb
83
89
  - spec/support/shared_examples.rb
84
90
  homepage: https://github.com/ryanoglesby08/lazy-doc
@@ -107,8 +113,11 @@ specification_version: 4
107
113
  summary: An implementation of the Embedded Document pattern for POROs
108
114
  test_files:
109
115
  - spec/acceptance/basic_behavior_spec.rb
116
+ - spec/acceptance/collections_spec.rb
117
+ - spec/acceptance/support/lotr_characters.json
110
118
  - spec/acceptance/support/user.json
111
119
  - spec/lib/lazy_doc/camelizable_string_spec.rb
120
+ - spec/lib/lazy_doc/collection_spec.rb
112
121
  - spec/lib/lazy_doc/commands/as_class_command_spec.rb
113
122
  - spec/lib/lazy_doc/commands/default_value_command_spec.rb
114
123
  - spec/lib/lazy_doc/commands/extract_command_spec.rb
@@ -117,5 +126,6 @@ test_files:
117
126
  - spec/lib/lazy_doc/commands_spec.rb
118
127
  - spec/lib/lazy_doc/dsl_spec.rb
119
128
  - spec/lib/lazy_doc/memoizer_spec.rb
129
+ - spec/lib/lazy_doc/parse_if_necessary_spec.rb
120
130
  - spec/spec_helper.rb
121
131
  - spec/support/shared_examples.rb