hashoid 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 +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +104 -0
- data/Rakefile +1 -0
- data/hashoid.gemspec +24 -0
- data/lib/hashoid/version.rb +3 -0
- data/lib/hashoid.rb +123 -0
- metadata +94 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: ab3788f63fb1dd2141d9919258b04f88bd8b52fa
|
|
4
|
+
data.tar.gz: 149957b26df64dad7b4afe9d9bedd88e035305ee
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 70400942144e911ccc64619e5baa1ce659b0af0793a1eeedc5bc7016e019e6b1a3f56d1754051ecd3c603c0604c43da397e1fffbdb57bd6603d94d5c923f5249
|
|
7
|
+
data.tar.gz: f9f0149d88d3318f719a61445d4a35a3d152423a430dc4af404a712e0f84101e56b9e1db95aea1c89edd35325d37393bd0517fe555b7cf32fd55100569d44776
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Thibaut Sacreste
|
|
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,104 @@
|
|
|
1
|
+
# Hashoid
|
|
2
|
+
|
|
3
|
+
Turns your bland json/hashes into flavoursome objects! Useful for consuming web services.
|
|
4
|
+
|
|
5
|
+
Loosely and shamelessly inspired by Mongoid.
|
|
6
|
+
|
|
7
|
+
Supports:
|
|
8
|
+
* attribute declaration via `field(s)` and `collection(s)` class methods
|
|
9
|
+
* field type inference from field name (supports name inflection for collections)
|
|
10
|
+
* custom type declaration
|
|
11
|
+
* custom transforms
|
|
12
|
+
* inheritance
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Add this line to your application's Gemfile:
|
|
17
|
+
|
|
18
|
+
gem 'hashoid'
|
|
19
|
+
|
|
20
|
+
And then execute:
|
|
21
|
+
|
|
22
|
+
$ bundle
|
|
23
|
+
|
|
24
|
+
Or install it yourself as:
|
|
25
|
+
|
|
26
|
+
$ gem install hashoid
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
require 'hashoid'
|
|
32
|
+
require 'json'
|
|
33
|
+
require 'time'
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Country
|
|
37
|
+
include Hashoid
|
|
38
|
+
field :name
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
class Food
|
|
42
|
+
include Hashoid
|
|
43
|
+
field :sell_by_date, transform: -> s {Time.parse(s)}
|
|
44
|
+
|
|
45
|
+
def out_of_date?
|
|
46
|
+
sell_by_date < Time.now
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class Cheese < Food
|
|
51
|
+
fields [:name, :age]
|
|
52
|
+
field :origin, type: Country
|
|
53
|
+
|
|
54
|
+
def ripe_enough?
|
|
55
|
+
age > 1
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class CheeseBoard
|
|
60
|
+
include Hashoid
|
|
61
|
+
collection :cheeses
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
cheeseboard_json = '
|
|
65
|
+
{
|
|
66
|
+
"cheeses" : [
|
|
67
|
+
{
|
|
68
|
+
"name" : "Camembert",
|
|
69
|
+
"age" : 2,
|
|
70
|
+
"origin" : { "name" : "France" },
|
|
71
|
+
"sell_by_date" : "2014/04/01"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"name" : "Gorgonzola",
|
|
75
|
+
"age" : 5,
|
|
76
|
+
"origin" : { "name" : "Italy" },
|
|
77
|
+
"sell_by_date" : "2014/03/28"
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
}'
|
|
81
|
+
|
|
82
|
+
cheese_board = CheeseBoard.new(JSON.parse(cheeseboard_json))
|
|
83
|
+
# or easier
|
|
84
|
+
cheese_board = CheeseBoard.from_json(cheeseboard_json)
|
|
85
|
+
|
|
86
|
+
p cheese_board
|
|
87
|
+
p cheese_board.to_h
|
|
88
|
+
p cheese_board.to_json
|
|
89
|
+
cheese_board.cheeses.each do |cheese|
|
|
90
|
+
if !cheese.out_of_date? && cheese.ripe_enough?
|
|
91
|
+
puts "Have some #{cheese.name} from #{cheese.origin.name}. It's ripe!"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
## Contributing
|
|
99
|
+
|
|
100
|
+
1. Fork it
|
|
101
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
102
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
103
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
104
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
data/hashoid.gemspec
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'hashoid/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "hashoid"
|
|
8
|
+
spec.version = Hashoid::VERSION
|
|
9
|
+
spec.authors = ["Thibaut Sacreste"]
|
|
10
|
+
spec.email = ["thibaut.sacreste@gmail.com"]
|
|
11
|
+
spec.description = "Hashoid: a JSON/Hash to Object Mapper"
|
|
12
|
+
spec.summary = "Turns your bland json/hashes into flavoursome objects!"
|
|
13
|
+
spec.homepage = "https://github.com/uswitch/hashoid"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
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.3"
|
|
22
|
+
spec.add_development_dependency "rake"
|
|
23
|
+
spec.add_runtime_dependency "activesupport"
|
|
24
|
+
end
|
data/lib/hashoid.rb
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
require_relative 'hashoid/version'
|
|
2
|
+
require 'active_support/inflector'
|
|
3
|
+
|
|
4
|
+
module Hashoid
|
|
5
|
+
|
|
6
|
+
def self.included base
|
|
7
|
+
base.extend(ClassMethods)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
|
|
12
|
+
def field name, opts={}
|
|
13
|
+
_fields[name] = opts.dup
|
|
14
|
+
attr_reader name
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def fields names, opts={}
|
|
18
|
+
names.each{|name| field(name, opts)}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def collection name, opts={}
|
|
22
|
+
field(name, opts.merge(collection: true))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def collections names, opts={}
|
|
26
|
+
names.each{|name| collection(name, opts)}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def from_json(json)
|
|
30
|
+
self.new(JSON.parse(json))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def _fields
|
|
34
|
+
@fields ||= superclass.respond_to?(:_fields) ? {}.merge(superclass._fields) : {}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def is_collection? field
|
|
38
|
+
_fields[field] && _fields[field][:collection]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def field_names
|
|
42
|
+
_fields.keys
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def field_type(field, &block)
|
|
46
|
+
info = _fields[field] ||= {}
|
|
47
|
+
info[:checked], info[:type] = true, yield unless info[:type] or info[:checked]
|
|
48
|
+
info[:type]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def field_default field
|
|
52
|
+
_fields[field] && _fields[field][:default]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def field_transform field
|
|
56
|
+
_fields[field] && _fields[field][:transform]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def find_class field
|
|
60
|
+
field_type(field) do
|
|
61
|
+
class_name = field.to_s.split('_').map(&:capitalize).join
|
|
62
|
+
class_name = class_name.singularize if is_collection?(field)
|
|
63
|
+
my_module.const_get(class_name) if my_module.const_defined?(class_name)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def my_module
|
|
68
|
+
@my_module ||= (self.name =~ /^(.+)::[^:]+$/) ? $1.constantize : Module
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def initialize args={}
|
|
73
|
+
self.class._fields.each {|k, _| instance_variable_set("@#{k}", self.class.field_default(k))}
|
|
74
|
+
|
|
75
|
+
@args = args
|
|
76
|
+
args.each do |k, v|
|
|
77
|
+
field = k.to_s.gsub('-', '_').to_sym
|
|
78
|
+
unless defined?(field).nil? # check if it's included as a reader attribute
|
|
79
|
+
result = v.instance_of?(Array) ? init_objects(field, v) : init_object(field, v)
|
|
80
|
+
instance_variable_set("@#{field}", result) if result
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def each
|
|
86
|
+
self.class.field_names.map do |f|
|
|
87
|
+
[f, self.send(f)]
|
|
88
|
+
end.reject do |_, v|
|
|
89
|
+
v.nil?
|
|
90
|
+
end.each do |f, v|
|
|
91
|
+
yield(f, v)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def [] field
|
|
96
|
+
respond_to?(field) ? self.send(field) : @args[field]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def to_h
|
|
100
|
+
@args
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def to_json
|
|
104
|
+
to_h.to_json
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
private
|
|
108
|
+
|
|
109
|
+
def init_objects field, values
|
|
110
|
+
values.inject([]) {|arr, v| arr << init_object(field, v)}
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def init_object field, value
|
|
114
|
+
klass = self.class.find_class(field)
|
|
115
|
+
value.instance_of?(Hash) && klass ? klass.new(value) : transform(field, value)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def transform field, value
|
|
119
|
+
(value && self.class.field_transform(field)) ? self.class.field_transform(field).call(value) : value
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
end
|
|
123
|
+
|
metadata
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hashoid
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Thibaut Sacreste
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-03-06 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: '1.3'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.3'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
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: activesupport
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - '>='
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :runtime
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: 'Hashoid: a JSON/Hash to Object Mapper'
|
|
56
|
+
email:
|
|
57
|
+
- thibaut.sacreste@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- .gitignore
|
|
63
|
+
- Gemfile
|
|
64
|
+
- LICENSE.txt
|
|
65
|
+
- README.md
|
|
66
|
+
- Rakefile
|
|
67
|
+
- hashoid.gemspec
|
|
68
|
+
- lib/hashoid.rb
|
|
69
|
+
- lib/hashoid/version.rb
|
|
70
|
+
homepage: https://github.com/uswitch/hashoid
|
|
71
|
+
licenses:
|
|
72
|
+
- MIT
|
|
73
|
+
metadata: {}
|
|
74
|
+
post_install_message:
|
|
75
|
+
rdoc_options: []
|
|
76
|
+
require_paths:
|
|
77
|
+
- lib
|
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - '>='
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - '>='
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '0'
|
|
88
|
+
requirements: []
|
|
89
|
+
rubyforge_project:
|
|
90
|
+
rubygems_version: 2.0.3
|
|
91
|
+
signing_key:
|
|
92
|
+
specification_version: 4
|
|
93
|
+
summary: Turns your bland json/hashes into flavoursome objects!
|
|
94
|
+
test_files: []
|