active_enum 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -43,11 +43,21 @@ Beware that if you change the order of values defined in an enum which don't hav
43
43
  This could corrupt your data if the enum values have been stored in a model record, as they will no longer map to
44
44
  the original enum.
45
45
 
46
+ Define values with meta data
47
+
48
+ class Sex < ActiveEnum::Base
49
+ value :id => 1, :name => 'Male', :symbol => '♂'
50
+ value :id => 2, :name => 'Female', :symbol => '♀'
51
+ end
52
+
53
+ The meta data can be any other key-value pairs, it's not limited to certain keys.
54
+
46
55
  Enum class usage
47
56
 
48
57
  Sex[1] # => 'Male'
49
58
  Sex['Male'] # => 1
50
59
  Sex[:male] # => 1
60
+ Sex.meta(1) # => { :symbol => '♂' }
51
61
  Sex.to_select # => [['Male', 1], ['Female',2]] for select form helpers
52
62
 
53
63
  === Ordering
@@ -100,11 +110,12 @@ Access the enum values and the enum class using the attribute method with a symb
100
110
 
101
111
  user = User.new
102
112
  user.sex = 1
103
- user.sex # => 1
113
+ user.sex # => 1
104
114
 
105
- user.sex(:id) # => 1
106
- user.sex(:name) # => 'Male'
107
- user.sex(:enum) # => Sex
115
+ user.sex(:id) # => 1
116
+ user.sex(:name) # => 'Male'
117
+ user.sex(:enum) # => Sex
118
+ user.sex(:symbol) # => ♂. Can use any meta data key.
108
119
 
109
120
  You can set the default to return the enum name value for enumerated attribute
110
121
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{active_enum}
5
- s.version = "0.8.0"
5
+ s.version = "0.8.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Adam Meehan"]
@@ -60,13 +60,14 @@ module ActiveEnum
60
60
  # user.sex(:id)
61
61
  # user.sex(:name)
62
62
  # user.sex(:enum)
63
+ # user.sex(:meta_key)
63
64
  #
64
65
  def define_active_enum_read_method(attribute)
65
66
  define_method("#{attribute}") do |*arg|
66
- arg = arg.first
67
+ arg = arg.first
67
68
  value = super()
69
+ enum = self.class.enum_for(attribute)
68
70
 
69
- enum = self.class.enum_for(attribute)
70
71
  case arg
71
72
  when :id
72
73
  value if enum[value]
@@ -74,6 +75,8 @@ module ActiveEnum
74
75
  enum[value]
75
76
  when :enum
76
77
  enum
78
+ when Symbol
79
+ (enum.meta(value) || {})[arg]
77
80
  else
78
81
  ActiveEnum.use_name_as_value ? enum[value] : value
79
82
  end
@@ -27,12 +27,13 @@ module ActiveEnum
27
27
 
28
28
  def sort!
29
29
  return if @order == :as_defined
30
- case @order
31
- when :asc
32
- values.sort! {|a,b| a[0] <=> b[0] }
33
- when :desc
34
- values.sort! {|a,b| b[0] <=> a[0] }
35
- end
30
+
31
+ case @order
32
+ when :asc
33
+ values.sort! {|a,b| a[0] <=> b[0] }
34
+ when :desc
35
+ values.sort! {|a,b| b[0] <=> a[0] }
36
+ end
36
37
  end
37
38
  end
38
39
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '0.8.0'
2
+ VERSION = '0.8.1'
3
3
  end
@@ -124,6 +124,34 @@ describe ActiveEnum::Extensions do
124
124
  end
125
125
  end
126
126
 
127
+ describe "with meta data" do
128
+ before(:all) do
129
+ reset_class Person do
130
+ enumerate :sex do
131
+ value :id => 1, :name => 'Male', :description => 'Man'
132
+ value :id => 2, :name => 'Female', :description => 'Woman'
133
+ end
134
+ end
135
+ end
136
+
137
+ before do
138
+ @person = Person.new(:sex =>1)
139
+ end
140
+
141
+ it 'should return meta value for existing key' do
142
+ @person.sex(:description).should == 'Man'
143
+ end
144
+
145
+ it 'should return nil for missing meta value' do
146
+ @person.sex(:nonexistent).should be_nil
147
+ end
148
+
149
+ it 'should return nil for missing index' do
150
+ @person.sex = nil
151
+ @person.sex(:description).should be_nil
152
+ end
153
+ end
154
+
127
155
  describe "question method" do
128
156
  before do
129
157
  @person.sex = 1
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- hash: 63
4
+ hash: 61
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 8
9
- - 0
10
- version: 0.8.0
9
+ - 1
10
+ version: 0.8.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Meehan