hashie 0.4.0 → 1.0.0

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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format=nested
data/Gemfile CHANGED
@@ -8,5 +8,5 @@ group :development do
8
8
  end
9
9
 
10
10
  group :test do
11
- gem 'rspec'
11
+ gem 'rspec', '~> 2.0'
12
12
  end
@@ -1,6 +1,7 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ diff-lcs (1.1.2)
4
5
  gemcutter (0.6.1)
5
6
  git (1.2.5)
6
7
  jeweler (1.4.0)
@@ -10,7 +11,14 @@ GEM
10
11
  json (1.4.3)
11
12
  json_pure (1.4.3)
12
13
  rake (0.8.7)
13
- rspec (1.3.0)
14
+ rspec (2.4.0)
15
+ rspec-core (~> 2.4.0)
16
+ rspec-expectations (~> 2.4.0)
17
+ rspec-mocks (~> 2.4.0)
18
+ rspec-core (2.4.0)
19
+ rspec-expectations (2.4.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.4.0)
14
22
  rubyforge (2.0.4)
15
23
  json_pure (>= 1.1.7)
16
24
 
@@ -21,4 +29,4 @@ DEPENDENCIES
21
29
  jeweler
22
30
  json
23
31
  rake
24
- rspec
32
+ rspec (~> 2.0)
data/Rakefile CHANGED
@@ -18,16 +18,10 @@ rescue LoadError
18
18
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
19
  end
20
20
 
21
- require 'spec/rake/spectask'
22
- Spec::Rake::SpecTask.new(:spec) do |spec|
23
- spec.libs << 'lib' << 'spec'
24
- spec.spec_files = FileList['spec/**/*_spec.rb']
25
- end
26
-
27
- Spec::Rake::SpecTask.new(:rcov) do |spec|
28
- spec.libs << 'lib' << 'spec'
21
+ require 'rspec/core/rake_task'
22
+ RSpec::Core::RakeTask.new do |spec|
23
+ # spec.libs << 'lib' << 'spec'
29
24
  spec.pattern = 'spec/**/*_spec.rb'
30
- spec.rcov = true
31
25
  end
32
26
 
33
27
  task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 1.0.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{hashie}
8
- s.version = "0.4.0"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Bleigh"]
12
- s.date = %q{2010-08-31}
12
+ s.date = %q{2011-01-27}
13
13
  s.description = %q{Hashie is a small collection of tools that make hashes more powerful. Currently includes Mash (Mocking Hash) and Dash (Discrete Hash).}
14
14
  s.email = %q{michael@intridea.com}
