embedded_document 0.0.2
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 +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +83 -0
- data/Rakefile +1 -0
- data/embedded_document.gemspec +26 -0
- data/lib/embedded_document.rb +27 -0
- data/lib/embedded_document/embedder_combinators.rb +11 -0
- data/lib/embedded_document/embedders.rb +11 -0
- data/lib/embedded_document/version.rb +3 -0
- data/spec/lib/embedded_document_spec.rb +26 -0
- data/spec/lib/embedder_spec.rb +51 -0
- data/spec/spec_helper.rb +4 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 99fa4b46c8c6f50abac6598a7814d9b0aee20d76
|
4
|
+
data.tar.gz: 6db4d87b169be3f96ce4a945d9889f89f4fddf5e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8153a45acabc2c411ebb27bbb3923e6c43c4c2bb82488d9d299877138605252c929a96dbdc48f3a2b5b359308551df9f4ef1900a7fabfbc76f3781cb4bafa2ab
|
7
|
+
data.tar.gz: 84b94e168be3b9291c586cf4ce3629e2ad60d159b3964f5dd13dd2561caed7fbdfaee9b2a5733151d41674cd64107e30d6885f44b64b4362442b027113d06f4b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Sumit Mahamuni And Rahul Phulore
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# EmbeddedDocument
|
2
|
+
Abstract away the smaller patterns that occur when using Embedded Document pattern.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
gem 'embedded_document'
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install embedded_document
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
```
|
21
|
+
class Item < EmbeddedDocument::Document
|
22
|
+
end
|
23
|
+
|
24
|
+
class Delivery < EmbeddedDocument::Document
|
25
|
+
key :shipDate, date, :ship_date
|
26
|
+
key :items, sequence_of(Item)
|
27
|
+
end
|
28
|
+
|
29
|
+
class Order < EmbeddedDocument::Document
|
30
|
+
key :deliveries, sequence_of(Delivery)
|
31
|
+
key :items, sequence_of(Item)
|
32
|
+
|
33
|
+
def quantity_for(a_product)
|
34
|
+
item = items.detect{|i| a_product == i.product}
|
35
|
+
item ? item.quantity : 0
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
order_hash = JSON.parse(
|
40
|
+
'{ "id": 1234,
|
41
|
+
"customer": "martin",
|
42
|
+
"items": [
|
43
|
+
{"product": "talisker", "quantity": 500},
|
44
|
+
{"product": "macallan", "quantity": 800},
|
45
|
+
{"product": "ledaig", "quantity": 1100}
|
46
|
+
],
|
47
|
+
"deliveries": [
|
48
|
+
{ "id": 7722,
|
49
|
+
"shipDate": "2013-04-19",
|
50
|
+
"items": [
|
51
|
+
{"product": "talisker", "quantity": 300},
|
52
|
+
{"product": "ledaig", "quantity": 500}
|
53
|
+
]
|
54
|
+
},
|
55
|
+
{ "id": 6533,
|
56
|
+
"shipDate": "2013-04-18",
|
57
|
+
"items": [
|
58
|
+
{"product": "talisker", "quantity": 200},
|
59
|
+
{"product": "ledaig", "quantity": 300},
|
60
|
+
{"product": "macallan", "quantity": 300}
|
61
|
+
]
|
62
|
+
}
|
63
|
+
]
|
64
|
+
}'
|
65
|
+
)
|
66
|
+
|
67
|
+
order = Order.new(order_hash)
|
68
|
+
p order.deliveries
|
69
|
+
p order.quantity_for('talisker')
|
70
|
+
```
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it ( http://github.com/<my-github-username>/embedded_document/fork )
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create new Pull Request
|
79
|
+
|
80
|
+
## References
|
81
|
+
|
82
|
+
1. Embedded Document pattern: (http://martinfowler.com/bliki/EmbeddedDocument.html)
|
83
|
+
2. Easing the use of Embedded Document pattern: (http://missingfaktor.blogspot.in/2013/07/easing-use-of-embedded-document-pattern.html)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'embedded_document/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'embedded_document'
|
8
|
+
spec.version = EmbeddedDocument::VERSION
|
9
|
+
spec.authors = ['Sumit Mahamuni', 'Rahul Phulore']
|
10
|
+
spec.email = ['mahamunisumit@gmail.com']
|
11
|
+
spec.summary = %q{Convenient way of dealing with JSON documents in web apps}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 2.6'
|
24
|
+
spec.add_development_dependency 'pry'
|
25
|
+
spec.add_development_dependency 'pry-debugger'
|
26
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'embedded_document/version'
|
2
|
+
require 'embedded_document/embedders'
|
3
|
+
require 'embedded_document/embedder_combinators'
|
4
|
+
|
5
|
+
module EmbeddedDocument
|
6
|
+
class Document < Struct.new(:data)
|
7
|
+
extend Embedders
|
8
|
+
extend EmbedderCombinators
|
9
|
+
|
10
|
+
def self.key(name, embedder, accessor_name = nil)
|
11
|
+
name = name.to_s
|
12
|
+
accessor_name ||= name
|
13
|
+
define_method(accessor_name) do
|
14
|
+
embedder.call(self.data[name])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.call(value)
|
19
|
+
self.new(value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing name
|
23
|
+
return self.data[name.to_s] if self.data[name.to_s]
|
24
|
+
super name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module EmbeddedDocument
|
2
|
+
module EmbedderCombinators
|
3
|
+
def sequence_of embedder
|
4
|
+
lambda { |array| array.map { |x| embedder.call(x) } }
|
5
|
+
end
|
6
|
+
|
7
|
+
def defaulted embedder, default_value
|
8
|
+
lambda { |value| value.nil? ? default_value : embedder.call(value) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EmbeddedDocument::Document do
|
4
|
+
context '.key' do
|
5
|
+
class AnyEmbedder < EmbeddedDocument::Document
|
6
|
+
key :anykey, identity, :any_key
|
7
|
+
key :example_key, identity
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should define instance method with accessor_name' do
|
11
|
+
expect { AnyEmbedder.new({}).any_key }.not_to raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should define instance method key_name if accessor_name is not given' do
|
15
|
+
expect { AnyEmbedder.new({}).example_key }.not_to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should return value for undefined_key if data contains key' do
|
19
|
+
expect(AnyEmbedder.new({'undefined_key' => 'anything'}).undefined_key).to eq('anything')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should raise no method error if data key is missing' do
|
23
|
+
expect { AnyEmbedder.new({}).missing_key }.to raise_error(NoMethodError)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Embedder' do
|
4
|
+
class AnyEmbedder < EmbeddedDocument::Document
|
5
|
+
key :anyKey, identity, :any_key
|
6
|
+
end
|
7
|
+
|
8
|
+
class NestedEmbedderExample < EmbeddedDocument::Document
|
9
|
+
key :anyEmbedder, AnyEmbedder, :any_embedder
|
10
|
+
end
|
11
|
+
|
12
|
+
class NestedSequenceEmbedderExample < EmbeddedDocument::Document
|
13
|
+
key :anyEmbedders, sequence_of(AnyEmbedder), :any_embedders
|
14
|
+
end
|
15
|
+
|
16
|
+
class DefaultedEmbedderExample < EmbeddedDocument::Document
|
17
|
+
key :anyEmbedder, defaulted(identity, 0), :any_embedder
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return any_key value' do
|
21
|
+
subject = AnyEmbedder.new({'anyKey' => 'anything'})
|
22
|
+
|
23
|
+
expect(subject.any_key).to eq('anything')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should return any_embedder' do
|
27
|
+
subject = NestedEmbedderExample.new({'anyEmbedder' => {'anyKey' => 'anything'}})
|
28
|
+
|
29
|
+
expect(subject.any_embedder).to be_a AnyEmbedder
|
30
|
+
expect(subject.any_embedder.any_key).to eq('anything')
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should return sequence of any_embedder' do
|
34
|
+
subject = NestedSequenceEmbedderExample.new({'anyEmbedders' => [{'anyKey' => 'anything'}]})
|
35
|
+
|
36
|
+
expect(subject.any_embedders).to be_a Array
|
37
|
+
expect(subject.any_embedders.first).to be_a AnyEmbedder
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should return default value if key is nil' do
|
41
|
+
subject = DefaultedEmbedderExample.new({'anyEmbedder' => nil})
|
42
|
+
|
43
|
+
expect(subject.any_embedder).to eq(0)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should return value if key is not nil' do
|
47
|
+
subject = DefaultedEmbedderExample.new({'anyEmbedder' => 'anything'})
|
48
|
+
|
49
|
+
expect(subject.any_embedder).to eq('anything')
|
50
|
+
end
|
51
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: embedded_document
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sumit Mahamuni
|
8
|
+
- Rahul Phulore
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.5'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.5'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '2.6'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '2.6'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: pry
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: pry-debugger
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: ''
|
85
|
+
email:
|
86
|
+
- mahamunisumit@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- embedded_document.gemspec
|
97
|
+
- lib/embedded_document.rb
|
98
|
+
- lib/embedded_document/embedder_combinators.rb
|
99
|
+
- lib/embedded_document/embedders.rb
|
100
|
+
- lib/embedded_document/version.rb
|
101
|
+
- spec/lib/embedded_document_spec.rb
|
102
|
+
- spec/lib/embedder_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
homepage: ''
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.2.2
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Convenient way of dealing with JSON documents in web apps
|
128
|
+
test_files:
|
129
|
+
- spec/lib/embedded_document_spec.rb
|
130
|
+
- spec/lib/embedder_spec.rb
|
131
|
+
- spec/spec_helper.rb
|