coded_options 1.0.2 → 1.1.0

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.
@@ -0,0 +1,3 @@
1
+ 1.1.0
2
+ ==================================
3
+ * changed the internal representation from an array to a hash to support different initial values
data/README.md CHANGED
@@ -11,13 +11,10 @@ in a Rails controller, it turns
11
11
 
12
12
  into
13
13
 
14
- STATES = %w(initial active closed)
14
+ STATES = {0 => initial, 1 => active, 2 => closed)
15
15
  STATE_OPTIONS = [["initial", 0], ["active", 1], ["closed", 2]]
16
16
 
17
- def state
18
- return unless state_id
19
- STATES[state_id]
20
- end
17
+ along with the getter and setter methods #state and #state=(new_state) respectively.
21
18
 
22
19
  The STATE_OPTIONS array is perfect for select tags. You can also use this in
23
20
  a plain Ruby object as follows:
@@ -35,25 +35,31 @@ module CodedOptions
35
35
 
36
36
  def setup_coded_options singular_name, values
37
37
  plural_name = singular_name.to_s.pluralize
38
+ options = to_options(values)
38
39
 
39
- const_set plural_name.upcase, values
40
- const_set "#{singular_name}_options".upcase, to_options(values)
40
+ field = "#{singular_name}_id"
41
+ values_constant = plural_name.upcase
42
+ options_constant = "#{singular_name}_options".upcase
43
+
44
+ const_set options_constant, options
45
+ const_set values_constant, Hash[*options.map(&:reverse).flatten]
41
46
 
42
47
  class_eval <<-EOT, __FILE__, (__LINE__+1)
43
- def #{singular_name} # def state
44
- return unless #{singular_name}_id # return unless state_id
45
- #{plural_name.upcase}[#{singular_name}_id.to_i] # STATES[state_id.to_i]
46
- end # end
48
+ def #{singular_name} # def state
49
+ return unless #{field} # return unless state_id
50
+ #{values_constant}[#{field}.to_i] # STATES[state_id.to_i]
51
+ end # end
47
52
 
48
- def #{singular_name}= new_value # def state= new_value
49
- send :#{singular_name}_id=, #{plural_name.upcase}.index(new_value) # send :state_id=, STATES.index(new_value)
50
- end # end
53
+ def #{singular_name}= new_value # def state= new_value
54
+ _, id = #{options_constant}.detect {|value, id| value == new_value } # _, id = STATE_OPTIONS.detect {|value, id| value == new_value }
55
+ send :#{field}=, id # send :state_id=, id
56
+ end # end
51
57
  EOT
52
58
  end
53
59
 
54
60
  def to_options values
55
61
  if values.is_a? Hash
56
- values.sort{|a,b| a.first <=> b.first}.map{|a| a.reverse}
62
+ values.sort{|a,b| a.first <=> b.first }.map{|a| a.reverse }
57
63
  else
58
64
  ids = (CodedOptions.initial_value..(CodedOptions.initial_value + values.length)).to_a
59
65
  values.zip(ids)
@@ -1,3 +1,3 @@
1
1
  module CodedOptions
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -39,6 +39,15 @@ describe CodedOptions do
39
39
  end
40
40
 
41
41
  Foo::STATE_OPTIONS.should == [["initial", 1], ["active", 2], ["closed", 3]]
42
+
43
+ @foo = Foo.new
44
+ @foo.state = "active"
45
+ @foo.state_id.should == 2
46
+
47
+ @foo = Foo.new
48
+ @foo.state_id = 3
49
+ @foo.state.should == "closed"
50
+
42
51
  CodedOptions.initial_value = 0
43
52
  end
44
53
 
@@ -53,6 +62,14 @@ describe CodedOptions do
53
62
 
54
63
  Foo::STATE_OPTIONS.should == [["initial", 42], ["active", 43], ["closed", 44]]
55
64
  Foo::BAR_OPTIONS.should == [["quux", 0], ["rand", 1]]
65
+
66
+ @foo = Foo.new
67
+ @foo.state = "active"
68
+ @foo.state_id.should == 43
69
+
70
+ @foo = Foo.new
71
+ @foo.state_id = 44
72
+ @foo.state.should == "closed"
56
73
  end
57
74
  end
58
75
 
@@ -83,7 +100,7 @@ describe CodedOptions do
83
100
  end
84
101
 
85
102
  it "should set a constant containing all the values" do
86
- Foo::STATES.should == %w(initial active closed)
103
+ Foo::STATES.values.should == %w(initial active closed)
87
104
  end
88
105
 
89
106
  it "should set a constant containing the ids and values suitable for consumption by select_tag" do
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 1.0.2
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Dew
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-01 00:00:00 -05:00
18
+ date: 2011-03-07 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -88,6 +88,7 @@ extra_rdoc_files: []
88
88
  files:
89
89
  - .gemtest
90
90
  - .gitignore
91
+ - CHANGELOG
91
92
  - Gemfile
92
93
  - LICENSE
93
94
  - README.md