active_enum 0.9.9 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ [0.9.11] : 2012-08-18
2
+ - Use the name key as default value for the name in I18n storage. [kengos]
3
+
4
+ [0.9.10] : 2012-07-18
5
+ - Fix I18n storage for meta data. [kengos]
6
+
1
7
  [0.9.9] : 2012-07-08
2
8
  - Really fix Railtie issue this time.
3
9
 
data/README.rdoc CHANGED
@@ -108,6 +108,20 @@ Multiple attributes with same enum
108
108
  enumerate :to, :from, :with => Sex
109
109
  end
110
110
 
111
+
112
+ === Validations
113
+
114
+ You can use an enum in a validation of inclusion like so
115
+
116
+ class User < ActiveRecord::Base
117
+ enumerate :sex, :with => Sex
118
+
119
+ validates_inclusion_of :sex, :in => Sex
120
+ end
121
+
122
+ This works because an enum class responds to #include?().
123
+
124
+
111
125
  === Attribute value lookup
112
126
 
113
127
  Access the enum values and the enum class using the attribute method with a symbol for the enum component you want
@@ -54,7 +54,7 @@ module ActiveEnum
54
54
 
55
55
  # Access id or name value. Pass an id number to retrieve the name or
56
56
  # a symbol or string to retrieve the matching id.
57
- def [](index)
57
+ def get(index)
58
58
  if index.is_a?(Fixnum)
59
59
  row = store.get_by_id(index)
60
60
  row[1] if row
@@ -63,9 +63,10 @@ module ActiveEnum
63
63
  row[0] if row
64
64
  end
65
65
  end
66
+ alias_method :[], :get
66
67
 
67
68
  def include?(value)
68
- !self[value].nil?
69
+ !get(value).nil?
69
70
  end
70
71
 
71
72
  # Access any meta data defined for a given id or name. Returns a hash.
@@ -81,11 +82,11 @@ module ActiveEnum
81
82
  private
82
83
 
83
84
  def id_and_name_and_meta(hash)
84
- if hash.has_key?(:id) || hash.has_key?(:name)
85
+ if hash.has_key?(:name)
85
86
  id = hash.delete(:id) || next_id
86
87
  name = hash.delete(:name)
87
88
  meta = hash
88
- return id, name, (meta.blank? ? nil : meta)
89
+ return id, name, (meta.empty? ? nil : meta)
89
90
  elsif hash.keys.first.is_a?(Fixnum)
90
91
  return *Array(hash).first
91
92
  else
@@ -85,6 +85,8 @@ module ActiveEnum
85
85
 
