hashie 0.1.2 → 0.1.3
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.
- data/README.rdoc +4 -0
- data/VERSION +1 -1
- data/hashie.gemspec +1 -1
- data/lib/hashie/dash.rb +22 -5
- data/lib/hashie/hash_extensions.rb +16 -0
- data/lib/hashie/mash.rb +3 -13
- data/spec/hashie/dash_spec.rb +10 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -58,6 +58,10 @@ can set defaults for each property.
|
|
58
58
|
p.email # => 'abc@def.com'
|
59
59
|
p[:awesome] # => NoMethodError
|
60
60
|
p[:occupation] # => 'Rubyist'
|
61
|
+
|
62
|
+
p = Person.new(:name => "Bob")
|
63
|
+
p.name # => 'Bob'
|
64
|
+
p.occupation # => 'Rubyist'
|
61
65
|
|
62
66
|
|
63
67
|
== Note on Patches/Pull Requests
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/hashie.gemspec
CHANGED
data/lib/hashie/dash.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'hashie/hash'
|
2
2
|
|
3
3
|
module Hashie
|
4
|
+
include Hashie::PrettyInspect
|
4
5
|
# A Dash is a 'defined' or 'discrete' Hash, that is, a Hash
|
5
6
|
# that has a set of defined keys that are accessible (with
|
6
7
|
# optional defaults) and only those keys may be set or read.
|
@@ -12,6 +13,9 @@ module Hashie
|
|
12
13
|
# It is preferrable to a Struct because of the in-class
|
13
14
|
# API for defining properties as well as per-property defaults.
|
14
15
|
class Dash < Hashie::Hash
|
16
|
+
include Hashie::PrettyInspect
|
17
|
+
alias_method :to_s, :inspect
|
18
|
+
|
15
19
|
# Defines a property on the Dash. Options are
|
16
20
|
# as follows:
|
17
21
|
#
|
@@ -27,11 +31,11 @@ module Hashie
|
|
27
31
|
|
28
32
|
class_eval <<-RUBY
|
29
33
|
def #{property_name}
|
30
|
-
self[
|
34
|
+
self[:#{property_name}]
|
31
35
|
end
|
32
36
|
|
33
37
|
def #{property_name}=(val)
|
34
|
-
self[
|
38
|
+
self[:#{property_name}] = val
|
35
39
|
end
|
36
40
|
RUBY
|
37
41
|
end
|
@@ -53,19 +57,32 @@ module Hashie
|
|
53
57
|
@defaults
|
54
58
|
end
|
55
59
|
|
60
|
+
# You may initialize a Dash with an attributes hash
|
61
|
+
# just like you would many other kinds of data objects.
|
62
|
+
def initialize(attributes = {})
|
63
|
+
self.class.properties.each do |prop|
|
64
|
+
puts "#{prop}="
|
65
|
+
self.send("#{prop}=", self.class.defaults[prop.to_sym])
|
66
|
+
end
|
67
|
+
|
68
|
+
attributes.each_pair do |att, value|
|
69
|
+
self.send("#{att}=", value)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
56
73
|
# Retrieve a value from the Dash (will return the
|
57
74
|
# property's default value if it hasn't been set).
|
58
75
|
def [](property_name)
|
59
|
-
super(property_name.to_sym)
|
76
|
+
super(property_name.to_sym)
|
60
77
|
end
|
61
78
|
|
62
79
|
# Set a value on the Dash in a Hash-like way. Only works
|
63
80
|
# on pre-existing properties.
|
64
81
|
def []=(property, value)
|
65
|
-
if self.class.property?(property)
|
82
|
+
if self.class.property?(property.to_sym)
|
66
83
|
super
|
67
84
|
else
|
68
|
-
raise NoMethodError, '
|
85
|
+
raise NoMethodError, "The property '#{property}' is not defined for this Dash."
|
69
86
|
end
|
70
87
|
end
|
71
88
|
end
|
@@ -28,4 +28,20 @@ module Hashie
|
|
28
28
|
Hashie::Mash.new(self)
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
module PrettyInspect
|
33
|
+
def self.included(base)
|
34
|
+
base.send :alias_method, :hash_inspect, :inspect
|
35
|
+
base.send :alias_method, :inspect, :hashie_inspect
|
36
|
+
end
|
37
|
+
|
38
|
+
def hashie_inspect
|
39
|
+
ret = "<##{self.class.to_s}"
|
40
|
+
keys.sort.each do |key|
|
41
|
+
ret << " #{key}=#{self[key].inspect}"
|
42
|
+
end
|
43
|
+
ret << ">"
|
44
|
+
ret
|
45
|
+
end
|
46
|
+
end
|
31
47
|
end
|
data/lib/hashie/mash.rb
CHANGED
@@ -41,6 +41,9 @@ module Hashie
|
|
41
41
|
# mash.author # => <Mash name="Michael Bleigh">
|
42
42
|
#
|
43
43
|
class Mash < Hashie::Hash
|
44
|
+
include Hashie::PrettyInspect
|
45
|
+
alias_method :to_s, :inspect
|
46
|
+
|
44
47
|
# If you pass in an existing hash, it will
|
45
48
|
# convert it to a Mash including recursively
|
46
49
|
# descending into arrays and hashes, converting
|
@@ -109,19 +112,6 @@ module Hashie
|
|
109
112
|
picky_key?(convert_key(key))
|
110
113
|
end
|
111
114
|
|
112
|
-
alias_method :regular_inspect, :inspect
|
113
|
-
# Prints out a pretty object-like string of the
|
114
|
-
# defined attributes.
|
115
|
-
def inspect
|
116
|
-
ret = "<#{self.class.to_s}"
|
117
|
-
keys.sort.each do |key|
|
118
|
-
ret << " #{key}=#{self[key].inspect}"
|
119
|
-
end
|
120
|
-
ret << ">"
|
121
|
-
ret
|
122
|
-
end
|
123
|
-
alias_method :to_s, :inspect
|
124
|
-
|
125
115
|
# Performs a deep_update on a duplicate of the
|
126
116
|
# current mash.
|
127
117
|
def deep_merge(other_hash)
|
data/spec/hashie/dash_spec.rb
CHANGED
@@ -45,6 +45,16 @@ describe Hashie::Dash do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
describe ' initializing with a Hash' do
|
49
|
+
it 'should not be able to initialize non-existent properties' do
|
50
|
+
lambda{DashTest.new(:bork => 'abc')}.should raise_error(NoMethodError)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should set properties that it is able to' do
|
54
|
+
DashTest.new(:first_name => 'Michael').first_name.should == 'Michael'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
48
58
|
describe ' defaults' do
|
49
59
|
before do
|
50
60
|
@dash = DashTest.new
|