hash19 0.0.1
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 +15 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +5 -0
- data/hash19.gemspec +30 -0
- data/lib/hash19/core.rb +59 -0
- data/lib/hash19/core_helpers.rb +47 -0
- data/lib/hash19/lazy_value.rb +11 -0
- data/lib/hash19/resolvers.rb +64 -0
- data/lib/hash19/version.rb +3 -0
- data/lib/hash19.rb +13 -0
- data/spec/hash19/associations_spec.rb +141 -0
- data/spec/hash19/contains_spec.rb +41 -0
- data/spec/hash19/core_spec.rb +54 -0
- data/spec/hash19/future_spec.rb +18 -0
- data/spec/hash19/injection_spec.rb +41 -0
- data/spec/spec_helper.rb +4 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f6e2781c60e26a68ec9744c3fae4425ae8eaeab4
|
4
|
+
data.tar.gz: 43f215b409572f8598d85448050a1256557253d6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86e5288025699960a6fa93425a8e62046148af170a9ae718af00e6777694d91b0de7ee68cb8d9490dd8e2bf5a9a9d01d68e387e5e091641636d381cf232e17e5
|
7
|
+
data.tar.gz: bd08b6977bbdb47f2b3a7a4e1332095aba92673b2e21f59f5a01921cb936f4a810434d7f2a199338c32d83a06e31dfad57d78016e04775937ed455cb3034d602
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
hash19
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 RC
|
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,33 @@
|
|
1
|
+
# Hash19
|
2
|
+
|
3
|
+
[](https://travis-ci.org/rcdexta/hash19)
|
4
|
+
|
5
|
+
TODO: Write a gem description
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'hash19'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install hash19
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( https://github.com/[my-github-username]/hash19/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/hash19.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hash19/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'hash19'
|
8
|
+
spec.version = Hash19::VERSION
|
9
|
+
spec.authors = ['RC']
|
10
|
+
spec.email = ['rc.chandru@gmail.com']
|
11
|
+
spec.summary = %q{Hash helpers to map complex JSON to ruby objects}
|
12
|
+
spec.description = %q{Handles associations and eager loading and collection injection }
|
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'
|
22
|
+
spec.add_development_dependency 'activesupport'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
spec.add_development_dependency 'pry'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'jsonpath', '~> 0.5.6'
|
28
|
+
spec.add_runtime_dependency 'eldritch'
|
29
|
+
|
30
|
+
end
|
data/lib/hash19/core.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module Hash19
|
2
|
+
module Core
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
include Hash19::CoreHelpers
|
8
|
+
end
|
9
|
+
|
10
|
+
include Hash19::Resolvers
|
11
|
+
|
12
|
+
module Initializer
|
13
|
+
def initialize(payload={})
|
14
|
+
if self.class.contains_klass
|
15
|
+
@hash19 = payload.map do |el|
|
16
|
+
klass = resolve_class(self.class.contains_klass.to_s.camelize.singularize)
|
17
|
+
klass.send(:new, el).to_h(lazy: true)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
hash = payload.with_indifferent_access
|
21
|
+
@hash19 = hash.slice(*self.class.keys)
|
22
|
+
resolve_aliases if self.class.aliases.present?
|
23
|
+
resolve_has_one(hash) if self.class.one_assocs.present?
|
24
|
+
resolve_has_many(hash) if self.class.many_assocs.present?
|
25
|
+
end
|
26
|
+
resolve_injections(@hash19) if self.class.injections.present?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.included(klass)
|
31
|
+
klass.send :prepend, Initializer
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_h(lazy:false)
|
35
|
+
return @hash19 if lazy
|
36
|
+
if @hash19.is_a? Array
|
37
|
+
@hash19.map { |hash| traverse_hash(hash) }
|
38
|
+
else
|
39
|
+
traverse_hash(@hash19)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def traverse_hash(hash)
|
45
|
+
hash.each_with_object({}) do |(k,v),res|
|
46
|
+
res[k] = if v.is_a?(LazyValue)
|
47
|
+
v.value
|
48
|
+
elsif v.is_a?(Hash19)
|
49
|
+
v.to_h
|
50
|
+
elsif v.is_a?(Hash)
|
51
|
+
traverse_hash(v)
|
52
|
+
else
|
53
|
+
v
|
54
|
+
end
|
55
|
+
end.with_indifferent_access
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Hash19
|
2
|
+
module CoreHelpers
|
3
|
+
|
4
|
+
attr_accessor :keys
|
5
|
+
attr_accessor :aliases
|
6
|
+
attr_accessor :one_assocs
|
7
|
+
attr_accessor :many_assocs
|
8
|
+
attr_accessor :injections
|
9
|
+
attr_accessor :contains_klass
|
10
|
+
|
11
|
+
def attributes(*list)
|
12
|
+
add_attributes(*list)
|
13
|
+
end
|
14
|
+
|
15
|
+
def attribute(name, opts = {})
|
16
|
+
add_attributes(opts[:key] || name)
|
17
|
+
@aliases ||= {}
|
18
|
+
@aliases[opts[:key]] = name if opts.has_key?(:key)
|
19
|
+
end
|
20
|
+
|
21
|
+
def has_one(name, opts = {})
|
22
|
+
@one_assocs ||= {}
|
23
|
+
@one_assocs[name] = opts
|
24
|
+
end
|
25
|
+
|
26
|
+
def has_many(name, opts = {})
|
27
|
+
@many_assocs ||= {}
|
28
|
+
@many_assocs[name] = opts
|
29
|
+
end
|
30
|
+
|
31
|
+
def inject(opts)
|
32
|
+
@injections ||= []
|
33
|
+
@injections << opts
|
34
|
+
end
|
35
|
+
|
36
|
+
def contains(class_name)
|
37
|
+
@contains_klass = class_name
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def add_attributes(*list)
|
43
|
+
@keys ||= []
|
44
|
+
@keys += list
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Hash19
|
2
|
+
module Resolvers
|
3
|
+
|
4
|
+
def resolve_has_one(hash)
|
5
|
+
resolve_associations(hash, self.class.one_assocs, :one)
|
6
|
+
end
|
7
|
+
|
8
|
+
def resolve_has_many(hash)
|
9
|
+
resolve_associations(hash, self.class.many_assocs, :many)
|
10
|
+
end
|
11
|
+
|
12
|
+
def resolve_associations(hash, associations, type)
|
13
|
+
associations.each do |name, opts|
|
14
|
+
class_name = name.to_s.camelize
|
15
|
+
association = hash[opts[:key] || name]
|
16
|
+
if association.present?
|
17
|
+
klass = resolve_class(class_name.singularize)
|
18
|
+
@hash19[opts[:alias] || name] = if type == :one
|
19
|
+
klass.send(:new, association).to_h(lazy: true)
|
20
|
+
elsif type == :many
|
21
|
+
association.map { |hash| klass.send(:new, hash).to_h(lazy: true) }
|
22
|
+
end
|
23
|
+
else
|
24
|
+
# raise "Key:<#{name}> is not present in #{self.class.name}. Possible specify a trigger" unless
|
25
|
+
@hash19[opts[:alias] || name] = LazyValue.new(-> { opts[:trigger].call(@hash19.delete(opts[:using])) }) if opts[:trigger]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def resolve_aliases
|
31
|
+
self.class.aliases.each do |key, as|
|
32
|
+
@hash19[as] = @hash19.delete(key) if @hash19.has_key?(key)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def resolve_injections(hash)
|
37
|
+
together do
|
38
|
+
self.class.injections.each do |opts|
|
39
|
+
async do
|
40
|
+
entries = JsonPath.new(opts[:at]).on(hash).flatten
|
41
|
+
ids = entries.map { |el| el[opts[:using]] }
|
42
|
+
to_inject = opts[:trigger].call(ids).map(&:with_indifferent_access)
|
43
|
+
key = opts[:as] || opts[:using].to_s.gsub(/_id$|Id$/, '')
|
44
|
+
entries.each do |entry|
|
45
|
+
id = entry.delete(opts[:using])
|
46
|
+
target = to_inject.find { |el| el[opts[:reference] || opts[:using]] == id }
|
47
|
+
entry[key] = target
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def resolve_class(assoc_name)
|
56
|
+
full_class_name = self.class.name
|
57
|
+
new_class = full_class_name.gsub(full_class_name.demodulize, assoc_name)
|
58
|
+
new_class.split('::').inject(Object) do |mod, class_name|
|
59
|
+
mod.const_get(class_name)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
data/lib/hash19.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'hash19/version'
|
2
|
+
require 'hash19/lazy_value'
|
3
|
+
require 'hash19/core_helpers'
|
4
|
+
require 'hash19/resolvers'
|
5
|
+
require 'hash19/core'
|
6
|
+
require 'active_support/all'
|
7
|
+
require 'jsonpath'
|
8
|
+
require 'eldritch'
|
9
|
+
|
10
|
+
module Hash19
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
include Core
|
13
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Associations' do
|
4
|
+
|
5
|
+
class Testable
|
6
|
+
include Hash19
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'has_one associations' do
|
10
|
+
class Child < Testable
|
11
|
+
attributes :x, :y, :z
|
12
|
+
end
|
13
|
+
|
14
|
+
class Parent < Testable
|
15
|
+
attributes :p, :q
|
16
|
+
has_one :child
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should be able to load the has_one child associations' do
|
20
|
+
parent = Parent.new(p: 1, q: 2, child: {x: 1, p: 3})
|
21
|
+
expect(parent.to_h).to eq('p' => 1, 'q' => 2, 'child' => {'x' => 1})
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should support key for has_one' do
|
25
|
+
class OtherParent < Testable
|
26
|
+
attributes :x
|
27
|
+
has_one :child, key: :offspring, class: 'Child'
|
28
|
+
end
|
29
|
+
|
30
|
+
parent = OtherParent.new(x: true, offspring: {x: 1, p: 3})
|
31
|
+
expect(parent.to_h).to eq('x' => true, 'child' => {'x' => 1})
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should support alias for has_one' do
|
35
|
+
class AnotherParent < Testable
|
36
|
+
attributes :x
|
37
|
+
has_one :child, key: :offspring, alias: :junior
|
38
|
+
end
|
39
|
+
|
40
|
+
parent = AnotherParent.new(x: true, offspring: {x: 1, p: 3})
|
41
|
+
expect(parent.to_h).to eq('x' => true, 'junior' => {'x' => 1})
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'has_many associations' do
|
46
|
+
class Wheel < Testable
|
47
|
+
attributes :flat
|
48
|
+
end
|
49
|
+
|
50
|
+
class Bike < Testable
|
51
|
+
attributes :cc
|
52
|
+
has_many :wheels
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should be able to load the has_one child associations' do
|
56
|
+
bike = Bike.new(cc: 150, wheels: [{name: 'one', flat: false}, {name: 'two', flat: true}])
|
57
|
+
expect(bike.to_h).to eq('cc' => 150, 'wheels' => [{'flat' => false}, {'flat' => true}])
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should support key for has_many' do
|
61
|
+
class OtherBike < Testable
|
62
|
+
attributes :cc
|
63
|
+
has_many :wheels, key: :discs
|
64
|
+
end
|
65
|
+
|
66
|
+
bike = OtherBike.new(cc: 150, discs: [{name: 'one', flat: false}, {name: 'two', flat: true}])
|
67
|
+
expect(bike.to_h).to eq('cc' => 150, 'wheels' => [{'flat' => false}, {'flat' => true}])
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should support alias for has_many' do
|
71
|
+
class AnotherBike < Testable
|
72
|
+
attributes :cc
|
73
|
+
has_many :wheels, key: :discs, alias: :rings
|
74
|
+
end
|
75
|
+
|
76
|
+
bike = AnotherBike.new(cc: 150, discs: [{name: 'one', flat: false}, {name: 'two', flat: true}])
|
77
|
+
expect(bike.to_h).to eq('cc' => 150, 'rings' => [{'flat' => false}, {'flat' => true}])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'across modules' do
|
82
|
+
module Bird
|
83
|
+
class Duck < Testable
|
84
|
+
attribute :quack
|
85
|
+
has_many :feathers
|
86
|
+
end
|
87
|
+
|
88
|
+
class Feather < Testable
|
89
|
+
attribute :light
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should be able to resolve has_many relationship within modules' do
|
94
|
+
duck = Bird::Duck.new(quack: true, feathers: [{light: true}, {light: true}])
|
95
|
+
expect(duck.to_h).to eq('quack' => true, 'feathers' => [{'light' => true}, {'light' => true}])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class Error < Testable
|
100
|
+
attributes :error_id, :desc
|
101
|
+
|
102
|
+
def self.find(id)
|
103
|
+
{error_id: 500, desc: 'fatal error'}
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.find_all
|
107
|
+
[{error_id: 500, desc: 'fatal error'}, {error_id: 404, desc: 'not found'}]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'has_one evaluation' do
|
112
|
+
|
113
|
+
class Packet < Testable
|
114
|
+
attributes :code, :error_id
|
115
|
+
has_one :error, using: :error_id, trigger: ->(id) { Error.find id }
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should be able to call the trigger on has_one association' do
|
119
|
+
packet = Packet.new(code: 500, error_id: 500)
|
120
|
+
expect(packet.to_h).to eq('code' => 500, 'error_id' => 500, 'error' => {'error_id' => 500, 'desc' => 'fatal error'})
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'has_many evaluation' do
|
126
|
+
|
127
|
+
class UDPPacket < Testable
|
128
|
+
attributes :code, :error_id
|
129
|
+
has_many :errors, using: :error_id, trigger: ->(id) { Error.find_all }, alias: 'all_errors'
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should be able to call the trigger on has_one association' do
|
133
|
+
packet = UDPPacket.new(code: 500, error_id: 500)
|
134
|
+
expect(packet.to_h).to eq('code' => 500, 'error_id' => 500, 'all_errors' => [{'error_id' => 500, 'desc' => 'fatal error'},
|
135
|
+
{'error_id' => 404, 'desc' => 'not found'}])
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Containers' do
|
4
|
+
|
5
|
+
class Testable
|
6
|
+
include Hash19
|
7
|
+
end
|
8
|
+
|
9
|
+
class SuperHeroes < Testable
|
10
|
+
contains :super_heroes
|
11
|
+
inject at: '$', using: :weapon_id, reference: :id, trigger: lambda { |ids| Weapon.find_all ids }
|
12
|
+
end
|
13
|
+
|
14
|
+
class SuperHero < Testable
|
15
|
+
attributes :name, :power, :weapon_id
|
16
|
+
has_one :weapon, using: :weapon_id, trigger: lambda { |id| Weapon.find id }
|
17
|
+
end
|
18
|
+
|
19
|
+
class Weapon < Testable
|
20
|
+
attributes :name, :id
|
21
|
+
|
22
|
+
def self.find_all(ids)
|
23
|
+
[{'name' => 'jarvis', 'id' => 1},
|
24
|
+
{'name' => 'hammer', 'id' => 2},
|
25
|
+
{'name' => 'hands', 'id' => 3}]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'contains directive' do
|
30
|
+
it 'should comprise of collection of specified object' do
|
31
|
+
super_heroes = SuperHeroes.new([{name: 'iron man', power: 'none', weapon_id: 1},
|
32
|
+
{name: 'thor', power: 'class 100', weapon_id: 2},
|
33
|
+
{name: 'hulk', power: 'bulk', weapon_id: 3}])
|
34
|
+
|
35
|
+
expect(super_heroes.to_h).to eq([{'name' => 'iron man', 'power' => 'none', 'weapon' => {'name' => 'jarvis', 'id' => 1}},
|
36
|
+
{'name' => 'thor', 'power' => 'class 100', 'weapon' => {'name' => 'hammer', 'id' => 2}},
|
37
|
+
{'name' => 'hulk', 'power' => 'bulk', 'weapon' => {'name' => 'hands', 'id' => 3}}])
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash19::Core do
|
4
|
+
|
5
|
+
class Testable
|
6
|
+
include Hash19
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'declare attributes' do
|
10
|
+
|
11
|
+
class Test1 < Testable
|
12
|
+
attributes :a, :b, :c
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
it 'should be able to assign whitelisted attributes' do
|
17
|
+
test = Test1.new(a: 1, b: 2, d: 4)
|
18
|
+
expect(test.to_h).to eq('a' => 1, 'b' => 2)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should return empty hash when no attributes match' do
|
22
|
+
test = Test1.new(x: 1, y: 2, z: 4)
|
23
|
+
expect(test.to_h).to eq({})
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'Single attribute and aliases' do
|
29
|
+
|
30
|
+
class Test2 < Testable
|
31
|
+
attributes :a, :b, :c
|
32
|
+
attribute :fake, key: :actual
|
33
|
+
attribute :d
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should be able to assign attributes based on alias' do
|
37
|
+
test = Test2.new(actual: 1)
|
38
|
+
expect(test.to_h).to eq('fake' => 1)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should be able to use both attribute and attributes constructs' do
|
42
|
+
test = Test2.new(actual: 1, a: 2, d: 3)
|
43
|
+
expect(test.to_h).to eq('fake' => 1, 'a' => 2, 'd' => 3)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should ignore alias if key not present' do
|
47
|
+
test = Test2.new(a: 2)
|
48
|
+
expect(test.to_h).to eq('a' => 2)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Hash19::LazyValue do
|
4
|
+
|
5
|
+
class Sheep
|
6
|
+
attr_accessor :name
|
7
|
+
def shout
|
8
|
+
"My name is #{@name || 'black sheep'}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should be able to postpone execution of a block' do
|
13
|
+
sheep = Sheep.new
|
14
|
+
delayed_voice = Hash19::LazyValue.new(-> { sheep.shout })
|
15
|
+
sheep.name = 'Dolly'
|
16
|
+
expect(delayed_voice.value).to eq('My name is Dolly')
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Injections' do
|
4
|
+
|
5
|
+
class Testable
|
6
|
+
include Hash19
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'has_many injection' do
|
10
|
+
|
11
|
+
class SuperVillain < Testable
|
12
|
+
attribute :name
|
13
|
+
has_many :minions
|
14
|
+
inject at: '$.minions', using: :fruit_id, reference: :id, trigger: lambda { |ids| Fruit.find_all ids }, as: 'eats'
|
15
|
+
end
|
16
|
+
|
17
|
+
class Minion < Testable
|
18
|
+
attributes :name, :fruit_id
|
19
|
+
end
|
20
|
+
|
21
|
+
class Fruit < Testable
|
22
|
+
attributes :id, :name
|
23
|
+
|
24
|
+
def self.find_all(ids)
|
25
|
+
[{id: 1, name: 'banana'}, {id: 2, name: 'apple'}, {id: 3, name: 'orange'}]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
it 'should be able to extract ids from a has_many association and inject' do
|
31
|
+
gru_team = SuperVillain.new(name: 'Gru', minions: [{name: 'Poppadom', fruit_id: 1},
|
32
|
+
{name: 'Gelato', fruit_id: 2},
|
33
|
+
{name: 'Kanpai', fruit_id: 3}])
|
34
|
+
expect(gru_team.to_h).to eq('name' => 'Gru', 'minions' => [{'name' => 'Poppadom', 'eats' => {'id' => 1, 'name' => 'banana'}},
|
35
|
+
{'name' => 'Gelato', 'eats' => {'id' => 2, 'name' => 'apple'}},
|
36
|
+
{'name' => 'Kanpai', 'eats' => {'id' => 3, 'name' => 'orange'}}
|
37
|
+
])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash19
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- RC
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: jsonpath
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.5.6
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.5.6
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: eldritch
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: 'Handles associations and eager loading and collection injection '
|
112
|
+
email:
|
113
|
+
- rc.chandru@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".ruby-gemset"
|
120
|
+
- ".ruby-version"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENSE.txt
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- hash19.gemspec
|
127
|
+
- lib/hash19.rb
|
128
|
+
- lib/hash19/core.rb
|
129
|
+
- lib/hash19/core_helpers.rb
|
130
|
+
- lib/hash19/lazy_value.rb
|
131
|
+
- lib/hash19/resolvers.rb
|
132
|
+
- lib/hash19/version.rb
|
133
|
+
- spec/hash19/associations_spec.rb
|
134
|
+
- spec/hash19/contains_spec.rb
|
135
|
+
- spec/hash19/core_spec.rb
|
136
|
+
- spec/hash19/future_spec.rb
|
137
|
+
- spec/hash19/injection_spec.rb
|
138
|
+
- spec/spec_helper.rb
|
139
|
+
homepage: ''
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
metadata: {}
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 2.2.2
|
160
|
+
signing_key:
|
161
|
+
specification_version: 4
|
162
|
+
summary: Hash helpers to map complex JSON to ruby objects
|
163
|
+
test_files:
|
164
|
+
- spec/hash19/associations_spec.rb
|
165
|
+
- spec/hash19/contains_spec.rb
|
166
|
+
- spec/hash19/core_spec.rb
|
167
|
+
- spec/hash19/future_spec.rb
|
168
|
+
- spec/hash19/injection_spec.rb
|
169
|
+
- spec/spec_helper.rb
|