coded_options 0.2.6 → 0.2.7

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.
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  # s.add_dependency "another", "~> 1.2"
20
20
 
21
21
  # If you need to check in files that aren't .rb files, add them here
22
- s.files = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md", "Gemfile", "Rakefile", ".gemtest", "coded_options.gemspec"]
22
+ s.files = Dir["{lib}/**/*.rb", "spec/*.rb", "LICENSE", "*.md", "Gemfile", "Rakefile", ".gemtest", "coded_options.gemspec"]
23
23
  s.require_path = 'lib'
24
24
 
25
25
  # If you need an executable, add it here
@@ -1,3 +1,3 @@
1
1
  module CodedOptions
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
@@ -0,0 +1,106 @@
1
+ require "spec_helper"
2
+ require "active_support/inflector"
3
+
4
+ describe CodedOptions do
5
+
6
+ describe "allow setting the *_id field via the value" do
7
+ it "should perform a lookup and set the proper id" do
8
+ class Foo
9
+ extend CodedOptions
10
+ attr_accessor :state_id
11
+ coded_options :state, %w(initial active closed)
12
+ end
13
+
14
+ @foo = Foo.new
15
+ @foo.state = "initial"
16
+ @foo.state_id.should == 0
17
+ @foo.state.should == "initial"
18
+ end
19
+ end
20
+
21
+ describe "given a single field" do
22
+
23
+ before(:all) do
24
+ class Foo
25
+ extend CodedOptions
26
+ coded_options :state, %w(initial active closed)
27
+ end
28
+
29
+ @foo = Foo.new
30
+ end
31
+
32
+ it "should set a constant containing all the values" do
33
+ Foo::STATES.should == %w(initial active closed)
34
+ end
35
+
36
+ it "should set a constant containing the ids and values suitable for consumption by select_tag" do
37
+ Foo::STATE_OPTIONS.should == [["initial", 0], ["active", 1], ["closed", 2]]
38
+ end
39
+
40
+ it "should define a method for accessing the value based on [name]_id" do
41
+ mock(@foo).state_id.twice { 1 }
42
+ @foo.state.should == "active"
43
+ end
44
+
45
+ it "should define a method for accessing the value that returns nil if the id is nil" do
46
+ mock(@foo).state_id { nil }
47
+ @foo.state.should be_nil
48
+ end
49
+
50
+ end
51
+
52
+ describe "given a hash" do
53
+
54
+ before(:all) do
55
+ class Bar
56
+ attr_accessor :state_id, :foo_id
57
+
58
+ extend CodedOptions
59
+ coded_options :state => %w(initial active closed),
60
+ :foo => %w(bar quux baz)
61
+ end
62
+
63
+ @bar = Bar.new
64
+ @bar.state_id = 0
65
+ @bar.foo_id = 1
66
+ end
67
+
68
+ it "should call setup_coded_options for each field" do
69
+ @bar.state.should == "initial"
70
+ @bar.foo.should == "quux"
71
+ end
72
+
73
+ end
74
+
75
+ describe "given a single field with a hash for the values" do
76
+
77
+ before(:all) do
78
+ class Foo
79
+ extend CodedOptions
80
+ coded_options :gender, {99 => 'other', 1 => 'male', 2 => 'female'}
81
+ end
82
+
83
+ @foo = Foo.new
84
+ end
85
+
86
+ it "should set a constant containing all the values" do
87
+ Foo::GENDERS.should == {99 => 'other', 1 => 'male', 2 => 'female'}
88
+ end
89
+
90
+ it "should set a constant containing the ids and values suitable for consumption by select_tag" do
91
+ Foo::GENDER_OPTIONS.should == [["male", 1], ["female", 2], ["other", 99]]
92
+ end
93
+
94
+ it "should define a method for accessing the value based on [name]_id" do
95
+ mock(@foo).gender_id.twice { 1 }
96
+ @foo.gender.should == "male"
97
+ end
98
+
99
+ it "should define a method for accessing the value that returns nil if the id is nil" do
100
+ mock(@foo).gender_id { nil }
101
+ @foo.gender.should be_nil
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,10 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup
4
+
5
+ require "rspec"
6
+ require "coded_options"
7
+
8
+ Rspec.configure do |config|
9
+ config.mock_with :rr
10
+ 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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 6
10
- version: 0.2.6
9
+ - 7
10
+ version: 0.2.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Dew
@@ -78,6 +78,8 @@ files:
78
78
  - lib/coded_options/version.rb
79
79
  - lib/coded_options.rb
80
80
  - lib/rails/coded_options.rb
81
+ - spec/coded_options_spec.rb
82
+ - spec/spec_helper.rb
81
83
  - LICENSE
82
84
  - README.md
83
85
  - Gemfile