active_hash 0.9.13 → 0.9.14
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 +4 -0
- data/README.md +19 -3
- data/lib/active_hash/version.rb +1 -1
- data/lib/associations/associations.rb +16 -3
- data/lib/enum/enum.rb +9 -8
- data/spec/associations/associations_spec.rb +1 -1
- data/spec/enum/enum_spec.rb +22 -7
- metadata +5 -2
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
2013-05-23 (v0.9.14)
|
|
2
|
+
- enum_accessor can now take multiple field names when generating the constant
|
|
3
|
+
- temporarily disabled rails edge specs since there's an appraisal issue with minitest
|
|
4
|
+
|
|
1
5
|
2013-01-22
|
|
2
6
|
- Fix find_by_id and find method returning nil unless .all called in ActiveYaml (mattheworiordan)
|
|
3
7
|
|
data/README.md
CHANGED
|
@@ -43,6 +43,7 @@ A quick example would be:
|
|
|
43
43
|
You can also use _create_:
|
|
44
44
|
|
|
45
45
|
class Country < ActiveHash::Base
|
|
46
|
+
field :name
|
|
46
47
|
create :id => 1, :name => "US"
|
|
47
48
|
create :id => 2, :name => "Canada"
|
|
48
49
|
end
|
|
@@ -50,6 +51,7 @@ You can also use _create_:
|
|
|
50
51
|
If you are Pat Nakajima, you probably prefer _add_:
|
|
51
52
|
|
|
52
53
|
class Country < ActiveHash::Base
|
|
54
|
+
field :name
|
|
53
55
|
add :id => 1, :name => "US"
|
|
54
56
|
add :id => 2, :name => "Canada"
|
|
55
57
|
end
|
|
@@ -62,7 +64,7 @@ If you add data to an ActiveHash file during an initializer, it will not be relo
|
|
|
62
64
|
|
|
63
65
|
ActiveHash will auto-define all fields for you when you load the hash. For example, if you have the following class:
|
|
64
66
|
|
|
65
|
-
class CustomField <
|
|
67
|
+
class CustomField < ActiveHash::Base
|
|
66
68
|
self.data = [
|
|
67
69
|
{:custom_field_1 => "foo"},
|
|
68
70
|
{:custom_field_2 => "foo"},
|
|
@@ -74,7 +76,7 @@ Once you call CustomField.all it will define methods for :custom_field_1, :custo
|
|
|
74
76
|
|
|
75
77
|
If you need the fields at load time, as opposed to after .all is called, you can also define them manually, like so:
|
|
76
78
|
|
|
77
|
-
class CustomField <
|
|
79
|
+
class CustomField < ActiveHash::Base
|
|
78
80
|
fields :custom_field_1, :custom_field_2, :custom_field_3
|
|
79
81
|
end
|
|
80
82
|
|
|
@@ -367,7 +369,21 @@ Records can be accessed by looking up the field constant:
|
|
|
367
369
|
=> 3
|
|
368
370
|
>> Country::CANADA
|
|
369
371
|
=> #<Country:0x10229fb28 @attributes={:name=>"Canada", :id=>2}
|
|
370
|
-
|
|
372
|
+
|
|
373
|
+
You may also use multiple attributes to generate the constant, like so:
|
|
374
|
+
|
|
375
|
+
class Town < ActiveHash::Base
|
|
376
|
+
include ActiveHash::Enum
|
|
377
|
+
self.data = [
|
|
378
|
+
{:id => 1, :name => "Columbus", :state => "NY"},
|
|
379
|
+
{:id => 2, :name => "Columbus", :state => "OH"}
|
|
380
|
+
]
|
|
381
|
+
enum_accessor :name, :state
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
>> Town::COLUMBUS_NY
|
|
385
|
+
>> Town::COLUMBUS_OH
|
|
386
|
+
|
|
371
387
|
Constants are formed by first stripping all non-word characters and then upcasing the result. This means strings like "Blazing Saddles", "ReBar", "Mike & Ike" and "Ho! Ho! Ho!" become BLAZING_SADDLES, REBAR, MIKE_IKE and HO_HO_HO.
|
|
372
388
|
|
|
373
389
|
The field specified as the _enum_accessor_ must contain unique data values.
|
data/lib/active_hash/version.rb
CHANGED
|
@@ -29,12 +29,23 @@ module ActiveHash
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
create_reflection
|
|
32
|
+
method = ActiveRecord::Base.method(:create_reflection)
|
|
33
|
+
if method.respond_to?(:parameters) && method.parameters.length == 5
|
|
34
|
+
create_reflection(
|
|
35
|
+
:belongs_to,
|
|
36
|
+
association_id.to_sym,
|
|
37
|
+
nil,
|
|
38
|
+
options,
|
|
39
|
+
self
|
|
40
|
+
)
|
|
41
|
+
else
|
|
42
|
+
create_reflection(
|
|
33
43
|
:belongs_to,
|
|
34
44
|
association_id.to_sym,
|
|
35
45
|
options,
|
|
36
46
|
options[:class_name].constantize
|
|
37
|
-
|
|
47
|
+
)
|
|
48
|
+
end
|
|
38
49
|
end
|
|
39
50
|
|
|
40
51
|
end
|
|
@@ -54,7 +65,9 @@ module ActiveHash
|
|
|
54
65
|
|
|
55
66
|
klass = options[:class_name].constantize
|
|
56
67
|
|
|
57
|
-
if
|
|
68
|
+
if ActiveRecord.const_defined?(:Relation) && klass.all.class < ActiveRecord::Relation
|
|
69
|
+
klass.where(options[:foreign_key] => id)
|
|
70
|
+
elsif klass.respond_to?(:scoped)
|
|
58
71
|
klass.scoped(:conditions => {options[:foreign_key] => id})
|
|
59
72
|
else
|
|
60
73
|
klass.send("find_all_by_#{options[:foreign_key]}", id)
|
data/lib/enum/enum.rb
CHANGED
|
@@ -6,23 +6,23 @@ module ActiveHash
|
|
|
6
6
|
def self.included(base)
|
|
7
7
|
base.extend(Methods)
|
|
8
8
|
end
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
module Methods
|
|
11
11
|
|
|
12
|
-
def enum_accessor(
|
|
13
|
-
@
|
|
12
|
+
def enum_accessor(*field_names)
|
|
13
|
+
@enum_accessors = field_names
|
|
14
14
|
reload
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def insert(record)
|
|
18
18
|
super
|
|
19
|
-
set_constant(record) if @
|
|
19
|
+
set_constant(record) if @enum_accessors.present?
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def delete_all
|
|
23
|
-
if @
|
|
23
|
+
if @enum_accessors.present?
|
|
24
24
|
@records.each do |record|
|
|
25
|
-
constant = constant_for(record
|
|
25
|
+
constant = constant_for(record, @enum_accessors)
|
|
26
26
|
remove_const(constant) if const_defined?(constant)
|
|
27
27
|
end
|
|
28
28
|
end
|
|
@@ -30,7 +30,7 @@ module ActiveHash
|
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
def set_constant(record)
|
|
33
|
-
constant = constant_for(record
|
|
33
|
+
constant = constant_for(record, @enum_accessors)
|
|
34
34
|
return nil if constant.blank?
|
|
35
35
|
|
|
36
36
|
unless const_defined?(constant)
|
|
@@ -42,7 +42,8 @@ module ActiveHash
|
|
|
42
42
|
|
|
43
43
|
private :set_constant
|
|
44
44
|
|
|
45
|
-
def constant_for(
|
|
45
|
+
def constant_for(record, field_names)
|
|
46
|
+
field_value = field_names.map { |name| record.attributes[name] }.join("_")
|
|
46
47
|
if constant = !field_value.nil? && field_value.dup
|
|
47
48
|
constant.gsub!(/\W+/, "_")
|
|
48
49
|
constant.gsub!(/^_|_$/, '')
|
|
@@ -37,7 +37,7 @@ describe ActiveHash::Base, "associations" do
|
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
if Object.const_defined?(:ActiveModel)
|
|
40
|
-
scope :published, {
|
|
40
|
+
scope( :published, proc { where(:published => true) })
|
|
41
41
|
else
|
|
42
42
|
named_scope :published, {:conditions => {:published => true}}
|
|
43
43
|
end
|
data/spec/enum/enum_spec.rb
CHANGED
|
@@ -4,33 +4,48 @@ describe ActiveHash::Base, "enum" do
|
|
|
4
4
|
|
|
5
5
|
before do
|
|
6
6
|
ActiveYaml::Base.set_root_path File.expand_path(File.dirname(__FILE__) + "/../fixtures")
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
class Borough < ActiveYaml::Base
|
|
9
9
|
include ActiveHash::Enum
|
|
10
10
|
fields :name, :county, :population
|
|
11
11
|
enum_accessor :name
|
|
12
12
|
end
|
|
13
|
+
|
|
14
|
+
class Neighborhood < ActiveHash::Base
|
|
15
|
+
include ActiveHash::Enum
|
|
16
|
+
fields :name, :county
|
|
17
|
+
enum_accessor :name, :county
|
|
18
|
+
|
|
19
|
+
self.data = [
|
|
20
|
+
{name: "Queen Ann", county: "King"}
|
|
21
|
+
]
|
|
22
|
+
end
|
|
13
23
|
end
|
|
14
24
|
|
|
15
25
|
after do
|
|
16
26
|
Object.send(:remove_const, :Borough)
|
|
27
|
+
Object.send(:remove_const, :Neighborhood)
|
|
17
28
|
end
|
|
18
29
|
|
|
19
30
|
describe "#enum_accessor" do
|
|
20
|
-
it "
|
|
31
|
+
it "can use a custom method" do
|
|
21
32
|
Borough::BROOKLYN.should == Borough.find_by_name("Brooklyn")
|
|
22
33
|
end
|
|
23
34
|
|
|
35
|
+
it "sets the field used for accessing records by constants" do
|
|
36
|
+
Neighborhood::QUEEN_ANN_KING.should == Neighborhood.find_by_name("Queen Ann")
|
|
37
|
+
end
|
|
38
|
+
|
|
24
39
|
it "ensures that values stored in the field specified are unique" do
|
|
25
40
|
lambda do
|
|
26
41
|
Class.new(ActiveHash::Base) do
|
|
27
42
|
include ActiveHash::Enum
|
|
28
43
|
self.data = [
|
|
29
|
-
{
|
|
30
|
-
{
|
|
31
|
-
{
|
|
44
|
+
{:name => 'Woodford Reserve'},
|
|
45
|
+
{:name => 'Bulliet Bourbon'},
|
|
46
|
+
{:name => 'Woodford Reserve'}
|
|
32
47
|
]
|
|
33
|
-
enum_accessor :name
|
|
48
|
+
enum_accessor :name
|
|
34
49
|
end
|
|
35
50
|
end.should raise_error(ActiveHash::Enum::DuplicateEnumAccessor)
|
|
36
51
|
end
|
|
@@ -41,7 +56,7 @@ describe ActiveHash::Base, "enum" do
|
|
|
41
56
|
self.data = [
|
|
42
57
|
{:name => 'Die Hard 2', :rating => '4.3'},
|
|
43
58
|
{:name => 'The Informant!', :rating => '4.3'},
|
|
44
|
-
{:name => 'In & Out', :rating => '4.3'}
|
|
59
|
+
{:name => 'In & Out', :rating => '4.3'}
|
|
45
60
|
]
|
|
46
61
|
enum_accessor :name
|
|
47
62
|
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.9.
|
|
4
|
+
version: 0.9.14
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -148,6 +148,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
148
148
|
- - ! '>='
|
|
149
149
|
- !ruby/object:Gem::Version
|
|
150
150
|
version: '0'
|
|
151
|
+
segments:
|
|
152
|
+
- 0
|
|
153
|
+
hash: 385675444061652865
|
|
151
154
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
155
|
none: false
|
|
153
156
|
requirements:
|
|
@@ -156,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
156
159
|
version: '0'
|
|
157
160
|
requirements: []
|
|
158
161
|
rubyforge_project:
|
|
159
|
-
rubygems_version: 1.8.
|
|
162
|
+
rubygems_version: 1.8.25
|
|
160
163
|
signing_key:
|
|
161
164
|
specification_version: 3
|
|
162
165
|
summary: An ActiveRecord-like model that uses a hash or file as a datasource
|