rna 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mash.rb +225 -0
- data/lib/node.rb +6 -0
- data/lib/rna/dsl.rb +2 -2
- data/lib/rna/version.rb +1 -1
- data/spec/lib/rna_spec.rb +11 -0
- metadata +2 -1
data/lib/mash.rb
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
# Copyright (c) 2009 Dan Kubb
|
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.
|
21
|
+
|
22
|
+
# ---
|
23
|
+
# ---
|
24
|
+
|
25
|
+
# Some portions of blank.rb and mash.rb are verbatim copies of software
|
26
|
+
# licensed under the MIT license. That license is included below:
|
27
|
+
|
28
|
+
# Copyright (c) 2005-2008 David Heinemeier Hansson
|
29
|
+
|
30
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
31
|
+
# a copy of this software and associated documentation files (the
|
32
|
+
# "Software"), to deal in the Software without restriction, including
|
33
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
34
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
35
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
36
|
+
# the following conditions:
|
37
|
+
|
38
|
+
# The above copyright notice and this permission notice shall be
|
39
|
+
# included in all copies or substantial portions of the Software.
|
40
|
+
|
41
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
42
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
43
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
44
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
45
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
46
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
47
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
48
|
+
|
49
|
+
# This class has dubious semantics and we only have it so that people can write
|
50
|
+
# params[:key] instead of params['key'].
|
51
|
+
class Mash < Hash
|
52
|
+
|
53
|
+
# @param constructor<Object>
|
54
|
+
# The default value for the mash. Defaults to an empty hash.
|
55
|
+
#
|
56
|
+
# @details [Alternatives]
|
57
|
+
# If constructor is a Hash, a new mash will be created based on the keys of
|
58
|
+
# the hash and no default value will be set.
|
59
|
+
def initialize(constructor = {})
|
60
|
+
if constructor.is_a?(Hash)
|
61
|
+
super()
|
62
|
+
update(constructor)
|
63
|
+
else
|
64
|
+
super(constructor)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# @param orig<Object> Mash being copied
|
69
|
+
#
|
70
|
+
# @return [Object] A new copied Mash
|
71
|
+
def initialize_copy(orig)
|
72
|
+
super
|
73
|
+
# Handle nested values
|
74
|
+
each do |k,v|
|
75
|
+
if v.kind_of?(Mash) || v.is_a?(Array)
|
76
|
+
self[k] = v.dup
|
77
|
+
end
|
78
|
+
end
|
79
|
+
self
|
80
|
+
end
|
81
|
+
|
82
|
+
# @param key<Object> The default value for the mash. Defaults to nil.
|
83
|
+
#
|
84
|
+
# @details [Alternatives]
|
85
|
+
# If key is a Symbol and it is a key in the mash, then the default value will
|
86
|
+
# be set to the value matching the key.
|
87
|
+
def default(key = nil)
|
88
|
+
if key.is_a?(Symbol) && include?(key = key.to_s)
|
89
|
+
self[key]
|
90
|
+
else
|
91
|
+
super
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
|
96
|
+
alias_method :regular_update, :update unless method_defined?(:regular_update)
|
97
|
+
|
98
|
+
# @param key<Object> The key to set.
|
99
|
+
# @param value<Object>
|
100
|
+
# The value to set the key to.
|
101
|
+
#
|
102
|
+
# @see Mash#convert_key
|
103
|
+
# @see Mash#convert_value
|
104
|
+
def []=(key, value)
|
105
|
+
regular_writer(convert_key(key), convert_value(value))
|
106
|
+
end
|
107
|
+
|
108
|
+
# @param other_hash<Hash>
|
109
|
+
# A hash to update values in the mash with. The keys and the values will be
|
110
|
+
# converted to Mash format.
|
111
|
+
#
|
112
|
+
# @return [Mash] The updated mash.
|
113
|
+
def update(other_hash)
|
114
|
+
other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
|
115
|
+
self
|
116
|
+
end
|
117
|
+
|
118
|
+
alias_method :merge!, :update
|
119
|
+
|
120
|
+
# @param key<Object> The key to check for. This will be run through convert_key.
|
121
|
+
#
|
122
|
+
# @return [Boolean] True if the key exists in the mash.
|
123
|
+
def key?(key)
|
124
|
+
super(convert_key(key))
|
125
|
+
end
|
126
|
+
|
127
|
+
# def include? def has_key? def member?
|
128
|
+
alias_method :include?, :key?
|
129
|
+
alias_method :has_key?, :key?
|
130
|
+
alias_method :member?, :key?
|
131
|
+
|
132
|
+
# @param key<Object> The key to fetch. This will be run through convert_key.
|
133
|
+
# @param *extras<Array> Default value.
|
134
|
+
#
|
135
|
+
# @return [Object] The value at key or the default value.
|
136
|
+
def fetch(key, *extras)
|
137
|
+
super(convert_key(key), *extras)
|
138
|
+
end
|
139
|
+
|
140
|
+
# @param *indices<Array>
|
141
|
+
# The keys to retrieve values for. These will be run through +convert_key+.
|
142
|
+
#
|
143
|
+
# @return [Array] The values at each of the provided keys
|
144
|
+
def values_at(*indices)
|
145
|
+
indices.collect {|key| self[convert_key(key)]}
|
146
|
+
end
|
147
|
+
|
148
|
+
# @param hash<Hash> The hash to merge with the mash.
|
149
|
+
#
|
150
|
+
# @return [Mash] A new mash with the hash values merged in.
|
151
|
+
def merge(hash)
|
152
|
+
self.dup.update(hash)
|
153
|
+
end
|
154
|
+
|
155
|
+
# @param key<Object>
|
156
|
+
# The key to delete from the mash.\
|
157
|
+
def delete(key)
|
158
|
+
super(convert_key(key))
|
159
|
+
end
|
160
|
+
|
161
|
+
# @param *rejected<Array[(String, Symbol)] The mash keys to exclude.
|
162
|
+
#
|
163
|
+
# @return [Mash] A new mash without the selected keys.
|
164
|
+
#
|
165
|
+
# @example
|
166
|
+
# { :one => 1, :two => 2, :three => 3 }.except(:one)
|
167
|
+
# #=> { "two" => 2, "three" => 3 }
|
168
|
+
def except(*keys)
|
169
|
+
super(*keys.map {|k| convert_key(k)})
|
170
|
+
end
|
171
|
+
|
172
|
+
# Used to provide the same interface as Hash.
|
173
|
+
#
|
174
|
+
# @return [Mash] This mash unchanged.
|
175
|
+
def stringify_keys!; self end
|
176
|
+
|
177
|
+
# @return [Hash] The mash as a Hash with symbolized keys.
|
178
|
+
def symbolize_keys
|
179
|
+
h = Hash.new(default)
|
180
|
+
each { |key, val| h[key.to_sym] = val }
|
181
|
+
h
|
182
|
+
end
|
183
|
+
|
184
|
+
# @return [Hash] The mash as a Hash with string keys.
|
185
|
+
def to_hash
|
186
|
+
Hash.new(default).merge(self)
|
187
|
+
end
|
188
|
+
|
189
|
+
# @return [Mash] Convert a Hash into a Mash
|
190
|
+
# The input Hash's default value is maintained
|
191
|
+
def self.from_hash(hash)
|
192
|
+
mash = Mash.new(hash)
|
193
|
+
mash.default = hash.default
|
194
|
+
mash
|
195
|
+
end
|
196
|
+
|
197
|
+
protected
|
198
|
+
# @param key<Object> The key to convert.
|
199
|
+
#
|
200
|
+
# @param [Object]
|
201
|
+
# The converted key. If the key was a symbol, it will be converted to a
|
202
|
+
# string.
|
203
|
+
#
|
204
|
+
# @api private
|
205
|
+
def convert_key(key)
|
206
|
+
key.kind_of?(Symbol) ? key.to_s : key
|
207
|
+
end
|
208
|
+
|
209
|
+
# @param value<Object> The value to convert.
|
210
|
+
#
|
211
|
+
# @return [Object]
|
212
|
+
# The converted value. A Hash or an Array of hashes, will be converted to
|
213
|
+
# their Mash equivalents.
|
214
|
+
#
|
215
|
+
# @api private
|
216
|
+
def convert_value(value)
|
217
|
+
if value.class == Hash
|
218
|
+
Mash.from_hash(value)
|
219
|
+
elsif value.is_a?(Array)
|
220
|
+
value.collect { |e| convert_value(e) }
|
221
|
+
else
|
222
|
+
value
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
data/lib/node.rb
CHANGED
data/lib/rna/dsl.rb
CHANGED
@@ -125,13 +125,13 @@ module Rna
|
|
125
125
|
@dsl = eval "self", @block.binding
|
126
126
|
instance_eval(&@block)
|
127
127
|
end
|
128
|
-
@data[:attributes].merge!(set.
|
128
|
+
@data[:attributes].merge!(set.to_mash)
|
129
129
|
@data
|
130
130
|
end
|
131
131
|
|
132
132
|
# http://www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation
|
133
133
|
def settings
|
134
|
-
@dsl.settings_node.
|
134
|
+
@dsl.settings_node.to_mash
|
135
135
|
end
|
136
136
|
|
137
137
|
def node
|
data/lib/rna/version.rb
CHANGED
data/spec/lib/rna_spec.rb
CHANGED
@@ -182,6 +182,17 @@ describe Node do
|
|
182
182
|
hash[:a][:b][:c].should == 2
|
183
183
|
end
|
184
184
|
|
185
|
+
it "should convert to a mash" do
|
186
|
+
@node[:a][:b][:c] = 2
|
187
|
+
@node[:a][:b][:c].should == 2
|
188
|
+
hash = @node.to_mash
|
189
|
+
# uncomment to see hash structure
|
190
|
+
# pp hash
|
191
|
+
# pp hash[:a][:b]
|
192
|
+
hash[:a][:b][:c].should == 2
|
193
|
+
hash['a'][:b]['c'].should == 2
|
194
|
+
end
|
195
|
+
|
185
196
|
it "should raise error if namespace is a non-node value and you try to access a value below that" do
|
186
197
|
@node[:a][:b] = 4
|
187
198
|
# this errors because it will run: 4[:c]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rna
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -195,6 +195,7 @@ files:
|
|
195
195
|
- lib/files/Guardfile
|
196
196
|
- lib/files/rna.rb
|
197
197
|
- lib/files/s3.yml
|
198
|
+
- lib/mash.rb
|
198
199
|
- lib/node.rb
|
199
200
|
- lib/rna.rb
|
200
201
|
- lib/rna/cli.rb
|