summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Bleigh <michael@intridea.com>2009-11-12 10:05:00 -0500
committerMichael Bleigh <michael@intridea.com>2009-11-12 10:05:13 -0500
commit9a4c4d9fb455fccfd32f1ccaab4f42558593ebf2 (patch)
tree3e7523c6104d9e08df5053491b1cae86dc848e77
parentf04ca62dc09d4c00500c0a64cb4bc3b12e7b4a0b (diff)
downloadhashie-9a4c4d9fb455fccfd32f1ccaab4f42558593ebf2.tar.gz
Adds pretty inspect for Dash.
-rw-r--r--lib/hashie/dash.rb27
-rw-r--r--lib/hashie/hash_extensions.rb16
-rw-r--r--lib/hashie/mash.rb16
-rw-r--r--spec/hashie/dash_spec.rb10
4 files changed, 51 insertions, 18 deletions
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
index 944763c..427cc96 100644
--- a/lib/hashie/dash.rb
+++ b/lib/hashie/dash.rb
@@ -1,6 +1,7 @@
require 'hashie/hash'
module Hashie
+ include Hashie::PrettyInspect
# A Dash is a 'defined' or 'discrete' Hash, that is, a Hash
# that has a set of defined keys that are accessible (with
# optional defaults) and only those keys may be set or read.
@@ -12,6 +13,9 @@ module Hashie
# It is preferrable to a Struct because of the in-class
# API for defining properties as well as per-property defaults.
class Dash < Hashie::Hash
+ include Hashie::PrettyInspect
+ alias_method :to_s, :inspect
+
# Defines a property on the Dash. Options are
# as follows:
#
@@ -27,11 +31,11 @@ module Hashie
class_eval <<-RUBY
def #{property_name}
- self['#{property_name}']
+ self[:#{property_name}]
end
def #{property_name}=(val)
- self['#{property_name}'] = val
+ self[:#{property_name}] = val
end
RUBY
end
@@ -53,19 +57,32 @@ module Hashie
@defaults
end
+ # You may initialize a Dash with an attributes hash
+ # just like you would many other kinds of data objects.
+ def initialize(attributes = {})
+ self.class.properties.each do |prop|
+ puts "#{prop}="
+ self.send("#{prop}=", self.class.defaults[prop.to_sym])
+ end
+
+ attributes.each_pair do |att, value|
+ self.send("#{att}=", value)
+ end
+ end
+
# Retrieve a value from the Dash (will return the
# property's default value if it hasn't been set).
def [](property_name)
- super(property_name.to_sym) || self.class.defaults[property_name.to_sym]
+ super(property_name.to_sym)
end
# Set a value on the Dash in a Hash-like way. Only works
# on pre-existing properties.
def []=(property, value)
- if self.class.property?(property)
+ if self.class.property?(property.to_sym)
super
else
- raise NoMethodError, 'You may only set pre-defined properties.'
+ raise NoMethodError, "The property '#{property}' is not defined for this Dash."
end
end
end
diff --git a/lib/hashie/hash_extensions.rb b/lib/hashie/hash_extensions.rb
index 188c10b..145bb06 100644
--- a/lib/hashie/hash_extensions.rb
+++ b/lib/hashie/hash_extensions.rb
@@ -28,4 +28,20 @@ module Hashie
Hashie::Mash.new(self)
end
end
+
+ module PrettyInspect
+ def self.included(base)
+ base.send :alias_method, :hash_inspect, :inspect
+ base.send :alias_method, :inspect, :hashie_inspect
+ end
+
+ def hashie_inspect
+ ret = "<##{self.class.to_s}"
+ keys.sort.each do |key|
+ ret << " #{key}=#{self[key].inspect}"
+ end
+ ret << ">"
+ ret
+ end
+ end
end \ No newline at end of file
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 39bfd69..a1f8fd8 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -41,6 +41,9 @@ module Hashie
# mash.author # => <Mash name="Michael Bleigh">
#
class Mash < Hashie::Hash
+ include Hashie::PrettyInspect
+ alias_method :to_s, :inspect
+
# If you pass in an existing hash, it will
# convert it to a Mash including recursively
# descending into arrays and hashes, converting
@@ -109,19 +112,6 @@ module Hashie
picky_key?(convert_key(key))
end
- alias_method :regular_inspect, :inspect
- # Prints out a pretty object-like string of the
- # defined attributes.
- def inspect
- ret = "<#{self.class.to_s}"
- keys.sort.each do |key|
- ret << " #{key}=#{self[key].inspect}"
- end
- ret << ">"
- ret
- end
- alias_method :to_s, :inspect
-
# Performs a deep_update on a duplicate of the
# current mash.
def deep_merge(other_hash)
diff --git a/spec/hashie/dash_spec.rb b/spec/hashie/dash_spec.rb
index d9fc01e..97ed3c1 100644
--- a/spec/hashie/dash_spec.rb
+++ b/spec/hashie/dash_spec.rb
@@ -45,6 +45,16 @@ describe Hashie::Dash do
end
end
+ describe ' initializing with a Hash' do
+ it 'should not be able to initialize non-existent properties' do
+ lambda{DashTest.new(:bork => 'abc')}.should raise_error(NoMethodError)
+ end
+
+ it 'should set properties that it is able to' do
+ DashTest.new(:first_name => 'Michael').first_name.should == 'Michael'
+ end
+ end
+
describe ' defaults' do
before do
@dash = DashTest.new