coded_options 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/coded_options.gemspec +3 -0
- data/lib/coded_options/base.rb +14 -2
- data/lib/coded_options/version.rb +1 -1
- data/spec/coded_options_spec.rb +14 -1
- metadata +35 -7
data/coded_options.gemspec
CHANGED
@@ -19,6 +19,9 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
+
s.add_dependency "activesupport"
|
23
|
+
s.add_dependency "i18n"
|
24
|
+
|
22
25
|
s.add_development_dependency "rspec", "~>2.0.0"
|
23
26
|
s.add_development_dependency "rr"
|
24
27
|
end
|
data/lib/coded_options/base.rb
CHANGED
@@ -13,7 +13,18 @@ module CodedOptions
|
|
13
13
|
module ClassMethods
|
14
14
|
def coded_options *args
|
15
15
|
case args.length
|
16
|
-
when 1 then
|
16
|
+
when 1 then
|
17
|
+
hash = args.first
|
18
|
+
if (new_initial_value = hash.delete(:initial_value))
|
19
|
+
old_initial_value = CodedOptions.initial_value
|
20
|
+
CodedOptions.initial_value = new_initial_value
|
21
|
+
|
22
|
+
hash.each {|name, values| setup_coded_options name, values }
|
23
|
+
|
24
|
+
CodedOptions.initial_value = old_initial_value
|
25
|
+
else
|
26
|
+
hash.each {|name, values| setup_coded_options name, values }
|
27
|
+
end
|
17
28
|
when 2 then setup_coded_options *args
|
18
29
|
else raise("Error in coded_options syntax, expecting name and values or a hash, got #{args.inspect}")
|
19
30
|
end
|
@@ -43,7 +54,8 @@ module CodedOptions
|
|
43
54
|
if values.is_a? Hash
|
44
55
|
values.sort{|a,b| a.first <=> b.first}.map{|a| a.reverse}
|
45
56
|
else
|
46
|
-
|
57
|
+
ids = (CodedOptions.initial_value..(CodedOptions.initial_value + values.length)).to_a
|
58
|
+
values.zip(ids)
|
47
59
|
end
|
48
60
|
end
|
49
61
|
end
|
data/spec/coded_options_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require "active_support/inflector"
|
|
4
4
|
describe CodedOptions do
|
5
5
|
|
6
6
|
describe "changing initial value" do
|
7
|
-
it "should use the initial value given" do
|
7
|
+
it "should use the initial value given when set manually" do
|
8
8
|
CodedOptions.initial_value = 1
|
9
9
|
CodedOptions.initial_value.should == 1
|
10
10
|
|
@@ -17,6 +17,19 @@ describe CodedOptions do
|
|
17
17
|
Foo::STATE_OPTIONS.should == [["initial", 1], ["active", 2], ["closed", 3]]
|
18
18
|
CodedOptions.initial_value = 0
|
19
19
|
end
|
20
|
+
|
21
|
+
it "should use the initial value given when set via the hash" do
|
22
|
+
class Foo
|
23
|
+
include CodedOptions
|
24
|
+
attr_accessor :state_id
|
25
|
+
coded_options :state => %w(initial active closed),
|
26
|
+
:initial_value => 42
|
27
|
+
coded_options :bar => %w(quux rand)
|
28
|
+
end
|
29
|
+
|
30
|
+
Foo::STATE_OPTIONS.should == [["initial", 42], ["active", 43], ["closed", 44]]
|
31
|
+
Foo::BAR_OPTIONS.should == [["quux", 0], ["rand", 1]]
|
32
|
+
end
|
20
33
|
end
|
21
34
|
|
22
35
|
describe "allow setting the *_id field via the value" do
|
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: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jason Dew
|
@@ -19,9 +19,37 @@ date: 2011-03-01 00:00:00 -05:00
|
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: activesupport
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: i18n
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
25
53
|
none: false
|
26
54
|
requirements:
|
27
55
|
- - ~>
|
@@ -33,11 +61,11 @@ dependencies:
|
|
33
61
|
- 0
|
34
62
|
version: 2.0.0
|
35
63
|
type: :development
|
36
|
-
version_requirements: *
|
64
|
+
version_requirements: *id003
|
37
65
|
- !ruby/object:Gem::Dependency
|
38
66
|
name: rr
|
39
67
|
prerelease: false
|
40
|
-
requirement: &
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
41
69
|
none: false
|
42
70
|
requirements:
|
43
71
|
- - ">="
|
@@ -47,7 +75,7 @@ dependencies:
|
|
47
75
|
- 0
|
48
76
|
version: "0"
|
49
77
|
type: :development
|
50
|
-
version_requirements: *
|
78
|
+
version_requirements: *id004
|
51
79
|
description: A gem for making fields with coded values easier
|
52
80
|
email:
|
53
81
|
- jason.dew@gmail.com
|