active_enum 0.9.10 → 0.9.11

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 CHANGED
@@ -1,3 +1,6 @@
1
+ [0.9.11] : 2012-08-18
2
+ - Use the name key as default value for the name in I18n storage. [kengos]
3
+
1
4
  [0.9.10] : 2012-07-18
2
5
  - Fix I18n storage for meta data. [kengos]
3
6
 
@@ -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
@@ -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.10'
2
+ VERSION = '0.9.11'
3
3
  end
@@ -78,18 +78,15 @@ 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
 
90
89
  it 'should return the value with meta for a given id' do
91
- I18n.locale = :en
92
-
93
90
  store.set 1, 'test', :description => 'meta'
94
91
  store.get_by_id(1).should == [1, 'Testing', { :description => 'meta' }]
95
92
  end
@@ -97,11 +94,18 @@ describe ActiveEnum::Storage::I18nStore do
97
94
  it 'should return nil when id not found' do
98
95
  store.get_by_id(1).should be_nil
99
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
100
103
  end
101
104
 
102
105
  describe "#get_by_name" do
103
106
  before do
104
107
  I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
108
+ I18n.locale = :en
105
109
  end
106
110
 
107
111
  it 'should return the value for a given name' 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: 47
4
+ hash: 45
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 10
10
- version: 0.9.10
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-18 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