restify 0.3.2.1.b56 → 0.3.2.1.b58
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/restify/adapter/celluloid.rb +34 -0
- data/lib/restify/{adapter.rb → adapter/em.rb} +0 -0
- data/lib/restify/contextual.rb +2 -0
- data/lib/restify/http.rb +1 -1
- data/restify.gemspec +3 -1
- data/spec/spec_helper.rb +1 -0
- metadata +35 -10
- data/vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/CHANGELOG.md +0 -150
- data/vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/LICENSE +0 -20
- data/vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/README.md +0 -710
- data/vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/hashie.gemspec +0 -35
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YmU5MmRkYWRlMjQ2MTVkYjU1OGQ2NDViNTUzZTVkNjc3NGE2YTNjYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MmZkMmY5YmM5YTUxMTA5ZGIzODFjMzI2MWM4ZTRmMjQ1NTA4NzYyNQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjdhYjI3NGFjNmViZmU4ODE3ODg2YWUyMTg2NGM0OTcwMTA2MDJkY2QwNzVi
|
10
|
+
OTE3NDkwMDc1MTVmOTg2MjBhYTJjM2M2ZGUxYWM5NmNkMTg2YzI3MDRmNTk2
|
11
|
+
ODM2ODM2NTM1NzdmYzAzYjYyMzU2ZjEzODc3YjA1MDg0NDFmMWU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NWUwNjRlNTgwNzUyMWQ0NmYyMzI0ZjEwZGRlMmRlODI5NDhkYzA5YTZlYWQ2
|
14
|
+
NDZhNDg0MDkzN2MzNDFmNWYxOGFkNTMxZThjYmM2ZTZjNTQ1NjM4ZTJmNGI1
|
15
|
+
NzA1NjcxZjFhMDVkNzlkNThjN2JmMTY2YTRlZDlmYWVmZDBiYjk=
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'celluloid/io'
|
2
|
+
require 'http'
|
3
|
+
|
4
|
+
module Restify
|
5
|
+
#
|
6
|
+
module Adapter
|
7
|
+
#
|
8
|
+
class Celluloid
|
9
|
+
include ::Celluloid::IO
|
10
|
+
|
11
|
+
def process(request, writer)
|
12
|
+
response = ::HTTP
|
13
|
+
.headers(request.headers)
|
14
|
+
.send request.method.downcase,
|
15
|
+
request.uri.to_s,
|
16
|
+
body: request.body,
|
17
|
+
socket_class: ::Celluloid::IO::TCPSocket
|
18
|
+
|
19
|
+
writer.fulfill Response.new \
|
20
|
+
request,
|
21
|
+
response.uri,
|
22
|
+
response.status.code,
|
23
|
+
response.headers.to_h,
|
24
|
+
response.body.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def call(request)
|
28
|
+
Obligation.create do |writer|
|
29
|
+
async.process request, writer
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
File without changes
|
data/lib/restify/contextual.rb
CHANGED
data/lib/restify/http.rb
CHANGED
data/restify.gemspec
CHANGED
@@ -20,7 +20,9 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_runtime_dependency 'obligation', '~> 0.1'
|
22
22
|
spec.add_runtime_dependency 'addressable', '~> 2.3'
|
23
|
-
spec.add_runtime_dependency '
|
23
|
+
spec.add_runtime_dependency 'http', '~> 0.8.12'
|
24
|
+
spec.add_runtime_dependency 'celluloid-io', '~> 0.16.2'
|
25
|
+
spec.add_runtime_dependency 'em-http-request'
|
24
26
|
spec.add_runtime_dependency 'hashie', '~> 3.3'
|
25
27
|
spec.add_runtime_dependency 'multi_json'
|
26
28
|
spec.add_runtime_dependency 'rack'
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.2.1.
|
4
|
+
version: 0.3.2.1.b58
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Graichen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: obligation
|
@@ -39,19 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: http
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 0.8.12
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 0.8.12
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: celluloid-io
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.16.2
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.16.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: em-http-request
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: hashie
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -119,7 +147,8 @@ files:
|
|
119
147
|
- LICENSE.txt
|
120
148
|
- README.md
|
121
149
|
- lib/restify.rb
|
122
|
-
- lib/restify/adapter.rb
|
150
|
+
- lib/restify/adapter/celluloid.rb
|
151
|
+
- lib/restify/adapter/em.rb
|
123
152
|
- lib/restify/collection.rb
|
124
153
|
- lib/restify/context.rb
|
125
154
|
- lib/restify/contextual.rb
|
@@ -140,10 +169,6 @@ files:
|
|
140
169
|
- spec/restify/resource_spec.rb
|
141
170
|
- spec/restify_spec.rb
|
142
171
|
- spec/spec_helper.rb
|
143
|
-
- vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/CHANGELOG.md
|
144
|
-
- vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/LICENSE
|
145
|
-
- vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/README.md
|
146
|
-
- vendor/bundle/ruby/2.1.0/bundler/gems/hashie-edeef5633c9b/hashie.gemspec
|
147
172
|
homepage: https://github.com/jgraichen/restify
|
148
173
|
licenses:
|
149
174
|
- LGPLv3
|
@@ -1,150 +0,0 @@
|
|
1
|
-
## Next Release
|
2
|
-
|
3
|
-
* Your contribution here.
|
4
|
-
* [#304](https://github.com/intridea/hashie/pull/304): Ensured compatibility of `Hash` extensions with singleton objects - [@regexident](https://github.com/regexident).
|
5
|
-
|
6
|
-
## 3.4.2 (6/2/2015)
|
7
|
-
|
8
|
-
* [#292](https://github.com/intridea/hashie/pull/292): Removed `Mash#id` and `Mash#type` - [@jrochkind](https://github.com/jrochkind).
|
9
|
-
* [#297](https://github.com/intridea/hashie/pull/297): Extracted `Trash`'s behavior into a new `Dash::PropertyTranslation` extension - [@michaelherold](https://github.com/michaelherold).
|
10
|
-
|
11
|
-
## 3.4.1 (3/31/2015)
|
12
|
-
|
13
|
-
* [#269](https://github.com/intridea/hashie/pull/272): Added Hashie::Extensions::DeepLocate - [@msievers](https://github.com/msievers).
|
14
|
-
* [#270](https://github.com/intridea/hashie/pull/277): Fixed ArgumentError raised when using IndifferentAccess and HashWithIndifferentAccess - [@gardenofwine](https://github.com/gardenofwine).
|
15
|
-
* [#281](https://github.com/intridea/hashie/pull/281): Added #reverse_merge to Mash to override ActiveSupport's version - [@mgold](https://github.com/mgold).
|
16
|
-
* [#282](https://github.com/intridea/hashie/pull/282): Fixed coercions in a subclass accumulating in the superclass - [@maxlinc](https://github.com/maxlinc), [@martinstreicher](https://github.com/martinstreicher).
|
17
|
-
|
18
|
-
## 3.4.0 (2/02/2015)
|
19
|
-
|
20
|
-
* [#271](https://github.com/intridea/hashie/pull/271): Added ability to define defaults based on current hash - [@gregory](https://github.com/gregory).
|
21
|
-
* [#247](https://github.com/intridea/hashie/pull/247): Fixed #stringify_keys and #symbolize_keys collision with ActiveSupport - [@bartoszkopinski](https://github.com/bartoszkopinski).
|
22
|
-
* [#249](https://github.com/intridea/hashie/pull/249): SafeAssignment will now also protect hash-style assignments - [@jrochkind](https://github.com/jrochkind).
|
23
|
-
* [#251](https://github.com/intridea/hashie/pull/251): Added block support to indifferent access #fetch - [@jgraichen](https://github.com/jgraichen).
|
24
|
-
* [#252](https://github.com/intridea/hashie/pull/252): Added support for conditionally required Hashie::Dash attributes - [@ccashwell](https://github.com/ccashwell).
|
25
|
-
* [#256](https://github.com/intridea/hashie/pull/256): Inherit key coercions - [@Erol](https://github.com/Erol).
|
26
|
-
* [#259](https://github.com/intridea/hashie/pull/259): Fixed handling of default proc values in Mash - [@Erol](https://github.com/Erol).
|
27
|
-
* [#260](https://github.com/intridea/hashie/pull/260): Added block support to Extensions::DeepMerge - [@galathius](https://github.com/galathius).
|
28
|
-
* [#254](https://github.com/intridea/hashie/pull/254): Added public utility methods for stringify and symbolize keys - [@maxlinc](https://github.com/maxlinc).
|
29
|
-
* [#261](https://github.com/intridea/hashie/pull/261): Fixed bug where Dash.property modifies argument object - [@d-tw](https://github.com/d-tw).
|
30
|
-
* [#264](https://github.com/intridea/hashie/pull/264): Methods such as abc? return true/false with Hashie::Extensions::MethodReader - [@Zloy](https://github.com/Zloy).
|
31
|
-
* [#269](https://github.com/intridea/hashie/pull/269): Add #extractable_options? so ActiveSupport Array#extract_options! can extract it - [@ridiculous](https://github.com/ridiculous).
|
32
|
-
|
33
|
-
## 3.3.2 (11/26/2014)
|
34
|
-
|
35
|
-
* [#233](https://github.com/intridea/hashie/pull/233): Custom error messages for required properties in Hashie::Dash subclasses - [@joss](https://github.com/joss).
|
36
|
-
* [#231](https://github.com/intridea/hashie/pull/231): Added support for coercion on class type that inherit from Hash - [@gregory](https://github.com/gregory).
|
37
|
-
* [#228](https://github.com/intridea/hashie/pull/228): Made Hashie::Extensions::Parsers::YamlErbParser pass template filename to ERB - [@jperville](https://github.com/jperville).
|
38
|
-
* [#224](https://github.com/intridea/hashie/pull/224): Merging Hashie::Mash now correctly only calls the block on duplicate values - [@amysutedja](https://github.com/amysutedja).
|
39
|
-
* [#221](https://github.com/intridea/hashie/pull/221): Reduce amount of allocated objects on calls with suffixes in Hashie::Mash - [@kubum](https://github.com/kubum).
|
40
|
-
* [#245](https://github.com/intridea/hashie/pull/245): Added Hashie::Extensions::MethodAccessWithOverride to autoloads - [@Fritzinger](https://github.com/Fritzinger).
|
41
|
-
|
42
|
-
## 3.3.1 (8/26/2014)
|
43
|
-
|
44
|
-
* [#183](https://github.com/intridea/hashie/pull/183): Added Mash#load with YAML file support - [@gregory](https://github.com/gregory).
|
45
|
-
* [#195](https://github.com/intridea/hashie/pull/195): Ensure that the same object is returned after injecting IndifferentAccess - [@michaelherold](https://github.com/michaelherold).
|
46
|
-
* [#201](https://github.com/intridea/hashie/pull/201): Hashie::Trash transforms can be inherited - [@fobocaster](https://github.com/fobocaster).
|
47
|
-
* [#189](https://github.com/intridea/hashie/pull/189): Added Rash#fetch - [@medcat](https://github.com/medcat).
|
48
|
-
* [#200](https://github.com/intridea/hashie/pull/200): Improved coercion: primitives and error handling - [@maxlinc](https://github.com/maxlinc).
|
49
|
-
* [#204](https://github.com/intridea/hashie/pull/204): Added Hashie::Extensions::MethodOverridingWriter and MethodAccessWithOverride - [@michaelherold](https://github.com/michaelherold).
|
50
|
-
* [#205](http://github.com/intridea/hashie/pull/205): Added Hashie::Extensions::Mash::SafeAssignment - [@michaelherold](https://github.com/michaelherold).
|
51
|
-
* [#206](http://github.com/intridea/hashie/pull/206): Fixed stack overflow from repetitively including coercion in subclasses - [@michaelherold](https://github.com/michaelherold).
|
52
|
-
* [#207](http://github.com/intridea/hashie/pull/207): Fixed inheritance of transformations in Trash - [@fobocaster](https://github.com/fobocaster).
|
53
|
-
* [#209](http://github.com/intridea/hashie/pull/209): Added Hashie::Extensions::DeepFind - [@michaelherold](https://github.com/michaelherold).
|
54
|
-
* [#69](https://github.com/intridea/hashie/pull/69): Fixed regression in assigning multiple properties in Hashie::Trash - [@michaelherold](https://github.com/michaelherold), [@einzige](https://github.com/einzige), [@dblock](https://github.com/dblock).
|
55
|
-
|
56
|
-
## 3.2.0 (7/10/2014)
|
57
|
-
|
58
|
-
* [#164](https://github.com/intridea/hashie/pull/164), [#165](https://github.com/intridea/hashie/pull/165), [#166](https://github.com/intridea/hashie/pull/166): Fixed stack overflow when coercing mashes that contain ActiveSupport::HashWithIndifferentAccess values - [@numinit](https://github.com/numinit), [@kgrz](https://github.com/kgrz).
|
59
|
-
* [#177](https://github.com/intridea/hashie/pull/177): Added support for coercing enumerables and collections - [@gregory](https://github.com/gregory).
|
60
|
-
* [#179](https://github.com/intridea/hashie/pull/179): Mash#values_at will convert each key before doing the lookup - [@nahiluhmot](https://github.com/nahiluhmot).
|
61
|
-
* [#184](https://github.com/intridea/hashie/pull/184): Allow ranges on Rash to match all Numeric types - [@medcat](https://github.com/medcat).
|
62
|
-
* [#187](https://github.com/intridea/hashie/pull/187): Automatically require version - [@medcat](https://github.com/medcat).
|
63
|
-
* [#190](https://github.com/intridea/hashie/issues/190): Fixed `coerce_key` with `from` Trash feature and Coercion extension - [@gregory](https://github.com/gregory).
|
64
|
-
* [#192](https://github.com/intridea/hashie/pull/192): Fixed StringifyKeys#stringify_keys! to recursively stringify keys of embedded ::Hash types - [@dblock](https://github.com/dblock).
|
65
|
-
|
66
|
-
## 3.1.0 (6/25/2014)
|
67
|
-
|
68
|
-
* [#169](https://github.com/intridea/hashie/pull/169): Hash#to_hash will also convert nested objects that implement to_hash - [@gregory](https://github.com/gregory).
|
69
|
-
* [#171](https://github.com/intridea/hashie/pull/171): Include Trash and Dash class name when raising `NoMethodError` - [@gregory](https://github.com/gregory).
|
70
|
-
* [#172](https://github.com/intridea/hashie/pull/172): Added Dash and Trash#update_attributes! - [@gregory](https://github.com/gregory).
|
71
|
-
* [#173](https://github.com/intridea/hashie/pull/173): Auto include Dash::IndifferentAccess when IndiferentAccess is included in Dash - [@gregory](https://github.com/gregory).
|
72
|
-
* [#174](https://github.com/intridea/hashie/pull/174): Fixed `from` and `transform_with` Trash features when IndifferentAccess is included - [@gregory](https://github.com/gregory).
|
73
|
-
|
74
|
-
## 3.0.0 (6/3/2014)
|
75
|
-
|
76
|
-
**Note:** This version introduces several backward incompatible API changes. See [UPGRADING](UPGRADING.md) for details.
|
77
|
-
|
78
|
-
* [#150](https://github.com/intridea/hashie/pull/159): Handle nil intermediate object on deep fetch - [@stephenaument](https://github.com/stephenaument).
|
79
|
-
* [#146](https://github.com/intridea/hashie/issues/146): Mash#respond_to? inconsistent with #method_missing and does not respond to #permitted? - [@dblock](https://github.com/dblock).
|
80
|
-
* [#152](https://github.com/intridea/hashie/pull/152): Do not convert keys to String in Hashie::Dash and Hashie::Trash, use Hashie::Extensions::Dash::IndifferentAccess to achieve backward compatible behavior - [@dblock](https://github.com/dblock).
|
81
|
-
* [#152](https://github.com/intridea/hashie/pull/152): Do not automatically stringify keys in Hashie::Hash#to_hash, pass `:stringify_keys` to achieve backward compatible behavior - [@dblock](https://github.com/dblock).
|
82
|
-
* [#148](https://github.com/intridea/hashie/pull/148): Consolidated Hashie::Hash#stringify_keys implementation - [@dblock](https://github.com/dblock).
|
83
|
-
* [#149](https://github.com/intridea/hashie/issues/149): Allow IgnoreUndeclared and DeepMerge to be used with undeclared properties - [@jhaesus](https://github.com/jhaesus).
|
84
|
-
|
85
|
-
## 2.1.2 (5/12/2014)
|
86
|
-
|
87
|
-
* [#169](https://github.com/intridea/hashie/pull/169): Hash#to_hash will also convert nested objects that implement `to_hash` - [@gregory](https://github.com/gregory).
|
88
|
-
|
89
|
-
## 2.1.1 (4/12/2014)
|
90
|
-
|
91
|
-
* [#144](https://github.com/intridea/hashie/issues/144): Fixed regression invoking `to_hash` with no parameters - [@mbleigh](https://github.com/mbleigh).
|
92
|
-
|
93
|
-
## 2.1.0 (4/6/2014)
|
94
|
-
|
95
|
-
* [#134](https://github.com/intridea/hashie/pull/134): Add deep_fetch extension for nested access - [@tylerdooling](https://github.com/tylerdooling).
|
96
|
-
* Removed support for Ruby 1.8.7 - [@dblock](https://github.com/dblock).
|
97
|
-
* Ruby style now enforced with Rubocop - [@dblock](https://github.com/dblock).
|
98
|
-
* [#138](https://github.com/intridea/hashie/pull/138): Added Hashie::Rash, a hash whose keys can be regular expressions or ranges - [@epitron](https://github.com/epitron).
|
99
|
-
* [#131](https://github.com/intridea/hashie/pull/131): Added IgnoreUndeclared, an extension to silently ignore undeclared properties at intialization - [@righi](https://github.com/righi).
|
100
|
-
* [#136](https://github.com/intridea/hashie/issues/136): Removed Hashie::Extensions::Structure - [@markiz](https://github.com/markiz).
|
101
|
-
* [#107](https://github.com/intridea/hashie/pull/107): Fixed excessive value conversions, poor performance of deep merge in Hashie::Mash - [@davemitchell](https://github.com/dblock), [@dblock](https://github.com/dblock).
|
102
|
-
* [#69](https://github.com/intridea/hashie/issues/69): Fixed assigning multiple properties in Hashie::Trash - [@einzige](https://github.com/einzige).
|
103
|
-
* [#100](https://github.com/intridea/hashie/pull/100): IndifferentAccess#store will respect indifference - [@jrochkind](https://github.com/jrochkind).
|
104
|
-
* [#103](https://github.com/intridea/hashie/pull/103): Fixed support for Hashie::Dash properties that end in bang - [@thedavemarshall](https://github.com/thedavemarshall).
|
105
|
-
* [89](https://github.com/intridea/hashie/issues/89): Do not respond to every method with suffix in Hashie::Mash, fixes Rails strong_parameters - [@Maxim-Filimonov](https://github.com/Maxim-Filimonov).
|
106
|
-
* [#110](https://github.com/intridea/hashie/pull/110): Correctly use Hash#default from Mash#method_missing - [@ryansouza](https://github.com/ryansouza).
|
107
|
-
* [#120](https://github.com/intridea/hashie/pull/120): Pass options to recursive to_hash calls - [@pwillett](https://github.com/pwillett).
|
108
|
-
* [#113](https://github.com/intridea/hashie/issues/113): Fixed Hash#merge with Hashie::Dash - [@spencer1248](https://github.com/spencer1248).
|
109
|
-
* [#99](https://github.com/intridea/hashie/issues/99): Hash#deep_merge raises errors when it encounters integers - [@defsprite](https://github.com/defsprite).
|
110
|
-
* [#133](https://github.com/intridea/hashie/pull/133): Fixed Hash##to_hash with symbolize_keys - [@mhuggins](https://github.com/mhuggins).
|
111
|
-
* [#130](https://github.com/intridea/hashie/pull/130): IndifferentAccess now works without MergeInitializer - [@npj](https://github.com/npj).
|
112
|
-
* [#111](https://github.com/intridea/hashie/issues/111): Trash#translations correctly maps original to translated names - [@artm](https://github.com/artm).
|
113
|
-
* [#129](https://github.com/intridea/hashie/pull/129): Added Trash#permitted_input_keys and inverse_translations - [@artm](https://github.com/artm).
|
114
|
-
|
115
|
-
## 2.0.5
|
116
|
-
|
117
|
-
* [#96](https://github.com/intridea/hashie/pull/96): Make coercion work better with non-symbol keys in Hashie::Mash - [@wapcaplet](https://github.com/wapcaplet).
|
118
|
-
|
119
|
-
## 2.0.4
|
120
|
-
|
121
|
-
* [#04](https://github.com/intridea/hashie/pull/94): Make #fetch method consistent with normal Hash - [@markiz](https://github.com/markiz).
|
122
|
-
* [#90](https://github.com/intridea/hashie/pull/90): Various doc tweaks - [@craiglittle](https://github.com/craiglittle).
|
123
|
-
|
124
|
-
## 2.0.3
|
125
|
-
|
126
|
-
* [#88](https://github.com/intridea/hashie/pull/88): Hashie::Mash.new(abc: true).respond_to?(:abc?) works - [@7even](https://github.com/7even).
|
127
|
-
* [#68](https://github.com/intridea/hashie/pull/68): Fix #replace - [@jimeh](https://github.com/jimeh).
|
128
|
-
|
129
|
-
## 2.0.2
|
130
|
-
|
131
|
-
* [#85](https://github.com/intridea/hashie/pull/85): adding symbolize_keys back to to_hash - [@cromulus](https://github.com/cromulus).
|
132
|
-
|
133
|
-
## 2.0.1
|
134
|
-
|
135
|
-
* [#81](https://github.com/intridea/hashie/pull/81): remove Mash#object_id override - [@matschaffer](https://github.com/matschaffer).
|
136
|
-
* Gem cleanup: removed VERSION, Gemfile.lock [@jch](https://github.com/jch), [@mbleigh](https://github.com/mbleigh).
|
137
|
-
|
138
|
-
## 2.0.0
|
139
|
-
|
140
|
-
* [#72](https://github.com/intridea/hashie/pull/72): Updated gemspec with license info - [@jordimassaguerpla](https://github.com/jordimassaguerpla).
|
141
|
-
* [#27](https://github.com/intridea/hashie/pull/27): Initialized with merge coerces values - [@mattfawcett](https://github.com/mattfawcett).
|
142
|
-
* [#28](https://github.com/intridea/hashie/pull/28): Hashie::Extensions::Coercion coerce_keys takes arguments - [@mattfawcett](https://github.com/mattfawcett).
|
143
|
-
* [#39](https://github.com/intridea/hashie/pull/39): Trash removes translated values on initialization - [@sleverbor](https://github.com/sleverbor).
|
144
|
-
* [#66](https://github.com/intridea/hashie/pull/66): Mash#fetch works with symbol or string keys - [@arthwood](https://github.com/arthwood).
|
145
|
-
* [#49](https://github.com/intridea/hashie/pull/49): Hashie::Hash inherits from ::Hash to avoid ambiguity - [@meh](https://github.com/meh), [@orend](https://github.com/orend).
|
146
|
-
* [#62](https://github.com/intridea/hashie/pull/62): update respond_to? method signature to match ruby core definition - [@dlupu](https://github.com/dlupu).
|
147
|
-
* [#41](https://github.com/intridea/hashie/pull/41): DeepMerge extension - [@nashby](https://github.com/nashby).
|
148
|
-
* [#63](https://github.com/intridea/hashie/pull/63): Dash defaults are dup'ed before assigned - [@ohrite](https://github.com/ohrite).
|
149
|
-
* [#77](https://github.com/intridea/hashie/pull/77): Remove id, type, and object_id as special allowable keys [@jch](https://github.com/jch).
|
150
|
-
* [#78](https://github.com/intridea/hashie/pull/78): Merge and update accepts a block - [@jch](https://github.com/jch).
|
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2009 Intridea, Inc., and Contributors
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
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.
|
@@ -1,710 +0,0 @@
|
|
1
|
-
# Hashie
|
2
|
-
|
3
|
-
[![Join the chat at https://gitter.im/intridea/hashie](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/intridea/hashie?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
4
|
-
|
5
|
-
[![Gem Version](http://img.shields.io/gem/v/hashie.svg)](http://badge.fury.io/rb/hashie)
|
6
|
-
[![Build Status](http://img.shields.io/travis/intridea/hashie.svg)](https://travis-ci.org/intridea/hashie)
|
7
|
-
[![Dependency Status](https://gemnasium.com/intridea/hashie.svg)](https://gemnasium.com/intridea/hashie)
|
8
|
-
[![Code Climate](https://codeclimate.com/github/intridea/hashie.svg)](https://codeclimate.com/github/intridea/hashie)
|
9
|
-
[![Coverage Status](https://codeclimate.com/github/intridea/hashie/badges/coverage.svg)](https://codeclimate.com/github/intridea/hashie)
|
10
|
-
|
11
|
-
Hashie is a growing collection of tools that extend Hashes and make them more useful.
|
12
|
-
|
13
|
-
## Installation
|
14
|
-
|
15
|
-
Hashie is available as a RubyGem:
|
16
|
-
|
17
|
-
```bash
|
18
|
-
$ gem install hashie
|
19
|
-
```
|
20
|
-
|
21
|
-
## Upgrading
|
22
|
-
|
23
|
-
You're reading the documentation for the next release of Hashie, which should be 3.4.3. Please read [UPGRADING](UPGRADING.md) when upgrading from a previous version. The current stable release is [3.4.2](https://github.com/intridea/hashie/blob/v3.4.2/README.md).
|
24
|
-
|
25
|
-
## Hash Extensions
|
26
|
-
|
27
|
-
The library is broken up into a number of atomically includeable Hash extension modules as described below. This provides maximum flexibility for users to mix and match functionality while maintaining feature parity with earlier versions of Hashie.
|
28
|
-
|
29
|
-
Any of the extensions listed below can be mixed into a class by `include`-ing `Hashie::Extensions::ExtensionName`.
|
30
|
-
|
31
|
-
### Coercion
|
32
|
-
|
33
|
-
Coercions allow you to set up "coercion rules" based either on the key or the value type to massage data as it's being inserted into the Hash. Key coercions might be used, for example, in lightweight data modeling applications such as an API client:
|
34
|
-
|
35
|
-
```ruby
|
36
|
-
class Tweet < Hash
|
37
|
-
include Hashie::Extensions::Coercion
|
38
|
-
coerce_key :user, User
|
39
|
-
end
|
40
|
-
|
41
|
-
user_hash = { name: "Bob" }
|
42
|
-
Tweet.new(user: user_hash)
|
43
|
-
# => automatically calls User.coerce(user_hash) or
|
44
|
-
# User.new(user_hash) if that isn't present.
|
45
|
-
```
|
46
|
-
|
47
|
-
Value coercions, on the other hand, will coerce values based on the type of the value being inserted. This is useful if you are trying to build a Hash-like class that is self-propagating.
|
48
|
-
|
49
|
-
```ruby
|
50
|
-
class SpecialHash < Hash
|
51
|
-
include Hashie::Extensions::Coercion
|
52
|
-
coerce_value Hash, SpecialHash
|
53
|
-
|
54
|
-
def initialize(hash = {})
|
55
|
-
super
|
56
|
-
hash.each_pair do |k,v|
|
57
|
-
self[k] = v
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
```
|
62
|
-
|
63
|
-
### Coercing Collections
|
64
|
-
|
65
|
-
```ruby
|
66
|
-
class Tweet < Hash
|
67
|
-
include Hashie::Extensions::Coercion
|
68
|
-
coerce_key :mentions, Array[User]
|
69
|
-
coerce_key :friends, Set[User]
|
70
|
-
end
|
71
|
-
|
72
|
-
user_hash = { name: "Bob" }
|
73
|
-
mentions_hash= [user_hash, user_hash]
|
74
|
-
friends_hash = [user_hash]
|
75
|
-
tweet = Tweet.new(mentions: mentions_hash, friends: friends_hash)
|
76
|
-
# => automatically calls User.coerce(user_hash) or
|
77
|
-
# User.new(user_hash) if that isn't present on each element of the array
|
78
|
-
|
79
|
-
tweet.mentions.map(&:class) # => [User, User]
|
80
|
-
tweet.friends.class # => Set
|
81
|
-
```
|
82
|
-
|
83
|
-
### Coercing Hashes
|
84
|
-
|
85
|
-
```ruby
|
86
|
-
class Relation
|
87
|
-
def initialize(string)
|
88
|
-
@relation = string
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
class Tweet < Hash
|
93
|
-
include Hashie::Extensions::Coercion
|
94
|
-
coerce_key :relations, Hash[User => Relation]
|
95
|
-
end
|
96
|
-
|
97
|
-
user_hash = { name: "Bob" }
|
98
|
-
relations_hash= { user_hash => "father", user_hash => "friend" }
|
99
|
-
tweet = Tweet.new(relations: relations_hash)
|
100
|
-
tweet.relations.map { |k,v| [k.class, v.class] } # => [[User, Relation], [User, Relation]]
|
101
|
-
tweet.relations.class # => Hash
|
102
|
-
|
103
|
-
# => automatically calls User.coerce(user_hash) on each key
|
104
|
-
# and Relation.new on each value since Relation doesn't define the `coerce` class method
|
105
|
-
```
|
106
|
-
|
107
|
-
### Coercing Core Types
|
108
|
-
|
109
|
-
Hashie handles coercion to the following by using standard conversion methods:
|
110
|
-
|
111
|
-
| type | method |
|
112
|
-
|----------|----------|
|
113
|
-
| Integer | `#to_i` |
|
114
|
-
| Float | `#to_f` |
|
115
|
-
| Complex | `#to_c` |
|
116
|
-
| Rational | `#to_r` |
|
117
|
-
| String | `#to_s` |
|
118
|
-
| Symbol | `#to_sym`|
|
119
|
-
|
120
|
-
**Note**: The standard Ruby conversion methods are less strict than you may assume. For example, `:foo.to_i` raises an error but `"foo".to_i` returns 0.
|
121
|
-
|
122
|
-
You can also use coerce from the following supertypes with `coerce_value`:
|
123
|
-
- Integer
|
124
|
-
- Numeric
|
125
|
-
|
126
|
-
Hashie does not have built-in support for coercion boolean values, since Ruby does not have a built-in boolean type or standard method for to a boolean. You can coerce to booleans using a custom proc.
|
127
|
-
|
128
|
-
### Coercion Proc
|
129
|
-
|
130
|
-
You can use a custom coercion proc on either `#coerce_key` or `#coerce_value`. This is useful for coercing to booleans or other simple types without creating a new class and `coerce` method. For example:
|
131
|
-
|
132
|
-
```ruby
|
133
|
-
class Tweet < Hash
|
134
|
-
include Hashie::Extensions::Coercion
|
135
|
-
coerce_key :retweeted, ->(v) do
|
136
|
-
case v
|
137
|
-
when String
|
138
|
-
return !!(v =~ /^(true|t|yes|y|1)$/i)
|
139
|
-
when Numeric
|
140
|
-
return !v.to_i.zero?
|
141
|
-
else
|
142
|
-
return v == true
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
```
|
147
|
-
|
148
|
-
#### A note on circular coercion
|
149
|
-
|
150
|
-
Since `coerce_key` is a class-level method, you cannot have circular coercion without the use of a proc. For example:
|
151
|
-
|
152
|
-
```ruby
|
153
|
-
class CategoryHash < Hash
|
154
|
-
include Hashie::Extensions::Coercion
|
155
|
-
include Hashie::Extensions::MergeInitializer
|
156
|
-
|
157
|
-
coerce_key :products, Array[ProductHash]
|
158
|
-
end
|
159
|
-
|
160
|
-
class ProductHash < Hash
|
161
|
-
include Hashie::Extensions::Coercion
|
162
|
-
include Hashie::Extensions::MergeInitializer
|
163
|
-
|
164
|
-
coerce_key :categories, Array[CategoriesHash]
|
165
|
-
end
|
166
|
-
```
|
167
|
-
|
168
|
-
This will fail with a `NameError` for `CategoryHash::ProductHash` because `ProductHash` is not defined at the point that `coerce_key` is happening for `CategoryHash`.
|
169
|
-
|
170
|
-
To work around this, you can use a coercion proc. For example, you could do:
|
171
|
-
|
172
|
-
```ruby
|
173
|
-
class CategoryHash < Hash
|
174
|
-
# ...
|
175
|
-
coerce_key :products, ->(value) do
|
176
|
-
return value.map { |v| ProductHash.new(v) } if value.respond_to?(:map)
|
177
|
-
|
178
|
-
ProductHash.new(value)
|
179
|
-
end
|
180
|
-
end
|
181
|
-
```
|
182
|
-
|
183
|
-
### KeyConversion
|
184
|
-
|
185
|
-
The KeyConversion extension gives you the convenience methods of `symbolize_keys` and `stringify_keys` along with their bang counterparts. You can also include just stringify or just symbolize with `Hashie::Extensions::StringifyKeys` or `Hashie::Extensions::SymbolizeKeys`.
|
186
|
-
|
187
|
-
Hashie also has a utility method for converting keys on a Hash without a mixin:
|
188
|
-
|
189
|
-
```ruby
|
190
|
-
Hashie.symbolize_keys! hash # => Symbolizes keys of hash.
|
191
|
-
Hashie.symbolize_keys hash # => Returns a copy of hash with keys symbolized.
|
192
|
-
Hashie.stringify_keys! hash # => Stringifies keys of hash.
|
193
|
-
Hashie.stringify_keys hash # => Returns a copy of hash with keys stringified.
|
194
|
-
```
|
195
|
-
|
196
|
-
### MergeInitializer
|
197
|
-
|
198
|
-
The MergeInitializer extension simply makes it possible to initialize a Hash subclass with another Hash, giving you a quick short-hand.
|
199
|
-
|
200
|
-
### MethodAccess
|
201
|
-
|
202
|
-
The MethodAccess extension allows you to quickly build method-based reading, writing, and querying into your Hash descendant. It can also be included as individual modules, i.e. `Hashie::Extensions::MethodReader`, `Hashie::Extensions::MethodWriter` and `Hashie::Extensions::MethodQuery`.
|
203
|
-
|
204
|
-
```ruby
|
205
|
-
class MyHash < Hash
|
206
|
-
include Hashie::Extensions::MethodAccess
|
207
|
-
end
|
208
|
-
|
209
|
-
h = MyHash.new
|
210
|
-
h.abc = 'def'
|
211
|
-
h.abc # => 'def'
|
212
|
-
h.abc? # => true
|
213
|
-
```
|
214
|
-
|
215
|
-
### MethodAccessWithOverride
|
216
|
-
|
217
|
-
The MethodAccessWithOverride extension is like the MethodAccess extension, except that it allows you to override Hash methods. It aliases any overridden method with two leading underscores. To include only this overriding functionality, you can include the single module `Hashie::Extensions::MethodOverridingWriter`.
|
218
|
-
|
219
|
-
```ruby
|
220
|
-
class MyHash < Hash
|
221
|
-
include Hashie::Extensions::MethodAccess
|
222
|
-
end
|
223
|
-
|
224
|
-
class MyOverridingHash < Hash
|
225
|
-
include Hashie::Extensions::MethodAccessWithOverride
|
226
|
-
end
|
227
|
-
|
228
|
-
non_overriding = MyHash.new
|
229
|
-
non_overriding.zip = 'a-dee-doo-dah'
|
230
|
-
non_overriding.zip #=> [[['zip', 'a-dee-doo-dah']]]
|
231
|
-
|
232
|
-
overriding = MyOverridingHash.new
|
233
|
-
overriding.zip = 'a-dee-doo-dah'
|
234
|
-
overriding.zip #=> 'a-dee-doo-dah'
|
235
|
-
overriding.__zip #=> [[['zip', 'a-dee-doo-dah']]]
|
236
|
-
```
|
237
|
-
|
238
|
-
### IndifferentAccess
|
239
|
-
|
240
|
-
This extension can be mixed in to instantly give you indifferent access to your Hash subclass. This works just like the params hash in Rails and other frameworks where whether you provide symbols or strings to access keys, you will get the same results.
|
241
|
-
|
242
|
-
A unique feature of Hashie's IndifferentAccess mixin is that it will inject itself recursively into subhashes *without* reinitializing the hash in question. This means you can safely merge together indifferent and non-indifferent hashes arbitrarily deeply without worrying about whether you'll be able to `hash[:other][:another]` properly.
|
243
|
-
|
244
|
-
### IgnoreUndeclared
|
245
|
-
|
246
|
-
This extension can be mixed in to silently ignore undeclared properties on initialization instead of raising an error. This is useful when using a Trash to capture a subset of a larger hash.
|
247
|
-
|
248
|
-
```ruby
|
249
|
-
class Person < Trash
|
250
|
-
include Hashie::Extensions::IgnoreUndeclared
|
251
|
-
property :first_name
|
252
|
-
property :last_name
|
253
|
-
end
|
254
|
-
|
255
|
-
user_data = {
|
256
|
-
first_name: 'Freddy',
|
257
|
-
last_name: 'Nostrils',
|
258
|
-
email: 'freddy@example.com'
|
259
|
-
}
|
260
|
-
|
261
|
-
p = Person.new(user_data) # 'email' is silently ignored
|
262
|
-
|
263
|
-
p.first_name # => 'Freddy'
|
264
|
-
p.last_name # => 'Nostrils'
|
265
|
-
p.email # => NoMethodError
|
266
|
-
```
|
267
|
-
|
268
|
-
### DeepMerge
|
269
|
-
|
270
|
-
This extension allow you to easily include a recursive merging
|
271
|
-
system to any Hash descendant:
|
272
|
-
|
273
|
-
```ruby
|
274
|
-
class MyHash < Hash
|
275
|
-
include Hashie::Extensions::DeepMerge
|
276
|
-
end
|
277
|
-
|
278
|
-
h1 = MyHash[{ x: { y: [4,5,6] }, z: [7,8,9] }]
|
279
|
-
h2 = MyHash[{ x: { y: [7,8,9] }, z: "xyz" }]
|
280
|
-
|
281
|
-
h1.deep_merge(h2) # => { x: { y: [7, 8, 9] }, z: "xyz" }
|
282
|
-
h2.deep_merge(h1) # => { x: { y: [4, 5, 6] }, z: [7, 8, 9] }
|
283
|
-
```
|
284
|
-
|
285
|
-
Like with Hash#merge in the standard library, a block can be provided to merge values:
|
286
|
-
|
287
|
-
```ruby
|
288
|
-
class MyHash < Hash
|
289
|
-
include Hashie::Extensions::DeepMerge
|
290
|
-
end
|
291
|
-
|
292
|
-
h1 = MyHash[{ a: 100, b: 200, c: { c1: 100 } }]
|
293
|
-
h2 = MyHash[{ b: 250, c: { c1: 200 } }]
|
294
|
-
|
295
|
-
h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
|
296
|
-
# => { a: 100, b: 450, c: { c1: 300 } }
|
297
|
-
```
|
298
|
-
|
299
|
-
|
300
|
-
### DeepFetch
|
301
|
-
|
302
|
-
This extension can be mixed in to provide for safe and concise retrieval of deeply nested hash values. In the event that the requested key does not exist a block can be provided and its value will be returned.
|
303
|
-
|
304
|
-
Though this is a hash extension, it conveniently allows for arrays to be present in the nested structure. This feature makes the extension particularly useful for working with JSON API responses.
|
305
|
-
|
306
|
-
```ruby
|
307
|
-
user = {
|
308
|
-
name: { first: 'Bob', last: 'Boberts' },
|
309
|
-
groups: [
|
310
|
-
{ name: 'Rubyists' },
|
311
|
-
{ name: 'Open source enthusiasts' }
|
312
|
-
]
|
313
|
-
}
|
314
|
-
|
315
|
-
user.extend Hashie::Extensions::DeepFetch
|
316
|
-
|
317
|
-
user.deep_fetch :name, :first # => 'Bob'
|
318
|
-
user.deep_fetch :name, :middle # => 'KeyError: Could not fetch middle'
|
319
|
-
|
320
|
-
# using a default block
|
321
|
-
user.deep_fetch(:name, :middle) { |key| 'default' } # => 'default'
|
322
|
-
|
323
|
-
# a nested array
|
324
|
-
user.deep_fetch :groups, 1, :name # => 'Open source enthusiasts'
|
325
|
-
```
|
326
|
-
|
327
|
-
### DeepFind
|
328
|
-
|
329
|
-
This extension can be mixed in to provide for concise searching for keys within a deeply nested hash.
|
330
|
-
|
331
|
-
It can also search through any Enumerable contained within the hash for objects with the specified key.
|
332
|
-
|
333
|
-
Note: The searches are depth-first, so it is not guaranteed that a shallowly nested value will be found before a deeply nested value.
|
334
|
-
|
335
|
-
```ruby
|
336
|
-
user = {
|
337
|
-
name: { first: 'Bob', last: 'Boberts' },
|
338
|
-
groups: [
|
339
|
-
{ name: 'Rubyists' },
|
340
|
-
{ name: 'Open source enthusiasts' }
|
341
|
-
]
|
342
|
-
}
|
343
|
-
|
344
|
-
user.extend Hashie::Extensions::DeepFind
|
345
|
-
|
346
|
-
user.deep_find(:name) #=> { first: 'Bob', last: 'Boberts' }
|
347
|
-
user.deep_detect(:name) #=> { first: 'Bob', last: 'Boberts' }
|
348
|
-
|
349
|
-
user.deep_find_all(:name) #=> [{ first: 'Bob', last: 'Boberts' }, 'Rubyists', 'Open source enthusiasts']
|
350
|
-
user.deep_select(:name) #=> [{ first: 'Bob', last: 'Boberts' }, 'Rubyists', 'Open source enthusiasts']
|
351
|
-
```
|
352
|
-
|
353
|
-
### DeepLocate
|
354
|
-
|
355
|
-
This extension can be mixed in to provide a depth first search based search for enumerables matching a given comparator callable.
|
356
|
-
|
357
|
-
It returns all enumerables which contain at least one element, for which the given comparator returns ```true```.
|
358
|
-
|
359
|
-
Because the container objects are returned, the result elements can be modified in place. This way, one can perform modifications on deeply nested hashes without the need to know the exact paths.
|
360
|
-
|
361
|
-
```ruby
|
362
|
-
|
363
|
-
books = [
|
364
|
-
{
|
365
|
-
title: "Ruby for beginners",
|
366
|
-
pages: 120
|
367
|
-
},
|
368
|
-
{
|
369
|
-
title: "CSS for intermediates",
|
370
|
-
pages: 80
|
371
|
-
},
|
372
|
-
{
|
373
|
-
title: "Collection of ruby books",
|
374
|
-
books: [
|
375
|
-
{
|
376
|
-
title: "Ruby for the rest of us",
|
377
|
-
pages: 576
|
378
|
-
}
|
379
|
-
]
|
380
|
-
}
|
381
|
-
]
|
382
|
-
|
383
|
-
books.extend(Hashie::Extensions::DeepLocate)
|
384
|
-
|
385
|
-
# for ruby 1.9 leave *no* space between the lambda rocket and the braces
|
386
|
-
# http://ruby-journal.com/becareful-with-space-in-lambda-hash-rocket-syntax-between-ruby-1-dot-9-and-2-dot-0/
|
387
|
-
|
388
|
-
books.deep_locate -> (key, value, object) { key == :title && value.include?("Ruby") }
|
389
|
-
# => [{:title=>"Ruby for beginners", :pages=>120}, {:title=>"Ruby for the rest of us", :pages=>576}]
|
390
|
-
|
391
|
-
books.deep_locate -> (key, value, object) { key == :pages && value <= 120 }
|
392
|
-
# => [{:title=>"Ruby for beginners", :pages=>120}, {:title=>"CSS for intermediates", :pages=>80}]
|
393
|
-
```
|
394
|
-
|
395
|
-
## Mash
|
396
|
-
|
397
|
-
Mash is an extended Hash that gives simple pseudo-object functionality that can be built from hashes and easily extended. It is intended to give the user easier access to the objects within the Mash through a property-like syntax, while still retaining all Hash functionality.
|
398
|
-
|
399
|
-
### Example:
|
400
|
-
|
401
|
-
```ruby
|
402
|
-
mash = Hashie::Mash.new
|
403
|
-
mash.name? # => false
|
404
|
-
mash.name # => nil
|
405
|
-
mash.name = "My Mash"
|
406
|
-
mash.name # => "My Mash"
|
407
|
-
mash.name? # => true
|
408
|
-
mash.inspect # => <Hashie::Mash name="My Mash">
|
409
|
-
|
410
|
-
mash = Hashie::Mash.new
|
411
|
-
# use bang methods for multi-level assignment
|
412
|
-
mash.author!.name = "Michael Bleigh"
|
413
|
-
mash.author # => <Hashie::Mash name="Michael Bleigh">
|
414
|
-
|
415
|
-
mash = Hashie::Mash.new
|
416
|
-
# use under-bang methods for multi-level testing
|
417
|
-
mash.author_.name? # => false
|
418
|
-
mash.inspect # => <Hashie::Mash>
|
419
|
-
```
|
420
|
-
|
421
|
-
**Note:** The `?` method will return false if a key has been set to false or nil. In order to check if a key has been set at all, use the `mash.key?('some_key')` method instead.
|
422
|
-
|
423
|
-
Please note that a Mash will not override methods through the use of the property-like syntax. This can lead to confusion if you expect to be able to access a Mash value through the property-like syntax for a key that conflicts with a method name. However, it protects users of your library from the unexpected behavior of those methods being overridden behind the scenes.
|
424
|
-
|
425
|
-
### Example:
|
426
|
-
|
427
|
-
```ruby
|
428
|
-
mash = Hashie::Mash.new
|
429
|
-
mash.name = "My Mash"
|
430
|
-
mash.zip = "Method Override?"
|
431
|
-
mash.zip # => [[["name", "My Mash"]], [["zip", "Method Override?"]]]
|
432
|
-
```
|
433
|
-
|
434
|
-
Mash allows you also to transform any files into a Mash objects.
|
435
|
-
|
436
|
-
### Example:
|
437
|
-
|
438
|
-
```yml
|
439
|
-
#/etc/config/settings/twitter.yml
|
440
|
-
development:
|
441
|
-
api_key: 'api_key'
|
442
|
-
production:
|
443
|
-
api_key: <%= ENV['API_KEY'] %> #let's say that ENV['API_KEY'] is set to 'abcd'
|
444
|
-
```
|
445
|
-
|
446
|
-
```ruby
|
447
|
-
mash = Mash.load('settings/twitter.yml')
|
448
|
-
mash.development.api_key # => 'localhost'
|
449
|
-
mash.development.api_key = "foo" # => <# RuntimeError can't modify frozen ...>
|
450
|
-
mash.development.api_key? # => true
|
451
|
-
```
|
452
|
-
|
453
|
-
You can access a Mash from another class:
|
454
|
-
|
455
|
-
```ruby
|
456
|
-
mash = Mash.load('settings/twitter.yml')[ENV['RACK_ENV']]
|
457
|
-
Twitter.extend mash.to_module # NOTE: if you want another name than settings, call: to_module('my_settings')
|
458
|
-
Twitter.settings.api_key # => 'abcd'
|
459
|
-
```
|
460
|
-
|
461
|
-
You can use another parser (by default: YamlErbParser):
|
462
|
-
|
463
|
-
```
|
464
|
-
#/etc/data/user.csv
|
465
|
-
id | name | lastname
|
466
|
-
---|------------- | -------------
|
467
|
-
1 |John | Doe
|
468
|
-
2 |Laurent | Garnier
|
469
|
-
```
|
470
|
-
|
471
|
-
```ruby
|
472
|
-
mash = Mash.load('data/user.csv', parser: MyCustomCsvParser)
|
473
|
-
# => { 1 => { name: 'John', lastname: 'Doe'}, 2 => { name: 'Laurent', lastname: 'Garnier' } }
|
474
|
-
mash[1] #=> { name: 'John', lastname: 'Doe' }
|
475
|
-
```
|
476
|
-
|
477
|
-
### Mash Extension: SafeAssignment
|
478
|
-
|
479
|
-
This extension can be mixed into a Mash to guard the attempted overwriting of methods by property setters. When mixed in, the Mash will raise an `ArgumentError` if you attempt to write a property with the same name as an existing method.
|
480
|
-
|
481
|
-
#### Example:
|
482
|
-
|
483
|
-
```ruby
|
484
|
-
class SafeMash < ::Hashie::Mash
|
485
|
-
include Hashie::Extensions::Mash::SafeAssignment
|
486
|
-
end
|
487
|
-
|
488
|
-
safe_mash = SafeMash.new
|
489
|
-
safe_mash.zip = 'Test' # => ArgumentError
|
490
|
-
safe_mash[:zip] = 'test' # => still ArgumentError
|
491
|
-
```
|
492
|
-
|
493
|
-
## Dash
|
494
|
-
|
495
|
-
Dash is an extended Hash that has a discrete set of defined properties and only those properties may be set on the hash. Additionally, you can set defaults for each property. You can also flag a property as required. Required properties will raise an exception if unset. Another option is message for required properties, which allow you to add custom messages for required property.
|
496
|
-
|
497
|
-
You can also conditionally require certain properties by passing a Proc or Symbol. If a Proc is provided, it will be run in the context of the Dash instance. If a Symbol is provided, the value returned for the property or method of the same name will be evaluated. The property will be required if the result of the conditional is truthy.
|
498
|
-
|
499
|
-
### Example:
|
500
|
-
|
501
|
-
```ruby
|
502
|
-
class Person < Hashie::Dash
|
503
|
-
property :name, required: true
|
504
|
-
property :age, required: true, message: 'must be set.'
|
505
|
-
property :email
|
506
|
-
property :phone, required: -> { email.nil? }, message: 'is required if email is not set.'
|
507
|
-
property :pants, required: :weekday?, message: 'are only required on weekdays.'
|
508
|
-
property :occupation, default: 'Rubyist'
|
509
|
-
|
510
|
-
def weekday?
|
511
|
-
[ Time.now.saturday?, Time.now.sunday? ].none?
|
512
|
-
end
|
513
|
-
end
|
514
|
-
|
515
|
-
p = Person.new # => ArgumentError: The property 'name' is required for this Dash.
|
516
|
-
p = Person.new(name: 'Bob') # => ArgumentError: The property 'age' must be set.
|
517
|
-
|
518
|
-
p = Person.new(name: "Bob", age: 18)
|
519
|
-
p.name # => 'Bob'
|
520
|
-
p.name = nil # => ArgumentError: The property 'name' is required for this Dash.
|
521
|
-
p.age # => 18
|
522
|
-
p.age = nil # => ArgumentError: The property 'age' must be set.
|
523
|
-
p.email = 'abc@def.com'
|
524
|
-
p.occupation # => 'Rubyist'
|
525
|
-
p.email # => 'abc@def.com'
|
526
|
-
p[:awesome] # => NoMethodError
|
527
|
-
p[:occupation] # => 'Rubyist'
|
528
|
-
p.update_attributes!(name: 'Trudy', occupation: 'Evil')
|
529
|
-
p.occupation # => 'Evil'
|
530
|
-
p.name # => 'Trudy'
|
531
|
-
p.update_attributes!(occupation: nil)
|
532
|
-
p.occupation # => 'Rubyist'
|
533
|
-
```
|
534
|
-
|
535
|
-
Properties defined as symbols are not the same thing as properties defined as strings.
|
536
|
-
|
537
|
-
### Example:
|
538
|
-
|
539
|
-
```ruby
|
540
|
-
class Tricky < Hashie::Dash
|
541
|
-
property :trick
|
542
|
-
property 'trick'
|
543
|
-
end
|
544
|
-
|
545
|
-
p = Tricky.new(trick: 'one', 'trick' => 'two')
|
546
|
-
p.trick # => 'one', always symbol version
|
547
|
-
p[:trick] # => 'one'
|
548
|
-
p['trick'] # => 'two'
|
549
|
-
```
|
550
|
-
|
551
|
-
Note that accessing a property as a method always uses the symbol version.
|
552
|
-
|
553
|
-
```ruby
|
554
|
-
class Tricky < Hashie::Dash
|
555
|
-
property 'trick'
|
556
|
-
end
|
557
|
-
|
558
|
-
p = Tricky.new('trick' => 'two')
|
559
|
-
p.trick # => NoMethodError
|
560
|
-
```
|
561
|
-
|
562
|
-
### Dash Extension: PropertyTranslation
|
563
|
-
|
564
|
-
The `Hashie::Extensions::Dash::PropertyTranslation` mixin extends a Dash with
|
565
|
-
the ability to remap keys from a source hash.
|
566
|
-
|
567
|
-
### Example from inconsistent APIs
|
568
|
-
|
569
|
-
Property translation is useful when you need to read data from another
|
570
|
-
application -- such as a Java API -- where the keys are named differently from
|
571
|
-
Ruby conventions.
|
572
|
-
|
573
|
-
```ruby
|
574
|
-
class PersonHash < Hashie::Dash
|
575
|
-
include Hashie::Extensions::Dash::PropertyTranslation
|
576
|
-
|
577
|
-
property :first_name, from: :firstName
|
578
|
-
property :last_name, from: :lastName
|
579
|
-
property :first_name, from: :f_name
|
580
|
-
property :last_name, from: :l_name
|
581
|
-
end
|
582
|
-
|
583
|
-
person = PersonHash.new(firstName: 'Michael', l_name: 'Bleigh')
|
584
|
-
person[:first_name] #=> 'Michael'
|
585
|
-
person[:last_name] #=> 'Bleigh
|
586
|
-
```
|
587
|
-
|
588
|
-
### Example using translation lambdas
|
589
|
-
|
590
|
-
You can also use a lambda to translate the value. This is particularly useful
|
591
|
-
when you want to ensure the type of data you're wrapping.
|
592
|
-
|
593
|
-
```ruby
|
594
|
-
class DataModelHash < Hashie::Dash
|
595
|
-
include Hashie::Extensions::Dash::PropertyTranslation
|
596
|
-
|
597
|
-
property :id, transform_with: ->(value) { value.to_i }
|
598
|
-
property :created_at, from: :created, with: ->(value) { Time.parse(value) }
|
599
|
-
end
|
600
|
-
|
601
|
-
model = DataModelHash.new(id: '123', created: '2014-04-25 22:35:28')
|
602
|
-
model.id.class #=> Fixnum
|
603
|
-
model.created_at.class #=> Time
|
604
|
-
```
|
605
|
-
|
606
|
-
### Mash and Rails 4 Strong Parameters
|
607
|
-
|
608
|
-
To enable compatibility with Rails 4 use the [hashie-forbidden_attributes](https://github.com/Maxim-Filimonov/hashie-forbidden_attributes) gem.
|
609
|
-
|
610
|
-
## Trash
|
611
|
-
|
612
|
-
A Trash is a Dash that allows you to translate keys on initialization. It mixes
|
613
|
-
in the PropertyTranslation mixin by default and is used like so:
|
614
|
-
|
615
|
-
```ruby
|
616
|
-
class Person < Hashie::Trash
|
617
|
-
property :first_name, from: :firstName
|
618
|
-
end
|
619
|
-
```
|
620
|
-
|
621
|
-
This will automatically translate the <tt>firstName</tt> key to <tt>first_name</tt>
|
622
|
-
when it is initialized using a hash such as through:
|
623
|
-
|
624
|
-
```ruby
|
625
|
-
Person.new(firstName: 'Bob')
|
626
|
-
```
|
627
|
-
|
628
|
-
Trash also supports translations using lambda, this could be useful when dealing with external API's. You can use it in this way:
|
629
|
-
|
630
|
-
```ruby
|
631
|
-
class Result < Hashie::Trash
|
632
|
-
property :id, transform_with: lambda { |v| v.to_i }
|
633
|
-
property :created_at, from: :creation_date, with: lambda { |v| Time.parse(v) }
|
634
|
-
end
|
635
|
-
```
|
636
|
-
|
637
|
-
this will produce the following
|
638
|
-
|
639
|
-
```ruby
|
640
|
-
result = Result.new(id: '123', creation_date: '2012-03-30 17:23:28')
|
641
|
-
result.id.class # => Fixnum
|
642
|
-
result.created_at.class # => Time
|
643
|
-
```
|
644
|
-
|
645
|
-
## Clash
|
646
|
-
|
647
|
-
Clash is a Chainable Lazy Hash that allows you to easily construct complex hashes using method notation chaining. This will allow you to use a more action-oriented approach to building options hashes.
|
648
|
-
|
649
|
-
Essentially, a Clash is a generalized way to provide much of the same kind of "chainability" that libraries like Arel or Rails 2.x's named_scopes provide.
|
650
|
-
|
651
|
-
### Example:
|
652
|
-
|
653
|
-
```ruby
|
654
|
-
c = Hashie::Clash.new
|
655
|
-
c.where(abc: 'def').order(:created_at)
|
656
|
-
c # => { where: { abc: 'def' }, order: :created_at }
|
657
|
-
|
658
|
-
# You can also use bang notation to chain into sub-hashes,
|
659
|
-
# jumping back up the chain with _end!
|
660
|
-
c = Hashie::Clash.new
|
661
|
-
c.where!.abc('def').ghi(123)._end!.order(:created_at)
|
662
|
-
c # => { where: { abc: 'def', ghi: 123 }, order: :created_at }
|
663
|
-
|
664
|
-
# Multiple hashes are merged automatically
|
665
|
-
c = Hashie::Clash.new
|
666
|
-
c.where(abc: 'def').where(hgi: 123)
|
667
|
-
c # => { where: { abc: 'def', hgi: 123 } }
|
668
|
-
```
|
669
|
-
|
670
|
-
## Rash
|
671
|
-
|
672
|
-
Rash is a Hash whose keys can be Regexps or Ranges, which will map many input keys to a value.
|
673
|
-
|
674
|
-
A good use case for the Rash is an URL router for a web framework, where URLs need to be mapped to actions; the Rash's keys match URL patterns, while the values call the action which handles the URL.
|
675
|
-
|
676
|
-
If the Rash's value is a `proc`, the `proc` will be automatically called with the regexp's MatchData (matched groups) as a block argument.
|
677
|
-
|
678
|
-
### Example:
|
679
|
-
|
680
|
-
```ruby
|
681
|
-
|
682
|
-
# Mapping names to appropriate greetings
|
683
|
-
greeting = Hashie::Rash.new( /^Mr./ => "Hello sir!", /^Mrs./ => "Evening, madame." )
|
684
|
-
greeting["Mr. Steve Austin"] # => "Hello sir!"
|
685
|
-
greeting["Mrs. Steve Austin"] # => "Evening, madame."
|
686
|
-
|
687
|
-
# Mapping statements to saucy retorts
|
688
|
-
mapper = Hashie::Rash.new(
|
689
|
-
/I like (.+)/ => proc { |m| "Who DOESN'T like #{m[1]}?!" },
|
690
|
-
/Get off my (.+)!/ => proc { |m| "Forget your #{m[1]}, old man!" }
|
691
|
-
)
|
692
|
-
mapper["I like traffic lights"] # => "Who DOESN'T like traffic lights?!"
|
693
|
-
mapper["Get off my lawn!"] # => "Forget your lawn, old man!"
|
694
|
-
```
|
695
|
-
|
696
|
-
### Auto-optimized
|
697
|
-
|
698
|
-
**Note:** The Rash is automatically optimized every 500 accesses (which means that it sorts the list of Regexps, putting the most frequently matched ones at the beginning).
|
699
|
-
|
700
|
-
If this value is too low or too high for your needs, you can tune it by setting: `rash.optimize_every = n`.
|
701
|
-
|
702
|
-
## Contributing
|
703
|
-
|
704
|
-
See [CONTRIBUTING.md](CONTRIBUTING.md)
|
705
|
-
|
706
|
-
## Copyright
|
707
|
-
|
708
|
-
Copyright (c) 2009-2014 Intridea, Inc. (http://intridea.com/) and [contributors](https://github.com/intridea/hashie/graphs/contributors).
|
709
|
-
|
710
|
-
MIT License. See [LICENSE](LICENSE) for details.
|
@@ -1,35 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: hashie 3.4.3 ruby lib
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "hashie"
|
6
|
-
s.version = "3.4.3"
|
7
|
-
|
8
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
9
|
-
s.require_paths = ["lib"]
|
10
|
-
s.authors = ["Michael Bleigh", "Jerry Cheung"]
|
11
|
-
s.date = "2015-07-30"
|
12
|
-
s.description = "Hashie is a collection of classes and mixins that make hashes more powerful."
|
13
|
-
s.email = ["michael@intridea.com", "jollyjerry@gmail.com"]
|
14
|
-
s.files = [".yardopts", "CHANGELOG.md", "CONTRIBUTING.md", "LICENSE", "README.md", "Rakefile", "UPGRADING.md", "hashie.gemspec", "lib/hashie.rb", "lib/hashie/clash.rb", "lib/hashie/dash.rb", "lib/hashie/extensions/coercion.rb", "lib/hashie/extensions/dash/indifferent_access.rb", "lib/hashie/extensions/dash/property_translation.rb", "lib/hashie/extensions/deep_fetch.rb", "lib/hashie/extensions/deep_find.rb", "lib/hashie/extensions/deep_locate.rb", "lib/hashie/extensions/deep_merge.rb", "lib/hashie/extensions/ignore_undeclared.rb", "lib/hashie/extensions/indifferent_access.rb", "lib/hashie/extensions/key_conversion.rb", "lib/hashie/extensions/mash/safe_assignment.rb", "lib/hashie/extensions/merge_initializer.rb", "lib/hashie/extensions/method_access.rb", "lib/hashie/extensions/parsers/yaml_erb_parser.rb", "lib/hashie/extensions/pretty_inspect.rb", "lib/hashie/extensions/stringify_keys.rb", "lib/hashie/extensions/symbolize_keys.rb", "lib/hashie/hash.rb", "lib/hashie/mash.rb", "lib/hashie/rash.rb", "lib/hashie/trash.rb", "lib/hashie/version.rb", "spec/hashie/clash_spec.rb", "spec/hashie/dash_spec.rb", "spec/hashie/extensions/autoload_spec.rb", "spec/hashie/extensions/coercion_spec.rb", "spec/hashie/extensions/dash/indifferent_access_spec.rb", "spec/hashie/extensions/deep_fetch_spec.rb", "spec/hashie/extensions/deep_find_spec.rb", "spec/hashie/extensions/deep_locate_spec.rb", "spec/hashie/extensions/deep_merge_spec.rb", "spec/hashie/extensions/ignore_undeclared_spec.rb", "spec/hashie/extensions/indifferent_access_spec.rb", "spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb", "spec/hashie/extensions/key_conversion_spec.rb", "spec/hashie/extensions/mash/safe_assignment_spec.rb", "spec/hashie/extensions/merge_initializer_spec.rb", "spec/hashie/extensions/method_access_spec.rb", "spec/hashie/extensions/stringify_keys_spec.rb", "spec/hashie/extensions/symbolize_keys_spec.rb", "spec/hashie/hash_spec.rb", "spec/hashie/mash_spec.rb", "spec/hashie/parsers/yaml_erb_parser_spec.rb", "spec/hashie/rash_spec.rb", "spec/hashie/trash_spec.rb", "spec/hashie/version_spec.rb", "spec/spec_helper.rb", "spec/support/module_context.rb", "spec/support/ruby_version.rb"]
|
15
|
-
s.homepage = "https://github.com/intridea/hashie"
|
16
|
-
s.licenses = ["MIT"]
|
17
|
-
s.rubygems_version = "2.4.5"
|
18
|
-
s.summary = "Your friendly neighborhood hash library."
|
19
|
-
s.test_files = ["spec/hashie/extensions/dash/indifferent_access_spec.rb", "spec/hashie/extensions/mash/safe_assignment_spec.rb", "spec/hashie/extensions/autoload_spec.rb", "spec/hashie/extensions/coercion_spec.rb", "spec/hashie/extensions/deep_fetch_spec.rb", "spec/hashie/extensions/deep_find_spec.rb", "spec/hashie/extensions/deep_locate_spec.rb", "spec/hashie/extensions/deep_merge_spec.rb", "spec/hashie/extensions/ignore_undeclared_spec.rb", "spec/hashie/extensions/indifferent_access_spec.rb", "spec/hashie/extensions/indifferent_access_with_rails_hwia_spec.rb", "spec/hashie/extensions/key_conversion_spec.rb", "spec/hashie/extensions/merge_initializer_spec.rb", "spec/hashie/extensions/method_access_spec.rb", "spec/hashie/extensions/stringify_keys_spec.rb", "spec/hashie/extensions/symbolize_keys_spec.rb", "spec/hashie/parsers/yaml_erb_parser_spec.rb", "spec/hashie/clash_spec.rb", "spec/hashie/dash_spec.rb", "spec/hashie/hash_spec.rb", "spec/hashie/mash_spec.rb", "spec/hashie/rash_spec.rb", "spec/hashie/trash_spec.rb", "spec/hashie/version_spec.rb", "spec/support/module_context.rb", "spec/support/ruby_version.rb", "spec/spec_helper.rb"]
|
20
|
-
|
21
|
-
if s.respond_to? :specification_version then
|
22
|
-
s.specification_version = 4
|
23
|
-
|
24
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
-
s.add_development_dependency(%q<rake>, [">= 0"])
|
26
|
-
s.add_development_dependency(%q<rspec>, ["~> 3.0"])
|
27
|
-
else
|
28
|
-
s.add_dependency(%q<rake>, [">= 0"])
|
29
|
-
s.add_dependency(%q<rspec>, ["~> 3.0"])
|
30
|
-
end
|
31
|
-
else
|
32
|
-
s.add_dependency(%q<rake>, [">= 0"])
|
33
|
-
s.add_dependency(%q<rspec>, ["~> 3.0"])
|
34
|
-
end
|
35
|
-
end
|