spice 1.0.0.rc → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +0 -14
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/TODO.md +2 -1
- data/lib/spice.rb +47 -72
- data/lib/spice/authentication.rb +23 -45
- data/lib/spice/base.rb +104 -0
- data/lib/spice/client.rb +8 -48
- data/lib/spice/config.rb +37 -19
- data/lib/spice/connection.rb +33 -117
- data/lib/spice/connection/clients.rb +26 -6
- data/lib/spice/connection/cookbooks.rb +19 -14
- data/lib/spice/connection/data_bags.rb +31 -20
- data/lib/spice/connection/environments.rb +6 -7
- data/lib/spice/connection/nodes.rb +23 -6
- data/lib/spice/connection/roles.rb +5 -5
- data/lib/spice/connection/search.rb +12 -12
- data/lib/spice/cookbook.rb +3 -29
- data/lib/spice/cookbook_version.rb +9 -61
- data/lib/spice/core_ext/array.rb +7 -0
- data/lib/spice/core_ext/enumerable.rb +11 -0
- data/lib/spice/core_ext/hash.rb +49 -0
- data/lib/spice/core_ext/mash.rb +219 -0
- data/lib/spice/data_bag.rb +3 -37
- data/lib/spice/data_bag_item.rb +7 -47
- data/lib/spice/environment.rb +12 -27
- data/lib/spice/error.rb +22 -10
- data/lib/spice/identity_map.rb +8 -0
- data/lib/spice/node.rb +13 -34
- data/lib/spice/request.rb +73 -11
- data/lib/spice/response/parse_json.rb +8 -3
- data/lib/spice/role.rb +8 -33
- data/lib/spice/version.rb +1 -1
- data/spec/fixtures/client.pem +27 -0
- data/spec/fixtures/clients/create.json +4 -0
- data/spec/fixtures/clients/index.json +5 -0
- data/spec/fixtures/clients/reregister.json +5 -0
- data/spec/fixtures/clients/show.json +8 -0
- data/spec/fixtures/clients/update.json +5 -0
- data/spec/fixtures/cookbook_versions/show.json +108 -0
- data/spec/fixtures/cookbook_versions/update.json +62 -0
- data/spec/fixtures/cookbooks/index-0.10.json +28 -0
- data/spec/fixtures/cookbooks/index-0.9.json +4 -0
- data/spec/fixtures/cookbooks/show-0.10.json +15 -0
- data/spec/fixtures/cookbooks/show-apache2-0.9.json +5 -0
- data/spec/fixtures/cookbooks/show-unicorn-0.9.json +5 -0
- data/spec/fixtures/data_bag_items/create.json +1 -0
- data/spec/fixtures/data_bag_items/show.json +4 -0
- data/spec/fixtures/data_bag_items/update.json +4 -0
- data/spec/fixtures/data_bags/create.json +3 -0
- data/spec/fixtures/data_bags/index.json +3 -0
- data/spec/fixtures/data_bags/show.json +3 -0
- data/spec/fixtures/environments/cookbook.json +15 -0
- data/spec/fixtures/environments/cookbooks.json +28 -0
- data/spec/fixtures/environments/create.json +1 -0
- data/spec/fixtures/environments/delete.json +8 -0
- data/spec/fixtures/environments/index.json +3 -0
- data/spec/fixtures/environments/show.json +8 -0
- data/spec/fixtures/environments/update.json +8 -0
- data/spec/fixtures/nodes/cookbooks.json +41 -0
- data/spec/fixtures/nodes/create.json +1 -0
- data/spec/fixtures/nodes/delete.json +17 -0
- data/spec/fixtures/nodes/index.json +3 -0
- data/spec/fixtures/nodes/show.json +17 -0
- data/spec/fixtures/nodes/update.json +13 -0
- data/spec/fixtures/roles/create.json +1 -0
- data/spec/fixtures/roles/delete.json +11 -0
- data/spec/fixtures/roles/index.json +3 -0
- data/spec/fixtures/roles/show.json +12 -0
- data/spec/fixtures/roles/update.json +11 -0
- data/spec/fixtures/search/client.json +1 -0
- data/spec/fixtures/search/data_bag.json +1 -0
- data/spec/fixtures/search/environment.json +1 -0
- data/spec/fixtures/search/node.json +1 -0
- data/spec/fixtures/search/role.json +1 -0
- data/spec/spec_helper.rb +5 -4
- data/spec/spice/base_spec.rb +34 -0
- data/spec/spice/client_spec.rb +0 -61
- data/spec/spice/config_spec.rb +14 -0
- data/spec/spice/connection/clients_spec.rb +82 -0
- data/spec/spice/connection/cookbooks_spec.rb +86 -0
- data/spec/spice/connection/data_bags_spec.rb +126 -0
- data/spec/spice/connection_spec.rb +63 -0
- data/spec/spice/cookbook_spec.rb +0 -61
- data/spec/spice/data_bag_item_spec.rb +7 -0
- data/spec/spice/data_bag_spec.rb +0 -160
- data/spec/spice/environment_spec.rb +0 -89
- data/spec/spice/node_spec.rb +0 -74
- data/spec/spice/role_spec.rb +0 -61
- data/spec/spice_spec.rb +8 -116
- data/spec/support/helpers.rb +58 -0
- data/spice.gemspec +13 -15
- metadata +227 -87
- data/.watchr +0 -22
- data/lib/spice/connection/authentication.rb +0 -47
- data/lib/spice/mock.rb +0 -46
- data/lib/spice/persistence.rb +0 -62
- data/spec/spice/chef_spec.rb +0 -52
- data/spec/spice/search_spec.rb +0 -2
- data/spec/support/chef_requests.rb +0 -0
- data/spec/support/client_requests.rb +0 -95
- data/spec/support/cookbook_requests.rb +0 -95
- data/spec/support/data_bag_item_requests.rb +0 -138
- data/spec/support/data_bag_requests.rb +0 -95
- data/spec/support/environment_requests.rb +0 -221
- data/spec/support/node_requests.rb +0 -138
- data/spec/support/respond_with_matcher.rb +0 -53
- data/spec/support/role_requests.rb +0 -95
@@ -7,7 +7,8 @@ module Spice
|
|
7
7
|
# @option options [String] :sort Order by which to sort the results
|
8
8
|
# @option options [Numeric] :start The number by which to offset the results
|
9
9
|
# @option options [Numeric] :rows The maximum number of rows to return
|
10
|
-
def search(index, options=
|
10
|
+
def search(index, options=Mash.new)
|
11
|
+
index = index.to_s
|
11
12
|
options = {:q => options} if options.is_a? String
|
12
13
|
options.symbolize_keys!
|
13
14
|
|
@@ -22,28 +23,27 @@ module Spice
|
|
22
23
|
params = options.collect{ |k, v| "#{k}=#{CGI::escape(v.to_s)}"}.join("&")
|
23
24
|
case index
|
24
25
|
when 'node'
|
25
|
-
|
26
|
-
Spice::Node.
|
26
|
+
get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |node|
|
27
|
+
Spice::Node.get_or_new(node)
|
27
28
|
end
|
28
29
|
when 'role'
|
29
|
-
|
30
|
-
Spice::Role.
|
30
|
+
get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |role|
|
31
|
+
Spice::Role.get_or_new(role)
|
31
32
|
end
|
32
33
|
when 'client'
|
33
|
-
|
34
|
-
Spice::Client.
|
34
|
+
get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |client|
|
35
|
+
Spice::Client.get_or_new(client)
|
35
36
|
end
|
36
37
|
when 'environment'
|
37
|
-
|
38
|
+
get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |env|
|
38
39
|
env['attrs'] = env.delete('attributes')
|
39
|
-
Spice::Environment.
|
40
|
+
Spice::Environment.get_or_new(env)
|
40
41
|
end
|
41
42
|
else
|
42
43
|
# assume it's a data bag
|
43
|
-
|
44
|
+
get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |db|
|
44
45
|
data = db['raw_data']
|
45
|
-
|
46
|
-
Spice::DataBagItem.new(:_id => id, :data => data, :name => index)
|
46
|
+
Spice::DataBagItem.get_or_new(data)
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end # def search
|
data/lib/spice/cookbook.rb
CHANGED
@@ -1,34 +1,8 @@
|
|
1
|
-
require 'spice/
|
1
|
+
require 'spice/base'
|
2
2
|
|
3
3
|
module Spice
|
4
|
-
class Cookbook
|
5
|
-
|
6
|
-
include Aequitas
|
7
|
-
include Spice::Persistence
|
8
|
-
extend Spice::Persistence
|
4
|
+
class Cookbook < Base
|
5
|
+
attr_reader :name, :versions
|
9
6
|
|
10
|
-
endpoint "cookbooks"
|
11
|
-
|
12
|
-
# @macro [attach] attribute
|
13
|
-
# @attribute [rw]
|
14
|
-
# @return [$2] the $1 attribute
|
15
|
-
attribute :name, String
|
16
|
-
attribute :versions, Array, :default => []
|
17
|
-
|
18
|
-
validates_presence_of :name
|
19
|
-
|
20
|
-
def self.get(name)
|
21
|
-
connection.cookbook(name)
|
22
|
-
end
|
23
|
-
|
24
|
-
# Check if the cookbook exists on the Chef server
|
25
|
-
def new_record?
|
26
|
-
begin
|
27
|
-
connection.get("/cookbooks/#{name}")
|
28
|
-
return false
|
29
|
-
rescue Spice::Error::NotFound
|
30
|
-
return true
|
31
|
-
end
|
32
|
-
end
|
33
7
|
end
|
34
8
|
end
|
@@ -1,68 +1,16 @@
|
|
1
|
-
require 'spice/
|
1
|
+
require 'spice/base'
|
2
2
|
|
3
3
|
module Spice
|
4
|
-
class CookbookVersion
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
extend Spice::Persistence
|
4
|
+
class CookbookVersion < Base
|
5
|
+
attr_reader :name, :version, :definitions, :files, :providers, :metadata,
|
6
|
+
:libraries, :templates, :resources, :attributes, :cookbook_name,
|
7
|
+
:recipes, :root_files
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# @return [$2] the $1 attribute
|
15
|
-
attribute :name, String
|
16
|
-
attribute :version, String
|
17
|
-
attribute :definitions, Array, :default => []
|
18
|
-
attribute :files, Array, :default => []
|
19
|
-
attribute :providers, Array, :default => []
|
20
|
-
attribute :metadata, Hash, :default => {}
|
21
|
-
attribute :libraries, Array, :default => []
|
22
|
-
attribute :templates, Array, :default => []
|
23
|
-
attribute :resources, Array, :default => []
|
24
|
-
attribute :_attributes, Array, :default => []
|
25
|
-
attribute :json_class, String, :default => "Chef::CookbookVersion"
|
26
|
-
attribute :cookbook_name, String
|
27
|
-
attribute :version, String
|
28
|
-
attribute :recipes, Array, :default => []
|
29
|
-
attribute :root_files, Array, :default => []
|
30
|
-
attribute :chef_type, String, :default => "cookbook_version"
|
31
|
-
|
32
|
-
validates_presence_of :name
|
33
|
-
|
34
|
-
def do_post
|
35
|
-
duped_attributes = attributes.dup
|
36
|
-
duped_attributes[:attributes] = attributes[:_attributes]
|
37
|
-
duped_attributes.delete(:_attributes)
|
38
|
-
|
39
|
-
connection.put("/cookbooks/#{cookbook_name}/#{version}", duped_attributes)
|
40
|
-
end
|
41
|
-
|
42
|
-
def do_put
|
43
|
-
duped_attributes = attributes.dup
|
44
|
-
duped_attributes[:attributes] = attributes[:_attributes]
|
45
|
-
duped_attributes.delete(:_attributes)
|
46
|
-
|
47
|
-
connection.put("/cookbooks/#{cookbook_name}/#{version}", duped_attributes)
|
9
|
+
def initialize(attrs=Mash.new)
|
10
|
+
super
|
11
|
+
@attrs['json_class'] ||= "Chef::CookbookVersion"
|
12
|
+
@attrs['chef_type'] ||= 'cookbook_version'
|
48
13
|
end
|
49
14
|
|
50
|
-
def do_delete
|
51
|
-
connection.delete("/cookbooks/#{cookbook_name}/#{version}")
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.get(name, version)
|
55
|
-
connection.cookbook_version(name, version)
|
56
|
-
end
|
57
|
-
|
58
|
-
# Check if the cookbook version exists on the Chef server
|
59
|
-
def new_record?
|
60
|
-
begin
|
61
|
-
connection.get("/cookbooks/#{name.split('-').first}/#{version}")
|
62
|
-
return false
|
63
|
-
rescue Spice::Error::NotFound
|
64
|
-
return true
|
65
|
-
end
|
66
|
-
end
|
67
15
|
end
|
68
16
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# Return a hash that includes everything but the given keys.
|
4
|
+
#
|
5
|
+
# @param keys [Array, Set]
|
6
|
+
# @return [Hash]
|
7
|
+
def except(*keys)
|
8
|
+
self.dup.except!(*keys)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Replaces the hash without the given keys.
|
12
|
+
#
|
13
|
+
# @param keys [Array, Set]
|
14
|
+
# @return [Hash]
|
15
|
+
def except!(*keys)
|
16
|
+
keys.each{|key| delete(key)}
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
# Merges self with another hash, recursively
|
21
|
+
#
|
22
|
+
# @param hash [Hash] The hash to merge
|
23
|
+
# @return [Hash]
|
24
|
+
def deep_merge(hash)
|
25
|
+
target = self.dup
|
26
|
+
hash.keys.each do |key|
|
27
|
+
if hash[key].is_a?(Hash) && self[key].is_a?(Hash)
|
28
|
+
target[key] = target[key].deep_merge(hash[key])
|
29
|
+
next
|
30
|
+
end
|
31
|
+
target[key] = hash[key]
|
32
|
+
end
|
33
|
+
target
|
34
|
+
end
|
35
|
+
|
36
|
+
def stringify_keys!
|
37
|
+
keys.each do |key|
|
38
|
+
self[key.to_s] = delete(key)
|
39
|
+
end
|
40
|
+
self
|
41
|
+
end unless defined? stringify_keys
|
42
|
+
|
43
|
+
def symbolize_keys!
|
44
|
+
keys.each do |key|
|
45
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
46
|
+
end
|
47
|
+
self
|
48
|
+
end unless defined? symbolize_keys
|
49
|
+
end
|
@@ -0,0 +1,219 @@
|
|
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
|
+
# [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
|
+
# [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
|
+
# @return [Mash] A new mash without the selected keys.
|
162
|
+
#
|
163
|
+
# @example
|
164
|
+
# { :one => 1, :two => 2, :three => 3 }.except(:one)
|
165
|
+
# #=> { "two" => 2, "three" => 3 }
|
166
|
+
def except(*keys)
|
167
|
+
super(*keys.map {|k| convert_key(k)})
|
168
|
+
end
|
169
|
+
|
170
|
+
# Used to provide the same interface as Hash.
|
171
|
+
#
|
172
|
+
# @return [Mash] This mash unchanged.
|
173
|
+
def stringify_keys!; self end
|
174
|
+
|
175
|
+
# @return [Hash] The mash as a Hash with symbolized keys.
|
176
|
+
def symbolize_keys
|
177
|
+
h = Hash.new(default)
|
178
|
+
each { |key, val| h[key.to_sym] = val }
|
179
|
+
h
|
180
|
+
end
|
181
|
+
|
182
|
+
# @return [Hash] The mash as a Hash with string keys.
|
183
|
+
def to_hash
|
184
|
+
Hash.new(default).merge(self)
|
185
|
+
end
|
186
|
+
|
187
|
+
# @return [Mash] Convert a Hash into a Mash
|
188
|
+
# The input Hash's default value is maintained
|
189
|
+
def self.from_hash(hash)
|
190
|
+
mash = Mash.new(hash)
|
191
|
+
mash.default = hash.default
|
192
|
+
mash
|
193
|
+
end
|
194
|
+
|
195
|
+
protected
|
196
|
+
# @param key<Object> The key to convert.
|
197
|
+
#
|
198
|
+
# @api private
|
199
|
+
def convert_key(key)
|
200
|
+
key.kind_of?(Symbol) ? key.to_s : key
|
201
|
+
end
|
202
|
+
|
203
|
+
# @param value<Object> The value to convert.
|
204
|
+
#
|
205
|
+
# @return [Object]
|
206
|
+
# The converted value. A Hash or an Array of hashes, will be converted to
|
207
|
+
# their Mash equivalents.
|
208
|
+
#
|
209
|
+
# @api private
|
210
|
+
def convert_value(value)
|
211
|
+
if value.class == Hash
|
212
|
+
Mash.from_hash(value)
|
213
|
+
elsif value.is_a?(Array)
|
214
|
+
value.collect { |e| convert_value(e) }
|
215
|
+
else
|
216
|
+
value
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|