86
86
  enum = self.class.active_enum_for(:#{attribute})
87
87
  case arg
88
+ when nil
89
+ #{ActiveEnum.use_name_as_value ? 'enum[value]' : 'value' }
88
90
  when :id
89
91
  value if enum[value]
90
92
  when :name
@@ -93,8 +95,6 @@ module ActiveEnum
93
95
  enum
94
96
  when Symbol
95
97
  (enum.meta(value) || {})[arg]
96
- else
97
- #{ActiveEnum.use_name_as_value ? 'enum[value]' : 'value' }
98
98
  end
99
99
  end
100
100
  DEF
@@ -110,10 +110,9 @@ module ActiveEnum
110
110
  class_eval <<-DEF
111
111
  def #{attribute}=(arg)
112
112
  if arg.is_a?(Symbol)
113
- value = self.class.active_enum_for(:#{attribute})[arg]
114
- super(value)
113
+ super self.class.active_enum_for(:#{attribute})[arg]
115
114
  else
116
- super(arg)
115
+ super arg
117
116
  end
118
117
  end
119
118
  DEF
@@ -126,9 +125,9 @@ module ActiveEnum
126
125
  #
127
126
  def define_active_enum_question_method(attribute)
128
127
  class_eval <<-DEF
129
- def #{attribute}?(*args)
130
- if args.first
131
- #{attribute} == self.class.active_enum_for(:#{attribute})[args.first]
128
+ def #{attribute}?(arg=nil)
129
+ if arg
130
+ self.#{attribute} == self.class.active_enum_for(:#{attribute})[arg]
132
131
  else
133
132
  super()
134
133
  end
@@ -5,12 +5,12 @@ module ActiveEnum
5
5
  class I18nStore < MemoryStore
6
6
  def get_by_id(id)
7
7
  row = _values.assoc(id)
8
- [ id, translate(row[1]) ] if row
8
+ [ id, translate(row[1]), row[2] ].compact if row
9
9
  end
10
10
 
11
11
  def get_by_name(name)
12
12
  row = _values.rassoc(name.to_s)
13
- [ row[0], translate(row[1]) ] if row
13
+ [ row[0], translate(row[1]), row[2] ].compact if row
14
14
  end
15
15
 
16
16
  def values
@@ -28,7 +28,7 @@ module ActiveEnum
28
28
  end
29
29
 
30
30
  def translate(key)
31
- I18n.translate key, :scope => i18n_scope
31
+ I18n.translate key, :scope => i18n_scope, :default => key
32
32
  end
33
33
 
34
34
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '0.9.9'
2
+ VERSION = '0.9.11'
3
3
  end
@@ -78,23 +78,34 @@ describe ActiveEnum::Storage::I18nStore do
78
78
  describe "#get_by_id" do
79
79
  before do
80
80
  I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
81
+ I18n.locale = :en
81
82
  end
82
83
 
83
84
  it 'should return the value for a given id' do
84
- I18n.locale = :en
85
-
86
85
  store.set 1, 'test'
87
86
  store.get_by_id(1).should == [1, 'Testing']
88
87
  end
89
88
 
89
+ it 'should return the value with meta for a given id' do
90
+ store.set 1, 'test', :description => 'meta'
91
+ store.get_by_id(1).should == [1, 'Testing', { :description => 'meta' }]
92
+ end
93
+
90
94
  it 'should return nil when id not found' do
91
95
  store.get_by_id(1).should be_nil
92
96
  end
97
+
98
+ it 'should return key when translation missing' do
99
+ I18n.locale = :ja
100
+ store.set 1, 'test'
101
+ store.get_by_id(1).should == [1, 'test']
102
+ end
93
103
  end
94
104
 
95
105
  describe "#get_by_name" do
96
106
  before do
97
107
  I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
108
+ I18n.locale = :en
98
109
  end
99
110
 
100
111
  it 'should return the value for a given name' do
@@ -102,6 +113,11 @@ describe ActiveEnum::Storage::I18nStore do
102
113
  store.get_by_name('test').should == [1, 'Testing']
103
114
  end
104
115
 
116
+ it 'should return the value with meta for a given name' do
117
+ store.set 1, 'test', :description => 'meta'
118
+ store.get_by_name('test').should == [1, 'Testing', { :description => 'meta' }]
119
+ end
120
+
105
121
  it 'should return nil when name not found' do
106
122
  store.get_by_name('test').should be_nil
107
123
  end
@@ -54,8 +54,8 @@ describe ActiveEnum::Storage::MemoryStore do
54
54
 
55
55
  describe "#get_by_id" do
56
56
  it 'should return the value for a given id' do
57
- store.set 1, 'test name'
58
- store.get_by_id(1).should == [1, 'test name']
57
+ store.set 1, 'test name', :description => 'meta'
58
+ store.get_by_id(1).should == [1, 'test name', {:description => "meta"}]
59
59
  end
60
60
 
61
61
  it 'should return nil when id not found' do
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: 41
4
+ hash: 45
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 9
10
- version: 0.9.9
9
+ - 11
10
+ version: 0.9.11
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Meehan
@@ -15,12 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-08 00:00:00 +10:00
18
+ date: 2012-08-18 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: activesupport
23
- prerelease: false
24
22
  requirement: &id001 !ruby/object:Gem::Requirement
25
23
  none: false
26
24
  requirements:
@@ -31,6 +29,8 @@ dependencies:
31
29
  - 3
32
30
  - 0
33
31
  version: "3.0"
32
+ name: activesupport
33
+ prerelease: false
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
36
  description: Define enum classes in Rails and use them to enumerate ActiveRecord attributes