active_hash 0.7.2 → 0.7.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +9 -0
- data/README.md +10 -12
- data/VERSION +1 -1
- data/active_hash.gemspec +4 -5
- data/lib/active_hash/base.rb +20 -1
- data/spec/active_hash/base_spec.rb +2 -0
- data/spec/associations/associations_spec.rb +19 -0
- metadata +2 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
2009-10-22
|
2
|
+
- added setters to ActiveHash::Base for all fields
|
3
|
+
- instantiating an ActiveHash object with a hash calls the setter methods on the object
|
4
|
+
- boolean default values now work
|
5
|
+
|
6
|
+
2009-10-21
|
7
|
+
- Removed auto-reloading of files based on mtime - maybe it will come back later
|
8
|
+
- Made ActiveFile::Base.all a bit more sane
|
9
|
+
|
1
10
|
2009-10-13
|
2
11
|
- added ActiveHash::Base.has_many, which works with ActiveRecord or ActiveHash classes (thanks to baldwindavid)
|
3
12
|
- added ActiveHash::Base.belongs_to, which works with ActiveRecord or ActiveHash classes (thanks to baldwindavid)
|
data/README.md
CHANGED
@@ -4,18 +4,20 @@ ActiveHash is a simple base class that allows you to use a ruby hash as a readon
|
|
4
4
|
|
5
5
|
ActiveHash assumes that every hash has an :id key, which is what you would probably store in a database. This allows you to seemlessly upgrade from ActiveHash objects to full ActiveRecord objects without having to change any code in your app, or any foreign keys in your database.
|
6
6
|
|
7
|
-
It also allows you to use #belongs_to in your AR objects.
|
7
|
+
It also allows you to use #has_many and #belongs_to in your AR objects.
|
8
8
|
|
9
9
|
ActiveHash can also be useful to create simple test classes that run without a database - ideal for testing plugins or gems that rely on simple AR behavior, but don't want to deal with databases or migrations for the spec suite.
|
10
10
|
|
11
11
|
ActiveHash also ships with:
|
12
12
|
|
13
|
-
* ActiveFile: a base class that
|
13
|
+
* ActiveFile: a base class that you can use to create file data sources
|
14
14
|
* ActiveYaml: a base class that will turn YAML into a hash and load the data into an ActiveHash object
|
15
15
|
|
16
16
|
## Installation
|
17
17
|
|
18
|
-
|
18
|
+
Make sure gemcutter.org is one of your gem sources, then run:
|
19
|
+
|
20
|
+
sudo gem install active_hash
|
19
21
|
|
20
22
|
## Usage
|
21
23
|
|
@@ -23,7 +25,7 @@ To use ActiveHash, you need to:
|
|
23
25
|
|
24
26
|
* Inherit from ActiveHash::Base
|
25
27
|
* Define your data
|
26
|
-
*
|
28
|
+
* Define your fields and/or default values
|
27
29
|
|
28
30
|
A quick example would be:
|
29
31
|
|
@@ -109,7 +111,8 @@ It also gives you a few dynamic finder methods. For example, if you defined :na
|
|
109
111
|
|
110
112
|
ActiveHash objects implement enough of the ActiveRecord api to satisfy most common needs. For example:
|
111
113
|
|
112
|
-
Country#id # => returns the
|
114
|
+
Country#id # => returns the id or nil
|
115
|
+
Country#id= # => sets the id attribute
|
113
116
|
Country#quoted_id # => returns the numeric id
|
114
117
|
Country#to_param # => returns the id as a string
|
115
118
|
Country#new_record? # => returns true if is not part of Country.all, false otherwise
|
@@ -121,6 +124,7 @@ ActiveHash also gives you methods related to the fields you defined. For exampl
|
|
121
124
|
|
122
125
|
Country#name # => returns the passed in name
|
123
126
|
Country#name? # => returns true if the name is not blank
|
127
|
+
Country#name= # => sets the name
|
124
128
|
|
125
129
|
## Saving in-memory records
|
126
130
|
|
@@ -227,8 +231,6 @@ By default, this class will look for a yml file named "countries.yml" in the sam
|
|
227
231
|
|
228
232
|
The above example will look for the file "/u/data/sample.yml".
|
229
233
|
|
230
|
-
ActiveYaml, as well as ActiveFile, check the mtime of the file you specified, and reloads the data if the mtime has changed. So you can replace the data in the files even if your app is running in production mode in rails.
|
231
|
-
|
232
234
|
Since ActiveYaml just creates a hash from the YAML file, you will have all fields specified in YAML auto-defined for you once you call all. You can format your YAML as an array, or as a hash:
|
233
235
|
|
234
236
|
# array style
|
@@ -276,11 +278,7 @@ Setting the default file location in Rails:
|
|
276
278
|
# config/initializers/active_file.rb
|
277
279
|
ActiveFile.set_root_path "config/activefiles"
|
278
280
|
|
279
|
-
|
280
|
-
|
281
|
-
ActiveFile.reload_active_file = true
|
282
|
-
|
283
|
-
In Rails, in development mode, it reloads the entire class, which reloads the file, so you don't need to turn this on in development.
|
281
|
+
In Rails, in development mode, it reloads the entire class, which reloads the file. In production, the data cached in memory.
|
284
282
|
|
285
283
|
NOTE: By default, .full_path refers to the current working directory. In a rails app, this will be RAILS_ROOT.
|
286
284
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.3
|
data/active_hash.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{active_hash}
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jeff Dean", "Mike Dalessio", "Corey Innis", "Peter Jaros"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-22}
|
13
13
|
s.email = %q{jeff@zilkey.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE",
|
@@ -66,4 +66,3 @@ Gem::Specification.new do |s|
|
|
66
66
|
s.add_dependency(%q<activesupport>, [">= 2.2.2"])
|
67
67
|
end
|
68
68
|
end
|
69
|
-
|
data/lib/active_hash/base.rb
CHANGED
@@ -89,6 +89,7 @@ module ActiveHash
|
|
89
89
|
@field_names << field_name
|
90
90
|
|
91
91
|
define_getter_method(field_name, options[:default])
|
92
|
+
define_setter_method(field_name)
|
92
93
|
define_interrogator_method(field_name)
|
93
94
|
define_custom_find_method(field_name)
|
94
95
|
define_custom_find_all_method(field_name)
|
@@ -127,13 +128,24 @@ module ActiveHash
|
|
127
128
|
def define_getter_method(field, default_value)
|
128
129
|
unless instance_methods.include?(field.to_s)
|
129
130
|
define_method(field) do
|
130
|
-
attributes[field]
|
131
|
+
attributes[field].nil? ? default_value : attributes[field]
|
131
132
|
end
|
132
133
|
end
|
133
134
|
end
|
134
135
|
|
135
136
|
private :define_getter_method
|
136
137
|
|
138
|
+
def define_setter_method(field)
|
139
|
+
method_name = "#{field}="
|
140
|
+
unless instance_methods.include?(method_name)
|
141
|
+
define_method(method_name) do |new_val|
|
142
|
+
attributes[field] = new_val
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
private :define_setter_method
|
148
|
+
|
137
149
|
def define_interrogator_method(field)
|
138
150
|
method_name = "#{field}?"
|
139
151
|
unless instance_methods.include?(method_name)
|
@@ -201,12 +213,19 @@ module ActiveHash
|
|
201
213
|
def initialize(options = {})
|
202
214
|
options.symbolize_keys!
|
203
215
|
@attributes = options
|
216
|
+
options.each do |key, value|
|
217
|
+
send "#{key}=", value
|
218
|
+
end
|
204
219
|
end
|
205
220
|
|
206
221
|
def id
|
207
222
|
attributes[:id] ? attributes[:id] : nil
|
208
223
|
end
|
209
224
|
|
225
|
+
def id=(id)
|
226
|
+
attributes[:id] = id
|
227
|
+
end
|
228
|
+
|
210
229
|
alias quoted_id id
|
211
230
|
|
212
231
|
def new_record?
|
@@ -330,11 +330,13 @@ describe ActiveHash, "Base" do
|
|
330
330
|
|
331
331
|
describe "#attributes" do
|
332
332
|
it "returns the hash passed in the initializer" do
|
333
|
+
Country.field :foo
|
333
334
|
country = Country.new(:foo => :bar)
|
334
335
|
country.attributes.should == {:foo => :bar}
|
335
336
|
end
|
336
337
|
|
337
338
|
it "symbolizes keys" do
|
339
|
+
Country.field :foo
|
338
340
|
country = Country.new("foo" => :bar)
|
339
341
|
country.attributes.should == {:foo => :bar}
|
340
342
|
end
|
@@ -95,6 +95,25 @@ describe ActiveHash::Base, "associations" do
|
|
95
95
|
author.city = @city
|
96
96
|
author.city_id.should == @city.id
|
97
97
|
end
|
98
|
+
|
99
|
+
it "works from hash assignment" do
|
100
|
+
author = Author.new :city => @city
|
101
|
+
author.city_id.should == @city.id
|
102
|
+
author.city.should == @city
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "with a different foreign key" do
|
107
|
+
before do
|
108
|
+
Author.belongs_to :residence, :class_name => "City", :foreign_key => "city_id"
|
109
|
+
@city = City.create :id => 1
|
110
|
+
end
|
111
|
+
|
112
|
+
it "works" do
|
113
|
+
author = Author.new
|
114
|
+
author.residence = @city
|
115
|
+
author.city_id.should == @city.id
|
116
|
+
end
|
98
117
|
end
|
99
118
|
|
100
119
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Dean
|
@@ -12,7 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2009-10-
|
15
|
+
date: 2009-10-22 00:00:00 -04:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|