coded_options 0.0.3 → 0.0.4
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/README.md +13 -1
- data/lib/coded_options/base.rb +12 -10
- data/lib/coded_options/version.rb +1 -1
- metadata +3 -3
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!
|
data/lib/coded_options/base.rb
CHANGED
@@ -1,27 +1,29 @@
|
|
1
1
|
module CodedOptions
|
2
|
-
module Constants
|
3
|
-
end
|
4
2
|
|
5
|
-
def
|
6
|
-
|
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
|
-
|
11
|
+
private
|
12
|
+
|
13
|
+
def setup_coded_options singular_name, values
|
10
14
|
plural_name = singular_name.to_s.pluralize
|
11
15
|
|
12
|
-
|
13
|
-
|
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
|
-
|
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
|
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jason Dew
|