ar-enums 0.3.2 → 0.3.3
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/VERSION +1 -1
- data/ar-enums.gemspec +2 -2
- data/lib/enum.rb +1 -1
- data/lib/enum_block.rb +3 -2
- data/lib/enum_definition.rb +2 -4
- data/lib/factory.rb +44 -22
- data/spec/enum_definition_spec.rb +14 -7
- data/spec/factory_spec.rb +1 -1
- data/spec/spec.opts +1 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.3
|
data/ar-enums.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ar-enums}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Emmanuel Nicolau"]
|
12
|
-
s.date = %q{2010-08-
|
12
|
+
s.date = %q{2010-08-12}
|
13
13
|
s.description = %q{Provides a simple way for defining enumerations in ActiveRecord models}
|
14
14
|
s.email = %q{emmanicolau@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/enum.rb
CHANGED
data/lib/enum_block.rb
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Enumerations
|
3
3
|
class EnumBlock
|
4
|
-
def initialize options = {}
|
4
|
+
def initialize enum_class, options = {}
|
5
5
|
@enums = []
|
6
6
|
@last_id = 0
|
7
|
+
@enum_class = enum_class
|
7
8
|
@options = options
|
8
9
|
end
|
9
10
|
|
10
11
|
def method_missing method, args = {}
|
11
12
|
attrs = @options.merge(args).merge(:name => method)
|
12
13
|
attrs[:id] ||= @last_id += 1
|
13
|
-
@enums << @
|
14
|
+
@enums << @enum_class.new(attrs)
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
data/lib/enum_definition.rb
CHANGED
@@ -9,10 +9,8 @@ module ActiveRecord
|
|
9
9
|
|
10
10
|
def enum field_name, *config, &block
|
11
11
|
field = EnumField.new field_name
|
12
|
-
|
13
|
-
|
14
|
-
add_options config, :enum_class => enum_class, :on_style_not_matched => asume_external_style(field)
|
15
|
-
enums = Factory.new.make_enums *config, &block
|
12
|
+
add_option config, :field => field, :active_record => self
|
13
|
+
enums = Factory.make_enums *config, &block
|
16
14
|
define_enums_getter field, enums
|
17
15
|
define_enum_getter_and_setter field, enums
|
18
16
|
end
|
data/lib/factory.rb
CHANGED
@@ -1,41 +1,63 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module Enumerations
|
3
3
|
class Factory
|
4
|
-
|
4
|
+
extend OptionsHelper
|
5
5
|
|
6
6
|
def self.make_enums *config, &block
|
7
|
-
|
7
|
+
values, options = extract_values_and_options config
|
8
|
+
new(values, options, &block).make_enums
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize values, options, &block
|
12
|
+
@values, @options, @block = values, options, block
|
13
|
+
@active_record = @options.delete :active_record
|
14
|
+
@field = @options.delete :field
|
15
|
+
@class_name = @options.delete(:class_name) || @field.name.camelize
|
16
|
+
@label_method = @options.delete(:label) || :desc
|
8
17
|
end
|
9
18
|
|
10
|
-
def make_enums
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
define_extra_columns_methods options[:enum_class], enums
|
19
|
+
def make_enums
|
20
|
+
enum_class.label_method = @label_method
|
21
|
+
create_enums.tap do |enums|
|
22
|
+
define_question_methods enums
|
23
|
+
define_extra_columns_methods enums
|
16
24
|
end
|
17
25
|
end
|
26
|
+
|
27
|
+
def enum_class
|
28
|
+
@enum_class ||= eval_external_class || create_inner_enum_class
|
29
|
+
end
|
18
30
|
|
19
|
-
private
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
31
|
+
private
|
32
|
+
def eval_external_class
|
33
|
+
@class_name.is_a?(String) || @class_name.is_a?(Symbol) ? @active_record.send(:compute_type, @class_name) : @class_name
|
34
|
+
rescue NameError
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_inner_enum_class
|
39
|
+
@active_record.const_set @class_name, Class.new(Enum)
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_enums
|
43
|
+
enums = if @block
|
44
|
+
block_style
|
45
|
+
elsif @values.any?
|
46
|
+
array_of_values_or_hashes_style
|
47
|
+
else
|
48
|
+
enum_class.all
|
27
49
|
end
|
28
50
|
end
|
29
51
|
|
30
|
-
def block_style
|
31
|
-
EnumBlock.new(options).instance_eval(
|
52
|
+
def block_style
|
53
|
+
EnumBlock.new(enum_class, @options).instance_eval(&@block)
|
32
54
|
end
|
33
55
|
|
34
|
-
def array_of_values_or_hashes_style
|
35
|
-
values.map { |value|
|
56
|
+
def array_of_values_or_hashes_style
|
57
|
+
@values.map { |value| enum_class.create_from(value, @values, @options) }
|
36
58
|
end
|
37
59
|
|
38
|
-
def define_question_methods
|
60
|
+
def define_question_methods enums
|
39
61
|
enums.each do |e|
|
40
62
|
enum_class.class_eval %Q{
|
41
63
|
def #{e.name}?
|
@@ -45,7 +67,7 @@ module ActiveRecord
|
|
45
67
|
end
|
46
68
|
end
|
47
69
|
|
48
|
-
def define_extra_columns_methods
|
70
|
+
def define_extra_columns_methods enums
|
49
71
|
extra_columns_names = enums.map(&:extra_columns).map(&:keys).flatten.uniq
|
50
72
|
extra_columns_names.each do |ecn|
|
51
73
|
enum_class.class_eval %Q{
|
@@ -83,23 +83,23 @@ end
|
|
83
83
|
|
84
84
|
describe "External enumerations" do
|
85
85
|
before do
|
86
|
-
define_model_class '
|
86
|
+
define_model_class 'State', 'ActiveRecord::Enum' do
|
87
87
|
enumeration do
|
88
88
|
ca
|
89
89
|
tx
|
90
90
|
end
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
define_model_class 'Country' do
|
94
|
-
enum :state
|
95
|
-
end
|
94
|
+
enum :state
|
95
|
+
end
|
96
96
|
|
97
97
|
define_model_class 'TrafficLightState', 'ActiveRecord::Enum' do
|
98
98
|
enumeration do
|
99
99
|
green :rgb => 0x0F0
|
100
100
|
red :rgb => 0xF00
|
101
101
|
end
|
102
|
-
end
|
102
|
+
end
|
103
103
|
|
104
104
|
define_model_class 'TrafficLight' do
|
105
105
|
enum :state, :class_name => 'TrafficLightState'
|
@@ -113,7 +113,7 @@ describe "External enumerations" do
|
|
113
113
|
|
114
114
|
it "should be posible to access all enums from withing the owner" do
|
115
115
|
TrafficLight.states.should equal(TrafficLightState.all)
|
116
|
-
Country.states.should equal(
|
116
|
+
Country.states.should equal(State.all)
|
117
117
|
end
|
118
118
|
|
119
119
|
it "should accept :class_name options to override de class of the external enum" do
|
@@ -139,6 +139,13 @@ describe "External enumerations" do
|
|
139
139
|
def double_factor() factor * 2 end
|
140
140
|
end
|
141
141
|
State.all.map(&:double_factor).should == [2, 4]
|
142
|
-
end
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should not define new constant form enum class" do
|
145
|
+
define_model_class 'TrafficLight' do
|
146
|
+
enum :estado, :class_name => 'TrafficLightState'
|
147
|
+
end
|
148
|
+
expect { TrafficLight.const_get(:Estado) }.to raise_error NameError
|
149
|
+
end
|
143
150
|
end
|
144
151
|
end
|
data/spec/factory_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe "Enums creation styles" do
|
|
4
4
|
include ActiveRecord::Enumerations::OptionsHelper
|
5
5
|
|
6
6
|
def make_enums *config, &block
|
7
|
-
add_option config, :
|
7
|
+
add_option config, :class_name => ActiveRecord::Enum
|
8
8
|
ActiveRecord::Enumerations::Factory.make_enums *config, &block
|
9
9
|
end
|
10
10
|
|
data/spec/spec.opts
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 3
|
9
|
+
version: 0.3.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Emmanuel Nicolau
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-08-
|
17
|
+
date: 2010-08-12 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|