coded_options 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,7 @@ CodedOptions
2
2
  ============
3
3
 
4
4
  Just a way to clean up my normal way to deal with coded options. Basically,
5
- it turns
5
+ in a Rails controller, it turns
6
6
 
7
7
  coded_options :state, %w(initial active closed)
8
8
 
@@ -16,4 +16,16 @@ into
16
16
  STATES[state_id]
17
17
  end
18
18
 
19
+ The STATE_OPTIONS array is perfect for select tags. You can also use this in
20
+ a plain Ruby object as follows:
21
+
22
+ class Foo
23
+ attr_accessor :state_id, :type_id
24
+ coded_options :state => %w(active closed), :type => %w(stupid awesome)
25
+ end
26
+
27
+ foo = Foo.new
28
+ foo.type_id = 1
29
+ foo.type #=> awesome
30
+
19
31
  Enjoy!
@@ -1,27 +1,29 @@
1
1
  module CodedOptions
2
- module Constants
3
- end
4
2
 
5
- def self.extended base
6
- base.send(:include, Constants)
3
+ def coded_options *args
4
+ case args.length
5
+ when 1 then args.first.each {|name, values| setup_coded_options name, values }
6
+ when 2 then setup_coded_options *args
7
+ else raise("Error in coded_options syntax, expecting name and values or a hash, got #{args.inspect}")
8
+ end
7
9
  end
8
10
 
9
- def coded_options singular_name, values
11
+ private
12
+
13
+ def setup_coded_options singular_name, values
10
14
  plural_name = singular_name.to_s.pluralize
11
15
 
12
- Constants.const_set plural_name.upcase, values
13
- Constants.const_set "#{singular_name}_options".upcase, to_options(values)
16
+ const_set plural_name.upcase, values
17
+ const_set "#{singular_name}_options".upcase, to_options(values)
14
18
 
15
19
  class_eval <<-EOT, __FILE__, (__LINE__+1)
16
20
  def #{singular_name} # def state
17
21
  return unless #{singular_name}_id # return unless state_id
18
- Constants::#{plural_name.upcase}[#{singular_name}_id] # STATES[state_id]
22
+ #{plural_name.upcase}[#{singular_name}_id] # STATES[state_id]
19
23
  end # end
20
24
  EOT
21
25
  end
22
26
 
23
- private
24
-
25
27
  def to_options values
26
28
  values.zip((0..values.length).to_a)
27
29
  end
@@ -1,3 +1,3 @@
1
1
  module CodedOptions
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coded_options
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Dew