15
15
  s.extra_rdoc_files = [
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
+ ".rspec",
22
23
  "Gemfile",
23
24
  "Gemfile.lock",
24
25
  "LICENSE",
@@ -28,18 +28,20 @@ module Hashie
28
28
 
29
29
  self.properties << property_name
30
30
 
31
- if options[:default] or self.defaults[property_name]
31
+ if options.has_key?(:default)
32
32
  self.defaults[property_name] = options[:default]
33
+ elsif self.defaults.has_key?(property_name)
34
+ self.defaults.delete property_name
33
35
  end
34
36
 
35
37
  unless instance_methods.map { |m| m.to_s }.include?("#{property_name}=")
36
38
  class_eval <<-ACCESSORS
37
- def #{property_name}
38
- _regular_reader(#{property_name.to_s.inspect})
39
+ def #{property_name}(&block)
40
+ self.[](#{property_name.to_s.inspect}, &block)
39
41
  end
40
42
 
41
43
  def #{property_name}=(value)
42
- _regular_writer(#{property_name.to_s.inspect}, value)
44
+ self.[]=(#{property_name.to_s.inspect}, value)
43
45
  end
44
46
  ACCESSORS
45
47
  end
@@ -74,11 +76,11 @@ module Hashie
74
76
  super(&block)
75
77
 
76
78
  self.class.defaults.each_pair do |prop, value|
77
- self.send("#{prop}=", value)
79
+ self[prop] = value
78
80
  end
79
81
 
80
82
  attributes.each_pair do |att, value|
81
- self.send("#{att}=", value)
83
+ self[att] = value
82
84
  end if attributes
83
85
  end
84
86
 
@@ -90,7 +92,9 @@ module Hashie
90
92
  # property's default value if it hasn't been set).
91
93
  def [](property)
92
94
  assert_property_exists! property
93
- super(property.to_s)
95
+ value = super(property.to_s)
96
+ yield value if block_given?
97
+ value
94
98
  end
95
99
 
96
100
  # Set a value on the Dash in a Hash-like way. Only works
@@ -67,7 +67,9 @@ module Hashie
67
67
  # Retrieves an attribute set in the Mash. Will convert
68
68
  # any key passed in to a string before retrieving.
69
69
  def [](key)
70
- regular_reader(convert_key(key))
70
+ value = regular_reader(convert_key(key))
71
+ yield value if block_given?
72
+ value
71
73
  end
72
74
 
73
75
  # Sets an attribute in the Mash. Key will be converted to
@@ -85,6 +87,10 @@ module Hashie
85
87
  regular_reader(ck)
86
88
  end
87
89
 
90
+ def delete(key)
91
+ super(convert_key(key))
92
+ end
93
+
88
94
  alias_method :regular_dup, :dup
89
95
  # Duplicates the current mash as a new mash.
90
96
  def dup
@@ -141,7 +147,7 @@ module Hashie
141
147
  end
142
148
 
143
149
  def method_missing(method_name, *args, &blk)
144
- return self[method_name] if key?(method_name)
150
+ return self.[](method_name, &blk) if key?(method_name)
145
151
  match = method_name.to_s.match(/(.*?)([?=!]?)$/)
146
152
  case match[2]
147
153
  when "="
@@ -54,6 +54,26 @@ describe DashTest do
54
54
  subject.first_name.should == 'Franklin'
55
55
  end
56
56
  end
57
+
58
+ context 'reading from properties' do
59
+ it 'fails reading from a non-existent property using []' do
60
+ lambda { subject['nonexistent'] }.should raise_error(NoMethodError)
61
+ end
62
+
63
+ it "should be able to retrieve properties through blocks" do
64
+ subject["first_name"] = "Aiden"
65
+ value = nil
66
+ subject.[]("first_name") { |v| value = v }
67
+ value.should == "Aiden"
68
+ end
69
+
70
+ it "should be able to retrieve properties through blocks with method calls" do
71
+ subject["first_name"] = "Frodo"
72
+ value = nil
73
+ subject.first_name { |v| value = v }
74
+ value.should == "Frodo"
75
+ end
76
+ end
57
77
 
58
78
  describe '.new' do
59
79
  it 'fails with non-existent properties' do
@@ -134,6 +154,12 @@ describe Hashie::Dash, 'inheritance' do
134
154
  @bottom.properties.to_a.should == [:echo]
135
155
  @bottom.new.echo.should be_nil
136
156
  end
157
+
158
+ it 'should allow nil defaults' do
159
+ @bottom.property :echo, :default => nil
160
+ @bottom.new.should have_key('echo')
161
+ end
162
+
137
163
  end
138
164
 
139
165
  describe Subclassed do
@@ -18,6 +18,20 @@ describe Hashie::Mash do
18
18
  @mash["test"] = "abc"
19
19
  @mash.test.should == "abc"
20
20
  end
21
+
22
+ it "should be able to retrieve set values through blocks" do
23
+ @mash["test"] = "abc"
24
+ value = nil
25
+ @mash.[]("test") { |v| value = v }
26
+ value.should == "abc"
27
+ end
28
+
29
+ it "should be able to retrieve set values through blocks with method calls" do
30
+ @mash["test"] = "abc"
31
+ value = nil
32
+ @mash.test { |v| value = v }
33
+ value.should == "abc"
34
+ end
21
35
 
22
36
  it "should test for already set values when passed a ? method" do
23
37
  @mash.test?.should be_false
@@ -128,6 +142,20 @@ describe Hashie::Mash do
128
142
  duped.details.email.should be_nil
129
143
  end
130
144
  end
145
+
146
+ describe 'delete' do
147
+ it 'should delete with String key' do
148
+ subject.delete('details')
149
+ subject.details.should be_nil
150
+ subject.should_not be_respond_to :details
151
+ end
152
+
153
+ it 'should delete with Symbol key' do
154
+ subject.delete(:details)
155
+ subject.details.should be_nil
156
+ subject.should_not be_respond_to :details
157
+ end
158
+ end
131
159
  end
132
160
 
133
161
  it "should convert hash assignments into Hashie::Mashes" do
@@ -5,9 +5,9 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
 
7
7
  require 'hashie'
8
- require 'spec'
9
- require 'spec/autorun'
8
+ require 'rspec'
9
+ require 'rspec/autorun'
10
10
 
11
- Spec::Runner.configure do |config|
11
+ RSpec.configure do |config|
12
12
 
13
13
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashie
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
4
  prerelease: false
6
5
  segments:
6
+ - 1
7
7
  - 0
8
- - 4
9
8
  - 0
10
- version: 0.4.0
9
+ version: 1.0.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Michael Bleigh
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-08-31 00:00:00 -05:00
17
+ date: 2011-01-27 00:00:00 -06:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -44,6 +42,7 @@ extra_rdoc_files:
44
42
  files:
45
43
  - .document
46
44
  - .gitignore
45
+ - .rspec
47
46
  - Gemfile
48
47
  - Gemfile.lock
49
48
  - LICENSE
@@ -79,7 +78,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
78
  requirements:
80
79
  - - ">="
81
80
  - !ruby/object:Gem::Version
82
- hash: 3
83
81
  segments:
84
82
  - 0
85
83
  version: "0"
@@ -88,7 +86,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  requirements:
89
87
  - - ">="
90
88
  - !ruby/object:Gem::Version
91
- hash: 3
92
89
  segments:
93
90
  - 0
94
91
  version: "0"