active_hash 0.9.1 → 0.9.2

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/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2011-01-22
2
+ - improved method_missing errors for dynamic finders (
3
+ - prevent users from trying to overwrite :attributes (https://github.com/zilkey/active_hash/issues/#issue/33)
4
+
1
5
  2010-12-08
2
6
  - ruby 1.9.2 compatibility
3
7
 
data/active_hash.gemspec CHANGED
@@ -1,8 +1,11 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "active_hash/version"
5
+
3
6
  Gem::Specification.new do |s|
4
7
  s.name = %q{active_hash}
5
- s.version = "0.9.1"
8
+ s.version = ActiveHash::Gem::VERSION
6
9
 
7
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
11
  s.authors = [
@@ -21,7 +24,7 @@ Gem::Specification.new do |s|
21
24
  "Joel Chippindale",
22
25
  "Kevin Olsen"
23
26
  ]
24
- s.date = %q{2010-12-08}
27
+ s.date = %q{2011-01-22}
25
28
  s.email = %q{jeff@zilkey.com}
26
29
  s.extra_rdoc_files = [
27
30
  "LICENSE",
@@ -31,7 +34,6 @@ Gem::Specification.new do |s|
31
34
  "CHANGELOG",
32
35
  "LICENSE",
33
36
  "README.md",
34
- "VERSION",
35
37
  "active_hash.gemspec",
36
38
  Dir.glob("lib/**/*")
37
39
  ].flatten
@@ -3,6 +3,9 @@ module ActiveHash
3
3
  class RecordNotFound < StandardError
4
4
  end
5
5
 
6
+ class ReservedFieldError < StandardError
7
+ end
8
+
6
9
  class Base
7
10
  class_inheritable_accessor :data, :dirty
8
11
 
@@ -16,7 +19,9 @@ module ActiveHash
16
19
  end
17
20
 
18
21
  class << self
19
- attr_reader :field_names
22
+ def field_names
23
+ @field_names ||= []
24
+ end
20
25
 
21
26
  def the_meta_class
22
27
  class << self
@@ -113,7 +118,7 @@ module ActiveHash
113
118
 
114
119
  delegate :first, :last, :to => :all
115
120
 
116
- def fields(* args)
121
+ def fields(*args)
117
122
  options = args.extract_options!
118
123
  args.each do |field|
119
124
  field(field, options)
@@ -121,8 +126,8 @@ module ActiveHash
121
126
  end
122
127
 
123
128
  def field(field_name, options = {})
124
- @field_names ||= []
125
- @field_names << field_name
129
+ validate_field(field_name)
130
+ field_names << field_name
126
131
 
127
132
  define_getter_method(field_name, options[:default])
128
133
  define_setter_method(field_name)
@@ -131,6 +136,14 @@ module ActiveHash
131
136
  define_custom_find_all_method(field_name)
132
137
  end
133
138
 
139
+ def validate_field(field_name)
140
+ if [:attributes].include?(field_name.to_sym)
141
+ raise ReservedFieldError.new("#{field_name} is a reserved field in ActiveHash. Please use another name.")
142
+ end
143
+ end
144
+
145
+ private :validate_field
146
+
134
147
  def respond_to?(method_name, include_private=false)
135
148
  super ||
136
149
  begin
@@ -0,0 +1,5 @@
1
+ module ActiveHash
2
+ module Gem
3
+ VERSION = "0.9.2"
4
+ end
5
+ end
@@ -75,6 +75,12 @@ describe ActiveHash, "Base" do
75
75
  Country.find_by_name_and_iso_name("Canada", "CA", :select => nil).should be_nil
76
76
  Country.find_all_by_name_and_iso_name("Canada", "CA", :select => nil).should == []
77
77
  end
78
+
79
+ it "blows up if you try to overwrite :attributes" do
80
+ proc do
81
+ Country.field :attributes
82
+ end.should raise_error(ActiveHash::ReservedFieldError)
83
+ end
78
84
  end
79
85
 
80
86
  describe ".data=" do
@@ -147,7 +153,7 @@ describe ActiveHash, "Base" do
147
153
  end
148
154
 
149
155
  it "returns all data as inflated objects" do
150
- Country.all.all?{|country| country.should be_kind_of(Country)}
156
+ Country.all.all? { |country| country.should be_kind_of(Country) }
151
157
  end
152
158
 
153
159
  it "populates the data correctly" do
@@ -167,7 +173,7 @@ describe ActiveHash, "Base" do
167
173
  records.first.name.should == "Canada"
168
174
  records.length.should == 1
169
175
  end
170
-
176
+
171
177
  it "filters the records from a AR-like conditions hash" do
172
178
  record = Country.all(:conditions => {:name => 'US'})
173
179
  record.count.should == 1
@@ -468,6 +474,14 @@ describe ActiveHash, "Base" do
468
474
  end
469
475
  end
470
476
 
477
+ describe "#method_missing" do
478
+ it "doesn't blow up if you call a missing dynamic finder when fields haven't been set" do
479
+ proc do
480
+ Country.find_by_name("Foo")
481
+ end.should raise_error(NoMethodError, "undefined method `find_by_name' for Country:Class")
482
+ end
483
+ end
484
+
471
485
  describe "#attributes" do
472
486
  it "returns the hash passed in the initializer" do
473
487
  Country.field :foo
@@ -644,8 +658,8 @@ describe ActiveHash, "Base" do
644
658
  end
645
659
 
646
660
  it "is hashable" do
647
- { Country.new(:id => 4) => "bar"}.should == {Country.new(:id => 4) => "bar" }
648
- { Country.new(:id => 3) => "bar"}.should_not == {Country.new(:id => 4) => "bar" }
661
+ {Country.new(:id => 4) => "bar"}.should == {Country.new(:id => 4) => "bar"}
662
+ {Country.new(:id => 3) => "bar"}.should_not == {Country.new(:id => 4) => "bar"}
649
663
  end
650
664
  end
651
665
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 57
5
- prerelease: false
4
+ hash: 63
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 1
10
- version: 0.9.1
9
+ - 2
10
+ version: 0.9.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeff Dean
@@ -28,7 +28,7 @@ autorequire:
28
28
  bindir: bin
29
29
  cert_chain: []
30
30
 
31
- date: 2010-12-08 00:00:00 -07:00
31
+ date: 2011-01-22 00:00:00 -07:00
32
32
  default_executable:
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
@@ -60,10 +60,10 @@ files:
60
60
  - CHANGELOG
61
61
  - LICENSE
62
62
  - README.md
63
- - VERSION
64
63
  - active_hash.gemspec
65
64
  - lib/active_file/base.rb
66
65
  - lib/active_hash/base.rb
66
+ - lib/active_hash/version.rb
67
67
  - lib/active_hash.rb
68
68
  - lib/active_yaml/base.rb
69
69
  - lib/associations/associations.rb
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  requirements: []
108
108
 
109
109
  rubyforge_project:
110
- rubygems_version: 1.3.7
110
+ rubygems_version: 1.4.2
111
111
  signing_key:
112
112
  specification_version: 3
113
113
  summary: An ActiveRecord-like model that uses a hash or file as a datasource
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.9.0