enumerize 0.0.1 → 0.0.2

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/.travis.yml CHANGED
@@ -3,4 +3,6 @@ rvm:
3
3
  - 1.9.2
4
4
  - 1.9.3
5
5
  notifications:
6
- email: false
6
+ email:
7
+ recipients:
8
+ - sergey.nartimov@twinslash.com
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Enumerize
1
+ # Enumerize [![TravisCI](https://secure.travis-ci.org/twinslash/enumerize.png?branch=master)](http://travis-ci.org/twinslash/enumerize)
2
2
 
3
- TODO: Write a gem description
3
+ Enumerated attributes with I18n and ActiveRecord support
4
4
 
5
5
  ## Installation
6
6
 
@@ -40,6 +40,41 @@ class User < ActiveRecord::Base
40
40
  end
41
41
  ```
42
42
 
43
+ I18n:
44
+
45
+ ```ruby
46
+ en:
47
+ enumerize:
48
+ user:
49
+ sex:
50
+ male: "Male"
51
+ female: "Female"
52
+ ```
53
+
54
+ or if you use `sex` attribute across several models you can use this:
55
+
56
+ ```ruby
57
+ en:
58
+ enumerize:
59
+ sex:
60
+ male: "Male"
61
+ female: "Female"
62
+ ```
63
+
64
+ get attribute value:
65
+
66
+ ```ruby
67
+ @user.sex_text # or @user.sex.text
68
+ ```
69
+
70
+ use it with forms:
71
+
72
+ ```erb
73
+ <%= form_for @user do |f| %>
74
+ <%= f.select :sex, User.sex.options %>
75
+ <% end %>
76
+ ```
77
+
43
78
  ## Contributing
44
79
 
45
80
  1. Fork it
@@ -8,6 +8,7 @@ module Enumerize
8
8
  @klass = klass
9
9
  @name = name
10
10
  @values = Array(options[:in]).map { |v| Value.new(self, v) }
11
+ @value_hash = Hash[@values.map { |v| [v.to_s, v] }]
11
12
 
12
13
  if options[:default]
13
14
  @default_value = options[:default] && find_value(options[:default])
@@ -16,9 +17,7 @@ module Enumerize
16
17
  end
17
18
 
18
19
  def find_value(value)
19
- return if value.nil?
20
- value = value.to_s
21
- values.find { |v| v == value }
20
+ @value_hash[value.to_s] unless value.nil?
22
21
  end
23
22
 
24
23
  def i18n_suffix
@@ -26,7 +25,7 @@ module Enumerize
26
25
  end
27
26
 
28
27
  def options
29
- values.map { |v| [v.text, v.to_s] }
28
+ @values.map { |v| [v.text, v.to_s] }
30
29
  end
31
30
  end
32
31
  end
@@ -7,7 +7,7 @@ module Enumerize
7
7
 
8
8
  module ClassMethods
9
9
  def enumerize(*args, &block)
10
- attr = Attribute.new(self, *args)
10
+ attr = Attribute.new(self, *args, &block)
11
11
  singleton_class.class_eval do
12
12
  define_method(attr.name) { attr }
13
13
  end
@@ -3,9 +3,7 @@ require 'i18n'
3
3
  module Enumerize
4
4
  class Value < String
5
5
  def initialize(attr, value)
6
- singleton_class.class_eval do
7
- define_method(:attr) { attr }
8
- end
6
+ @attr = attr
9
7
 
10
8
  super(value.to_s)
11
9
  freeze
@@ -13,8 +11,8 @@ module Enumerize
13
11
 
14
12
  def text
15
13
  i18n_keys = ['']
16
- i18n_keys.unshift "#{attr.i18n_suffix}." if attr.i18n_suffix
17
- i18n_keys.map! { |k| :"enumerize.#{k}#{attr.name}.#{self}" }
14
+ i18n_keys.unshift "#{@attr.i18n_suffix}." if @attr.i18n_suffix
15
+ i18n_keys.map! { |k| :"enumerize.#{k}#{@attr.name}.#{self}" }
18
16
  I18n.t(i18n_keys.shift, :default => i18n_keys)
19
17
  end
20
18
  end
@@ -1,3 +1,3 @@
1
1
  module Enumerize
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ describe Enumerize::Attribute do
4
+ def attr
5
+ @attr ||= nil
6
+ end
7
+
8
+ def build_attr(*args, &block)
9
+ @attr = Enumerize::Attribute.new(*args, &block)
10
+ end
11
+
12
+ it 'returns values' do
13
+ build_attr nil, :foo, :in => [:a, :b]
14
+ attr.values.must_equal %w[a b]
15
+ end
16
+
17
+ it 'returns options for select' do
18
+ I18n.backend.store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}})
19
+ build_attr nil, :foo, :in => %w[a b]
20
+ attr.options.must_equal [['a text', 'a'], ['b text', 'b']]
21
+ end
22
+ end
data/test/basic_test.rb CHANGED
@@ -9,12 +9,12 @@ describe Enumerize::Integrations::Basic do
9
9
 
10
10
  let(:object) { klass.new }
11
11
 
12
- it 'defines method that returns nil' do
12
+ it 'returns nil when not set' do
13
13
  klass.enumerize(:foo, :in => [:a, :b])
14
14
  object.foo.must_equal nil
15
15
  end
16
16
 
17
- it 'defines setter method' do
17
+ it 'returns value that was set' do
18
18
  klass.enumerize(:foo, :in => [:a, :b])
19
19
  object.foo = :a
20
20
  object.foo.must_equal 'a'
@@ -50,12 +50,6 @@ describe Enumerize::Integrations::Basic do
50
50
  object.foo_text.must_equal 'a text scoped'
51
51
  end
52
52
 
53
- it 'returns options for select' do
54
- I18n.backend.store_translations(:en, :enumerize => {:foo => {:a => 'a text', :b => 'b text'}})
55
- klass.enumerize(:foo, :in => [:a, :b])
56
- klass.foo.options.must_equal [['a text', 'a'], ['b text', 'b']]
57
- end
58
-
59
53
  it 'stores value as string' do
60
54
  klass.enumerize(:foo, :in => [:a, :b])
61
55
  object.foo = :a
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-26 00:00:00.000000000 Z
12
+ date: 2012-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &21707780 !ruby/object:Gem::Requirement
16
+ requirement: &24691120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 3.1.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *21707780
24
+ version_requirements: *24691120
25
25
  description: Enumerated attributes with I18n and ActiveRecord support
26
26
  email: info@twinslash.com
27
27
  executables: []
@@ -45,6 +45,7 @@ files:
45
45
  - test/active_record/migrate/1_create_tables.rb
46
46
  - test/active_record/models.rb
47
47
  - test/active_record_test.rb
48
+ - test/attribute_test.rb
48
49
  - test/basic_test.rb
49
50
  - test/test_helper.rb
50
51
  - test/value_test.rb
@@ -62,7 +63,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
63
  version: '0'
63
64
  segments:
64
65
  - 0
65
- hash: -2173772140920833240
66
+ hash: 2302864515113486890
66
67
  required_rubygems_version: !ruby/object:Gem::Requirement
67
68
  none: false
68
69
  requirements:
@@ -71,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
72
  version: '0'
72
73
  segments:
73
74
  - 0
74
- hash: -2173772140920833240
75
+ hash: 2302864515113486890
75
76
  requirements: []
76
77
  rubyforge_project:
77
78
  rubygems_version: 1.8.15
@@ -82,6 +83,7 @@ test_files:
82
83
  - test/active_record/migrate/1_create_tables.rb
83
84
  - test/active_record/models.rb
84
85
  - test/active_record_test.rb
86
+ - test/attribute_test.rb
85
87
  - test/basic_test.rb
86
88
  - test/test_helper.rb
87
89
  - test/value_test.rb