ar-enums 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rbenv-version +1 -0
- data/Gemfile.lock +33 -18
- data/Guardfile +21 -0
- data/README.rdoc +2 -2
- data/ar_enums.gemspec +2 -0
- data/lib/ar-enums.rb +1 -1
- data/lib/ar_enums/{enum.rb → base.rb} +7 -7
- data/lib/ar_enums/enum_block.rb +13 -15
- data/lib/ar_enums/enum_definition.rb +6 -5
- data/lib/ar_enums/enum_field.rb +18 -20
- data/lib/ar_enums/factory.rb +70 -72
- data/lib/ar_enums/options_helper.rb +16 -18
- data/lib/ar_enums/version.rb +1 -1
- data/spec/ar_enums/{enum_spec.rb → base_spec.rb} +20 -20
- data/spec/ar_enums/enum_definition_spec.rb +3 -3
- data/spec/ar_enums/factory_spec.rb +28 -28
- data/spec/spec_helper.rb +1 -1
- metadata +71 -38
- data/.rvmrc +0 -1
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c34a6a56500c6697165a3252f1509a54cbfee915
|
4
|
+
data.tar.gz: 88e8e2f99106d56c1c596cf758d7f80398bac27d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f9bc984e495b93905ca602534a0248f405ec336db1d08318fef5ecb25111432e8a5c6430b4d39d5c9d68fdf201cd4dcbc8fde72f0b4cab051c8aee4411182250
|
7
|
+
data.tar.gz: 09f67c296f366917fcdecc0b5d2f99405517431e10ed815aa601c5a64564d2f7e208fdd0ca3fd7eb7386715d8b901173c00e8d8c09f5c72182df554ba3ef447a
|
data/.rbenv-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.0
|
data/Gemfile.lock
CHANGED
@@ -1,30 +1,40 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ar-enums (
|
4
|
+
ar-enums (2.0.0)
|
5
5
|
activerecord (>= 3.1.0)
|
6
6
|
rake
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: http://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (
|
12
|
-
activesupport (=
|
13
|
-
builder (~> 3.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
11
|
+
activemodel (4.1.4)
|
12
|
+
activesupport (= 4.1.4)
|
13
|
+
builder (~> 3.1)
|
14
|
+
activerecord (4.1.4)
|
15
|
+
activemodel (= 4.1.4)
|
16
|
+
activesupport (= 4.1.4)
|
17
|
+
arel (~> 5.0.0)
|
18
|
+
activesupport (4.1.4)
|
19
|
+
i18n (~> 0.6, >= 0.6.9)
|
20
|
+
json (~> 1.7, >= 1.7.7)
|
21
|
+
minitest (~> 5.1)
|
22
|
+
thread_safe (~> 0.1)
|
23
|
+
tzinfo (~> 1.1)
|
24
|
+
arel (5.0.1.20140414130214)
|
25
|
+
builder (3.2.2)
|
24
26
|
diff-lcs (1.1.2)
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
guard (1.4.0)
|
28
|
+
listen (>= 0.4.2)
|
29
|
+
thor (>= 0.14.6)
|
30
|
+
guard-rspec (0.4.5)
|
31
|
+
guard (>= 0.4.0)
|
32
|
+
i18n (0.6.11)
|
33
|
+
json (1.8.1)
|
34
|
+
listen (0.5.3)
|
35
|
+
minitest (5.4.0)
|
36
|
+
rake (10.3.2)
|
37
|
+
rb-fsevent (0.9.3)
|
28
38
|
rspec (2.4.0)
|
29
39
|
rspec-core (~> 2.4.0)
|
30
40
|
rspec-expectations (~> 2.4.0)
|
@@ -34,12 +44,17 @@ GEM
|
|
34
44
|
diff-lcs (~> 1.1.2)
|
35
45
|
rspec-mocks (2.4.0)
|
36
46
|
sqlite3 (1.3.4)
|
37
|
-
|
47
|
+
thor (0.19.1)
|
48
|
+
thread_safe (0.3.4)
|
49
|
+
tzinfo (1.2.1)
|
50
|
+
thread_safe (~> 0.1)
|
38
51
|
|
39
52
|
PLATFORMS
|
40
53
|
ruby
|
41
54
|
|
42
55
|
DEPENDENCIES
|
43
56
|
ar-enums!
|
57
|
+
guard-rspec
|
58
|
+
rb-fsevent
|
44
59
|
rspec
|
45
60
|
sqlite3 (>= 1.3.4)
|
data/Guardfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^spec/.+_spec\.rb$})
|
11
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
12
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
13
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
14
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
15
|
+
watch('spec/spec_helper.rb') { "spec" }
|
16
|
+
watch('config/routes.rb') { "spec/routing" }
|
17
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
18
|
+
# Capybara request specs
|
19
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
|
20
|
+
end
|
21
|
+
|
data/README.rdoc
CHANGED
@@ -27,7 +27,7 @@ This is the simplest and probably the most frecuent case:
|
|
27
27
|
tl.state_id # => 2
|
28
28
|
tl.state.green? # => true
|
29
29
|
|
30
|
-
As you can see each enum value is an instance of a dinamically generated class (which inherits from
|
30
|
+
As you can see each enum value is an instance of a dinamically generated class (which inherits from ArEnums::Base) and has an ordinal value, id, autogenerated staring on 1.
|
31
31
|
|
32
32
|
The enumerations array is accessed with a class method:
|
33
33
|
|
@@ -69,7 +69,7 @@ the enumeration can be on it's own class, as the State enum in the following exa
|
|
69
69
|
enum :state
|
70
70
|
end
|
71
71
|
|
72
|
-
class State <
|
72
|
+
class State < ArEnums::Base
|
73
73
|
enumeration do
|
74
74
|
red :stop_traffic => true, :rgb => 0xF00, :desc => 'Rojo'
|
75
75
|
green :stop_traffic => false, :rgb => 0x0F0, :desc => 'Verde'
|
data/ar_enums.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
|
20
20
|
s.add_development_dependency "rspec"
|
21
|
+
s.add_development_dependency "guard-rspec"
|
22
|
+
s.add_development_dependency "rb-fsevent"
|
21
23
|
# s.add_development_dependency "ruby-debug19"
|
22
24
|
s.add_development_dependency "sqlite3", ">= 1.3.4"
|
23
25
|
s.add_dependency "activerecord", ">= 3.1.0"
|
data/lib/ar-enums.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
require 'active_record'
|
2
|
-
%w[metaprogramming_extensions options_helper
|
2
|
+
%w[metaprogramming_extensions options_helper base enum_block enum_field factory enum_definition].each do |f|
|
3
3
|
require "ar_enums/#{f}"
|
4
4
|
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
module
|
2
|
-
class
|
3
|
-
extend
|
1
|
+
module ArEnums
|
2
|
+
class Base
|
3
|
+
extend OptionsHelper
|
4
4
|
|
5
5
|
attr_reader :id, :name, :extra_columns
|
6
6
|
class_attribute :label_method
|
@@ -14,7 +14,7 @@ module ActiveRecord
|
|
14
14
|
def self.create_from value, values, options
|
15
15
|
required_attrs = case value
|
16
16
|
when String, Symbol
|
17
|
-
{ :
|
17
|
+
{ name: value }
|
18
18
|
else
|
19
19
|
value
|
20
20
|
end
|
@@ -23,7 +23,7 @@ module ActiveRecord
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def == other
|
26
|
-
return id == other.id if other.is_a?(
|
26
|
+
return id == other.id if other.is_a?(Base)
|
27
27
|
[id.to_s, name].include?(other.to_s)
|
28
28
|
end
|
29
29
|
alias_method :eql?, :==
|
@@ -41,8 +41,8 @@ module ActiveRecord
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def self.enumeration *config, &block
|
44
|
-
add_option config, :
|
45
|
-
define_enums_getter
|
44
|
+
add_option config, class_name: self
|
45
|
+
define_enums_getter ArEnums::Factory.make_enums(*config, &block)
|
46
46
|
end
|
47
47
|
|
48
48
|
def self.[] name_or_id
|
data/lib/ar_enums/enum_block.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@enums << @enum_class.new(attrs)
|
15
|
-
end
|
1
|
+
module ArEnums
|
2
|
+
class EnumBlock
|
3
|
+
def initialize enum_class, options = {}
|
4
|
+
@enums = []
|
5
|
+
@last_id = 0
|
6
|
+
@enum_class = enum_class
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing method, args = {}
|
11
|
+
attrs = @options.merge(args).merge(:name => method)
|
12
|
+
attrs[:id] ||= @last_id += 1
|
13
|
+
@enums << @enum_class.new(attrs)
|
16
14
|
end
|
17
15
|
end
|
18
16
|
end
|
@@ -1,21 +1,22 @@
|
|
1
|
-
module
|
2
|
-
module
|
1
|
+
module ArEnums
|
2
|
+
module EnumDefinition
|
3
3
|
def self.included(base)
|
4
4
|
base.send :extend, ClassMethods
|
5
5
|
end
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
-
include
|
8
|
+
include ArEnums::OptionsHelper
|
9
9
|
|
10
10
|
def enum field_name, *config, &block
|
11
11
|
field = EnumField.new field_name
|
12
|
-
add_option config, :
|
12
|
+
add_option config, field: field, active_record: self
|
13
13
|
enums = Factory.make_enums *config, &block
|
14
14
|
define_enums_getter field, enums
|
15
15
|
define_enum_getter_and_setter field, enums
|
16
16
|
end
|
17
17
|
|
18
18
|
private
|
19
|
+
|
19
20
|
def asume_external_style field
|
20
21
|
lambda { |options| external_class(field, options).all }
|
21
22
|
end
|
@@ -41,4 +42,4 @@ module ActiveRecord
|
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
|
-
ActiveRecord::Base.send :include,
|
45
|
+
ActiveRecord::Base.send :include, ArEnums::EnumDefinition
|
data/lib/ar_enums/enum_field.rb
CHANGED
@@ -1,23 +1,21 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@name = name.to_s
|
8
|
-
end
|
9
|
-
|
10
|
-
def enums_getter
|
11
|
-
name.pluralize
|
12
|
-
end
|
13
|
-
|
14
|
-
def enums_setter
|
15
|
-
"#{enums_getter}="
|
16
|
-
end
|
17
|
-
|
18
|
-
def foreign_key
|
19
|
-
"#{name}_id"
|
20
|
-
end
|
1
|
+
module ArEnums
|
2
|
+
class EnumField
|
3
|
+
attr_reader :name
|
4
|
+
|
5
|
+
def initialize name
|
6
|
+
@name = name.to_s
|
21
7
|
end
|
8
|
+
|
9
|
+
def enums_getter
|
10
|
+
name.pluralize
|
11
|
+
end
|
12
|
+
|
13
|
+
def enums_setter
|
14
|
+
"#{enums_getter}="
|
15
|
+
end
|
16
|
+
|
17
|
+
def foreign_key
|
18
|
+
"#{name}_id"
|
19
|
+
end
|
22
20
|
end
|
23
21
|
end
|
data/lib/ar_enums/factory.rb
CHANGED
@@ -1,82 +1,80 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
extend OptionsHelper
|
5
|
-
|
6
|
-
def self.make_enums *config, &block
|
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
|
17
|
-
end
|
1
|
+
module ArEnums
|
2
|
+
class Factory
|
3
|
+
extend OptionsHelper
|
18
4
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
define_extra_columns_methods enums
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def enum_class
|
28
|
-
@enum_class ||= eval_external_class || create_inner_enum_class
|
29
|
-
end
|
5
|
+
def self.make_enums *config, &block
|
6
|
+
values, options = extract_values_and_options config
|
7
|
+
new(values, options, &block).make_enums
|
8
|
+
end
|
30
9
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
block_style
|
45
|
-
elsif @values.any?
|
46
|
-
array_of_values_or_hashes_style
|
47
|
-
else
|
48
|
-
enum_class.all
|
49
|
-
end
|
10
|
+
def initialize values, options, &block
|
11
|
+
@values, @options, @block = values, options, block
|
12
|
+
@active_record = @options.delete :active_record
|
13
|
+
@field = @options.delete :field
|
14
|
+
@class_name = @options.delete(:class_name) || @field.name.camelize
|
15
|
+
@label_method = @options.delete(:label) || :desc
|
16
|
+
end
|
17
|
+
|
18
|
+
def make_enums
|
19
|
+
enum_class.label_method = @label_method
|
20
|
+
create_enums.tap do |enums|
|
21
|
+
define_question_methods enums
|
22
|
+
define_extra_columns_methods enums
|
50
23
|
end
|
24
|
+
end
|
51
25
|
|
52
|
-
|
53
|
-
|
54
|
-
|
26
|
+
def enum_class
|
27
|
+
@enum_class ||= eval_external_class || create_inner_enum_class
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def eval_external_class
|
32
|
+
@class_name.is_a?(String) || @class_name.is_a?(Symbol) ? @active_record.send(:compute_type, @class_name) : @class_name
|
33
|
+
rescue NameError
|
34
|
+
nil
|
35
|
+
end
|
55
36
|
|
56
|
-
|
57
|
-
|
37
|
+
def create_inner_enum_class
|
38
|
+
@active_record.const_set @class_name, Class.new(Base)
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_enums
|
42
|
+
if @block
|
43
|
+
block_style
|
44
|
+
elsif @values.any?
|
45
|
+
array_of_values_or_hashes_style
|
46
|
+
else
|
47
|
+
enum_class.all
|
58
48
|
end
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
49
|
+
end
|
50
|
+
|
51
|
+
def block_style
|
52
|
+
EnumBlock.new(enum_class, @options).instance_eval(&@block)
|
53
|
+
end
|
54
|
+
|
55
|
+
def array_of_values_or_hashes_style
|
56
|
+
@values.map { |value| enum_class.create_from(value, @values, @options) }
|
57
|
+
end
|
58
|
+
|
59
|
+
def define_question_methods enums
|
60
|
+
enums.each do |e|
|
61
|
+
enum_class.class_eval %Q{
|
62
|
+
def #{e.name}?
|
63
|
+
self == :#{e.name}
|
64
|
+
end
|
65
|
+
}
|
68
66
|
end
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
67
|
+
end
|
68
|
+
|
69
|
+
def define_extra_columns_methods enums
|
70
|
+
extra_columns_names = enums.map(&:extra_columns).map(&:keys).flatten.uniq
|
71
|
+
extra_columns_names.each do |ecn|
|
72
|
+
enum_class.class_eval %Q{
|
73
|
+
def #{ecn}
|
74
|
+
extra_columns[:#{ecn}]
|
75
|
+
end
|
76
|
+
}
|
79
77
|
end
|
80
78
|
end
|
81
79
|
end
|
82
|
-
end
|
80
|
+
end
|
@@ -1,22 +1,20 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
[(config[0] || {}).merge(option)]
|
9
|
-
end
|
10
|
-
config.replace new_config
|
1
|
+
module ArEnums
|
2
|
+
module OptionsHelper
|
3
|
+
def add_option config, option
|
4
|
+
new_config = if config.first.is_a?(Array)
|
5
|
+
[config[0], (config[1] || {}).merge(option)]
|
6
|
+
else
|
7
|
+
[(config[0] || {}).merge(option)]
|
11
8
|
end
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
config.replace new_config
|
10
|
+
end
|
11
|
+
alias_method :add_options, :add_option
|
12
|
+
|
13
|
+
def extract_values_and_options config
|
14
|
+
if config.first.is_a?(Array)
|
15
|
+
[config[0], config[1] || {}]
|
16
|
+
else
|
17
|
+
[[], config[0] || {}]
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
data/lib/ar_enums/version.rb
CHANGED
@@ -2,42 +2,42 @@ require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
|
2
2
|
|
3
3
|
describe "Enum" do
|
4
4
|
it "should provide :to_sym method returning name as symbols" do
|
5
|
-
|
5
|
+
ArEnums::Base.new(name: :green).to_sym.should == :green
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should store extra columns as a hash without the :enum_class that is passed from other classes" do
|
9
|
-
|
9
|
+
ArEnums::Base.new(name: :green, factor: 1.5, enum_class: Class.new).extra_columns.should == { factor: 1.5 }
|
10
10
|
end
|
11
11
|
|
12
12
|
context "External enums" do
|
13
13
|
before do
|
14
|
-
define_model_class 'Color', '
|
14
|
+
define_model_class 'Color', 'ArEnums::Base' do
|
15
15
|
enumeration do
|
16
|
-
red :
|
17
|
-
green :
|
18
|
-
blue :
|
16
|
+
red rgb: 0xF00
|
17
|
+
green rgb: 0x0F0
|
18
|
+
blue rgb: 0x00F
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
define_model_class 'State', '
|
22
|
+
define_model_class 'State', 'ArEnums::Base' do
|
23
23
|
enumeration do
|
24
|
-
on :
|
25
|
-
off :
|
24
|
+
on id: 80
|
25
|
+
off id: 90
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should provide :all method to access the enums" do
|
31
|
-
Color.all[0].should be_enum_with(:
|
32
|
-
Color.all[1].should be_enum_with(:
|
33
|
-
State.all[0].should be_enum_with(:
|
34
|
-
State.all[1].should be_enum_with(:
|
31
|
+
Color.all[0].should be_enum_with(name: 'red', rgb: 0xF00)
|
32
|
+
Color.all[1].should be_enum_with(name: 'green', rgb: 0x0F0)
|
33
|
+
State.all[0].should be_enum_with(name: 'on', id: 80)
|
34
|
+
State.all[1].should be_enum_with(name: 'off', id: 90)
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should provide [] method to access the enums" do
|
38
|
-
Color[:red].should be_enum_with(:
|
39
|
-
Color['green'].should be_enum_with(:
|
40
|
-
Color[2].should be_enum_with(:
|
38
|
+
Color[:red].should be_enum_with(name: 'red')
|
39
|
+
Color['green'].should be_enum_with(name: 'green')
|
40
|
+
Color[2].should be_enum_with(name: 'green')
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -50,11 +50,11 @@ describe "Enum" do
|
|
50
50
|
end
|
51
51
|
|
52
52
|
it "in? should check if == to any" do
|
53
|
-
define_model_class 'Color', '
|
53
|
+
define_model_class 'Color', 'ArEnums::Base' do
|
54
54
|
enumeration do
|
55
|
-
red :
|
56
|
-
green :
|
57
|
-
blue :
|
55
|
+
red rgb: 0xF00
|
56
|
+
green rgb: 0x0F0
|
57
|
+
blue rgb: 0x00F
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -83,7 +83,7 @@ end
|
|
83
83
|
|
84
84
|
describe "External enumerations" do
|
85
85
|
before do
|
86
|
-
define_model_class 'State', '
|
86
|
+
define_model_class 'State', 'ArEnums::Base' do
|
87
87
|
enumeration do
|
88
88
|
ca
|
89
89
|
tx
|
@@ -94,7 +94,7 @@ describe "External enumerations" do
|
|
94
94
|
enum :state
|
95
95
|
end
|
96
96
|
|
97
|
-
define_model_class 'TrafficLightState', '
|
97
|
+
define_model_class 'TrafficLightState', 'ArEnums::Base' do
|
98
98
|
enumeration do
|
99
99
|
green :rgb => 0x0F0
|
100
100
|
red :rgb => 0xF00
|
@@ -130,7 +130,7 @@ describe "External enumerations" do
|
|
130
130
|
end
|
131
131
|
|
132
132
|
it "should be posible to define new methods in Enum subclass" do
|
133
|
-
define_model_class 'State', '
|
133
|
+
define_model_class 'State', 'ArEnums::Base' do
|
134
134
|
enumeration do
|
135
135
|
green :factor => 1
|
136
136
|
red :factor => 2
|
@@ -1,19 +1,19 @@
|
|
1
1
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
2
|
|
3
3
|
describe "Enums creation styles" do
|
4
|
-
include
|
4
|
+
include ArEnums::OptionsHelper
|
5
5
|
|
6
6
|
def make_enums *config, &block
|
7
|
-
add_option config, :
|
8
|
-
|
7
|
+
add_option config, class_name: ArEnums::Base
|
8
|
+
ArEnums::Factory.make_enums *config, &block
|
9
9
|
end
|
10
10
|
|
11
11
|
context "array of values style" do
|
12
12
|
it "should generate ids" do
|
13
13
|
enums = make_enums %w[red green blue]
|
14
|
-
enums[0].should be_enum_with(:
|
15
|
-
enums[1].should be_enum_with(:
|
16
|
-
enums[2].should be_enum_with(:
|
14
|
+
enums[0].should be_enum_with(id: 1, name: 'red')
|
15
|
+
enums[1].should be_enum_with(id: 2, name: 'green')
|
16
|
+
enums[2].should be_enum_with(id: 3, name: 'blue')
|
17
17
|
end
|
18
18
|
|
19
19
|
it "default to_s should be name titleized" do
|
@@ -21,42 +21,42 @@ describe "Enums creation styles" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should override default to_s" do
|
24
|
-
make_enums(%w[green red], :
|
24
|
+
make_enums(%w[green red], label: :upcase).map(&:to_s).should == %w[GREEN RED]
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
context "array of hashes style" do
|
29
29
|
it "should accept ids if provided" do
|
30
|
-
enums = make_enums [{ :
|
31
|
-
enums[0].should be_enum_with(:
|
32
|
-
enums[1].should be_enum_with(:
|
30
|
+
enums = make_enums [{ id: 20, name: :red }, { id: 10, name: :green }]
|
31
|
+
enums[0].should be_enum_with(id: 20, name: 'red')
|
32
|
+
enums[1].should be_enum_with(id: 10, name: 'green')
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should generate ids if not provided" do
|
36
|
-
enums = make_enums [{ :
|
37
|
-
enums[0].should be_enum_with(:
|
38
|
-
enums[1].should be_enum_with(:
|
36
|
+
enums = make_enums [{ name: :red }, { name: :green }]
|
37
|
+
enums[0].should be_enum_with(id: 1, name: 'red')
|
38
|
+
enums[1].should be_enum_with(id: 2, name: 'green')
|
39
39
|
end
|
40
40
|
|
41
41
|
it "default to_s should be :desc column" do
|
42
|
-
enums = make_enums [{ :
|
42
|
+
enums = make_enums [{ name: :red, desc: 'Rojo' }, { name: :green, desc: 'Verde' }]
|
43
43
|
enums.map(&:to_s).should == %w[Rojo Verde]
|
44
44
|
end
|
45
45
|
|
46
46
|
it ":label options can be a method to call on name" do
|
47
|
-
enums = make_enums [{ :
|
47
|
+
enums = make_enums [{ name: :red }, { name: :green }], label: :upcase
|
48
48
|
enums.map(&:to_s).should == %w[RED GREEN]
|
49
49
|
end
|
50
50
|
|
51
51
|
it ":label option can be a enum column" do
|
52
|
-
enums = make_enums [{ :
|
52
|
+
enums = make_enums [{ name: :red, title: 'Rojo' }, { name: :green, title: 'Verde' }], label: :title
|
53
53
|
enums.map(&:to_s).should == %w[Rojo Verde]
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should accept extra columns" do
|
57
57
|
enums = make_enums [
|
58
|
-
{ :
|
59
|
-
{ :
|
58
|
+
{ name: :red, factor: 1.5, stop_traffic: true },
|
59
|
+
{ name: :green, factor: 2.5, stop_traffic: false }
|
60
60
|
]
|
61
61
|
enums.map(&:factor).should == [1.5, 2.5]
|
62
62
|
enums.map(&:stop_traffic).should == [true, false]
|
@@ -66,32 +66,32 @@ describe "Enums creation styles" do
|
|
66
66
|
context "block style" do
|
67
67
|
it "can be created with a block" do
|
68
68
|
enums = make_enums do
|
69
|
-
red :
|
70
|
-
green :
|
69
|
+
red rgb: 0xF00
|
70
|
+
green rgb: 0x0F0
|
71
71
|
end
|
72
|
-
enums[0].should be_enum_with(:
|
73
|
-
enums[1].should be_enum_with(:
|
72
|
+
enums[0].should be_enum_with(id: 1, name: 'red', rgb: 0xF00)
|
73
|
+
enums[1].should be_enum_with(id: 2, name: 'green', rgb: 0x0F0)
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should accept :label option" do
|
77
|
-
enums = make_enums :
|
78
|
-
red :
|
79
|
-
green :
|
77
|
+
enums = make_enums label: :title do
|
78
|
+
red title: 'Rojo'
|
79
|
+
green title: 'Verde'
|
80
80
|
end
|
81
81
|
enums.map(&:to_s).should == %w[Rojo Verde]
|
82
82
|
end
|
83
83
|
|
84
84
|
it "should accept extra columns" do
|
85
85
|
enums = make_enums do
|
86
|
-
red :
|
87
|
-
green :
|
86
|
+
red factor: 1.5
|
87
|
+
green factor: 2.5
|
88
88
|
end
|
89
89
|
enums.map(&:factor).should == [1.5, 2.5]
|
90
90
|
end
|
91
91
|
|
92
92
|
it "when extra column is empty should return nil" do
|
93
93
|
enums = make_enums do
|
94
|
-
red :
|
94
|
+
red factor: 1.5
|
95
95
|
green
|
96
96
|
end
|
97
97
|
enums.map(&:factor).should == [1.5, nil]
|
data/spec/spec_helper.rb
CHANGED
@@ -17,7 +17,7 @@ end
|
|
17
17
|
|
18
18
|
RSpec::Matchers.define :be_enum_with do |expected_attrs|
|
19
19
|
match do |enum|
|
20
|
-
enum.should be_a(
|
20
|
+
enum.should be_a(ArEnums::Base)
|
21
21
|
expected_attrs.each do |atrib, expected_value|
|
22
22
|
enum.send(atrib).should == expected_value
|
23
23
|
end
|
metadata
CHANGED
@@ -1,60 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar-enums
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Emmanuel Nicolau
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-07-25 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rb-fsevent
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
25
55
|
- !ruby/object:Gem::Dependency
|
26
56
|
name: sqlite3
|
27
|
-
requirement:
|
28
|
-
none: false
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
29
58
|
requirements:
|
30
|
-
- -
|
59
|
+
- - ">="
|
31
60
|
- !ruby/object:Gem::Version
|
32
61
|
version: 1.3.4
|
33
62
|
type: :development
|
34
63
|
prerelease: false
|
35
|
-
version_requirements:
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.3.4
|
36
69
|
- !ruby/object:Gem::Dependency
|
37
70
|
name: activerecord
|
38
|
-
requirement:
|
39
|
-
none: false
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
40
72
|
requirements:
|
41
|
-
- -
|
73
|
+
- - ">="
|
42
74
|
- !ruby/object:Gem::Version
|
43
75
|
version: 3.1.0
|
44
76
|
type: :runtime
|
45
77
|
prerelease: false
|
46
|
-
version_requirements:
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.1.0
|
47
83
|
- !ruby/object:Gem::Dependency
|
48
84
|
name: rake
|
49
|
-
requirement:
|
50
|
-
none: false
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
51
86
|
requirements:
|
52
|
-
- -
|
87
|
+
- - ">="
|
53
88
|
- !ruby/object:Gem::Version
|
54
89
|
version: '0'
|
55
90
|
type: :runtime
|
56
91
|
prerelease: false
|
57
|
-
version_requirements:
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
58
97
|
description:
|
59
98
|
email:
|
60
99
|
- emmanicolau@gmail.com
|
@@ -62,12 +101,13 @@ executables: []
|
|
62
101
|
extensions: []
|
63
102
|
extra_rdoc_files: []
|
64
103
|
files:
|
65
|
-
- .autotest
|
66
|
-
- .gitignore
|
67
|
-
- .
|
68
|
-
- .
|
104
|
+
- ".autotest"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rbenv-version"
|
107
|
+
- ".rspec"
|
69
108
|
- Gemfile
|
70
109
|
- Gemfile.lock
|
110
|
+
- Guardfile
|
71
111
|
- LICENSE
|
72
112
|
- README.rdoc
|
73
113
|
- Rakefile
|
@@ -75,7 +115,7 @@ files:
|
|
75
115
|
- autotest/discover.rb
|
76
116
|
- examples/internal_enums.rb
|
77
117
|
- lib/ar-enums.rb
|
78
|
-
- lib/ar_enums/
|
118
|
+
- lib/ar_enums/base.rb
|
79
119
|
- lib/ar_enums/enum_block.rb
|
80
120
|
- lib/ar_enums/enum_definition.rb
|
81
121
|
- lib/ar_enums/enum_field.rb
|
@@ -83,44 +123,37 @@ files:
|
|
83
123
|
- lib/ar_enums/metaprogramming_extensions.rb
|
84
124
|
- lib/ar_enums/options_helper.rb
|
85
125
|
- lib/ar_enums/version.rb
|
126
|
+
- spec/ar_enums/base_spec.rb
|
86
127
|
- spec/ar_enums/enum_definition_spec.rb
|
87
|
-
- spec/ar_enums/enum_spec.rb
|
88
128
|
- spec/ar_enums/factory_spec.rb
|
89
129
|
- spec/schema.rb
|
90
130
|
- spec/spec_helper.rb
|
91
131
|
homepage:
|
92
132
|
licenses: []
|
133
|
+
metadata: {}
|
93
134
|
post_install_message:
|
94
135
|
rdoc_options: []
|
95
136
|
require_paths:
|
96
137
|
- lib
|
97
138
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
139
|
requirements:
|
100
|
-
- -
|
140
|
+
- - ">="
|
101
141
|
- !ruby/object:Gem::Version
|
102
142
|
version: '0'
|
103
|
-
segments:
|
104
|
-
- 0
|
105
|
-
hash: -209326541352629550
|
106
143
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
-
none: false
|
108
144
|
requirements:
|
109
|
-
- -
|
145
|
+
- - ">="
|
110
146
|
- !ruby/object:Gem::Version
|
111
147
|
version: '0'
|
112
|
-
segments:
|
113
|
-
- 0
|
114
|
-
hash: -209326541352629550
|
115
148
|
requirements: []
|
116
149
|
rubyforge_project: ar-enums
|
117
|
-
rubygems_version:
|
150
|
+
rubygems_version: 2.4.1
|
118
151
|
signing_key:
|
119
|
-
specification_version:
|
152
|
+
specification_version: 4
|
120
153
|
summary: Provides a simple way for defining enumerations in ActiveRecord models
|
121
154
|
test_files:
|
155
|
+
- spec/ar_enums/base_spec.rb
|
122
156
|
- spec/ar_enums/enum_definition_spec.rb
|
123
|
-
- spec/ar_enums/enum_spec.rb
|
124
157
|
- spec/ar_enums/factory_spec.rb
|
125
158
|
- spec/schema.rb
|
126
159
|
- spec/spec_helper.rb
|
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm 1.9.3-p0
|