assignable_values 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- assignable_values - Enums on vitamins [![Build Status](https://secure.travis-ci.org/makandra/assignable_values.png?branch=master)](https://travis-ci.org/makandra/assignable_values)
1
+ assignable_values - Enums on vitamins [![Build Status](https://secure.travis-ci.org/makandra/assignable_values.png?branch=master)](https://travis-ci.org/makandra/assignable_values) [![Code Climate](https://codeclimate.com/github/makandra/assignable_values.png)](https://codeclimate.com/github/makandra/assignable_values)
2
2
  =====================================
3
3
 
4
4
  `assignable_values` lets you restrict the values that can be assigned to attributes or associations of ActiveRecord models. You can think of it as enums where the list of allowed values is generated at runtime and the value is checked during validation.
@@ -56,9 +56,10 @@ You can access the humanized version for the current value like this:
56
56
  song = Song.new(:genre => 'pop')
57
57
  song.humanized_genre # => 'Pop music'
58
58
 
59
- Or you can retrieve the humanized version of any given value by passing it as an argument:
59
+ Or you can retrieve the humanized version of any given value by passing it as an argument to either instance or class:
60
60
 
61
61
  song.humanized_genre('rock') # => 'Rock music'
62
+ Song.humanized_genre('rock') # => 'Rock music'
62
63
 
63
64
  You can obtain a list of all assignable values with their humanizations:
64
65
 
@@ -4,4 +4,5 @@ require 'assignable_values/active_record/restriction/base'
4
4
  require 'assignable_values/active_record/restriction/belongs_to_association'
5
5
  require 'assignable_values/active_record/restriction/scalar_attribute'
6
6
  require 'assignable_values/humanized_value'
7
+ require 'assignable_values/humanizable_string'
7
8
 
@@ -127,6 +127,10 @@ module AssignableValues
127
127
  options[:through]
128
128
  end
129
129
 
130
+ def enhance_model_singleton(&block)
131
+ @model.singleton_class.class_eval(&block)
132
+ end
133
+
130
134
  def enhance_model(&block)
131
135
  @model.class_eval(&block)
132
136
  end
@@ -5,13 +5,14 @@ module AssignableValues
5
5
 
6
6
  def initialize(*args)
7
7
  super
8
- define_humanized_value_method
9
- define_humanized_values_method
8
+ define_humanized_value_instance_method
9
+ define_humanized_value_class_method
10
+ define_humanized_values_instance_method
10
11
  end
11
12
 
12
13
  def humanized_value(values, value) # we take the values because that contains the humanizations in case humanizations are hard-coded as a hash
13
14
  if value.present?
14
- if values.respond_to?(:humanizations)
15
+ if values && values.respond_to?(:humanizations)
15
16
  values.humanizations[value]
16
17
  else
17
18
  dictionary_scope = "assignable_values.#{model.name.underscore}.#{property}"
@@ -41,22 +42,21 @@ module AssignableValues
41
42
  if values.is_a?(Hash)
42
43
  { :values => values.keys,
43
44
  :humanizations => values }
44
- #puts "----"
45
- #puts "HAS HUMANIZATIONS"
46
- #humanizations = values
47
- #values = values.keys.dup
48
- #values.singleton_class.send(:define_method, :humanizations) do
49
- # puts "----"
50
- # puts "HUMANIZATIONS used"
51
- # humanizations
52
- #end
53
- #values
54
45
  else
55
46
  super
56
47
  end
57
48
  end
58
49
 
59
- def define_humanized_value_method
50
+ def define_humanized_value_class_method
51
+ restriction = self
52
+ enhance_model_singleton do
53
+ define_method "humanized_#{restriction.property}" do |given_value|
54
+ restriction.humanized_value(nil, given_value)
55
+ end
56
+ end
57
+ end
58
+
59
+ def define_humanized_value_instance_method
60
60
  restriction = self
61
61
  enhance_model do
62
62
  define_method "humanized_#{restriction.property}" do |*args|
@@ -68,7 +68,7 @@ module AssignableValues
68
68
  end
69
69
  end
70
70
 
71
- def define_humanized_values_method
71
+ def define_humanized_values_instance_method
72
72
  restriction = self
73
73
  enhance_model do
74
74
  define_method "humanized_#{restriction.property.to_s.pluralize}" do
@@ -81,10 +81,8 @@ module AssignableValues
81
81
  restriction = self
82
82
  values.collect do |value|
83
83
  if value.is_a?(String)
84
- value = value.dup
85
- value.singleton_class.send(:define_method, :humanized) do
86
- restriction.humanized_value(values, value)
87
- end
84
+ humanization = restriction.humanized_value(values, value)
85
+ value = HumanizableString.new(value, humanization)
88
86
  end
89
87
  value
90
88
  end
@@ -0,0 +1,14 @@
1
+ # A String that responds to #humanized.
2
+ # Earlier versions of assignable_values dependent on such an API.
3
+ class HumanizableString < String
4
+
5
+ def initialize(string, humanization)
6
+ super(string)
7
+ @humanization = humanization
8
+ end
9
+
10
+ def humanized
11
+ @humanization
12
+ end
13
+
14
+ end
@@ -1,3 +1,3 @@
1
1
  module AssignableValues
2
- VERSION = '0.7.2'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.7.1)
4
+ assignable_values (0.7.2)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -18,13 +18,12 @@ GEM
18
18
  activesupport (= 2.3.17)
19
19
  activesupport (2.3.17)
20
20
  columnize (0.3.6)
21
- debugger (1.3.3)
21
+ debugger (1.6.1)
22
22
  columnize (>= 0.3.1)
23
- debugger-linecache (~> 1.1.1)
24
- debugger-ruby_core_source (~> 1.2.0)
25
- debugger-linecache (1.1.2)
26
- debugger-ruby_core_source (>= 1.1.1)
27
- debugger-ruby_core_source (1.2.0)
23
+ debugger-linecache (~> 1.2.0)
24
+ debugger-ruby_core_source (~> 1.2.3)
25
+ debugger-linecache (1.2.0)
26
+ debugger-ruby_core_source (1.2.3)
28
27
  hoe (2.8.0)
29
28
  rake (>= 0.8.7)
30
29
  linecache (0.46)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.7.1)
4
+ assignable_values (0.7.2)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -37,13 +37,12 @@ GEM
37
37
  arel (2.0.10)
38
38
  builder (2.1.2)
39
39
  columnize (0.3.6)
40
- debugger (1.3.3)
40
+ debugger (1.6.1)
41
41
  columnize (>= 0.3.1)
42
- debugger-linecache (~> 1.1.1)
43
- debugger-ruby_core_source (~> 1.2.0)
44
- debugger-linecache (1.1.2)
45
- debugger-ruby_core_source (>= 1.1.1)
46
- debugger-ruby_core_source (1.2.0)
42
+ debugger-linecache (~> 1.2.0)
43
+ debugger-ruby_core_source (~> 1.2.3)
44
+ debugger-linecache (1.2.0)
45
+ debugger-ruby_core_source (1.2.3)
47
46
  diff-lcs (1.2.1)
48
47
  erubis (2.6.6)
49
48
  abstract (>= 1.0.0)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- assignable_values (0.7.1)
4
+ assignable_values (0.7.2)
5
5
  activerecord
6
6
 
7
7
  GEM
@@ -37,13 +37,12 @@ GEM
37
37
  arel (3.0.2)
38
38
  builder (3.0.4)
39
39
  columnize (0.3.6)
40
- debugger (1.3.3)
40
+ debugger (1.6.1)
41
41
  columnize (>= 0.3.1)
42
- debugger-linecache (~> 1.1.1)
43
- debugger-ruby_core_source (~> 1.2.0)
44
- debugger-linecache (1.1.2)
45
- debugger-ruby_core_source (>= 1.1.1)
46
- debugger-ruby_core_source (1.2.0)
42
+ debugger-linecache (~> 1.2.0)
43
+ debugger-ruby_core_source (~> 1.2.3)
44
+ debugger-linecache (1.2.0)
45
+ debugger-ruby_core_source (1.2.3)
47
46
  diff-lcs (1.2.1)
48
47
  erubis (2.7.0)
49
48
  hike (1.2.1)
@@ -62,11 +62,15 @@ describe AssignableValues::ActiveRecord do
62
62
  song.humanized_genre.should be_nil
63
63
  end
64
64
 
65
- it 'should generate a method to retrieve the humanization of any given value' do
65
+ it 'should generate an instance method to retrieve the humanization of any given value' do
66
66
  song = @klass.new(:genre => 'pop')
67
67
  song.humanized_genre('rock').should == 'Rock music'
68
68
  end
69
69
 
70
+ it 'should generate a class method to retrieve the humanization of any given value' do
71
+ @klass.humanized_genre('rock').should == 'Rock music'
72
+ end
73
+
70
74
  end
71
75
 
72
76
  context 'if the :allow_blank option is set to true' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assignable_values
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 2
10
- version: 0.7.2
8
+ - 8
9
+ - 0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Henning Koch
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-07-26 00:00:00 +02:00
18
+ date: 2013-11-27 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -113,6 +113,7 @@ files:
113
113
  - lib/assignable_values/active_record/restriction/belongs_to_association.rb
114
114
  - lib/assignable_values/active_record/restriction/scalar_attribute.rb
115
115
  - lib/assignable_values/errors.rb
116
+ - lib/assignable_values/humanizable_string.rb
116
117
  - lib/assignable_values/humanized_value.rb
117
118
  - lib/assignable_values/version.rb
118
119
  - spec/rails-2.3/Gemfile