coded_options 0.2.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ Gemfile.lock
2
+ .bundle
3
+ .DS_Store
4
+ *.swp
5
+ *.gem
data/Gemfile CHANGED
@@ -1,8 +1,2 @@
1
1
  source :rubygems
2
-
3
- gem "rails", "~>3.0.0"
4
-
5
- group :development do
6
- gem "rspec", "~>2.0.0"
7
- gem "rr"
8
- end
2
+ gemspec
data/README.md CHANGED
@@ -25,7 +25,7 @@ a plain Ruby object as follows:
25
25
  require "coded_options"
26
26
 
27
27
  class Foo
28
- extend CodedOptions
28
+ include CodedOptions
29
29
  attr_accessor :state_id, :type_id
30
30
  coded_options :state => %w(active closed), :type => %w(stupid awesome)
31
31
  end
@@ -45,7 +45,7 @@ numerically by the keys of the hash.
45
45
 
46
46
  If you want to use this with MongoDB, just add
47
47
 
48
- extend CodedOptions
48
+ include CodedOptions
49
49
 
50
50
  to your model.
51
51
 
data/Rakefile CHANGED
@@ -1,18 +1,2 @@
1
1
  require "bundler"
2
- Bundler.setup
3
-
4
- require "rspec"
5
- require "rspec/core/rake_task"
6
-
7
- Rspec::Core::RakeTask.new(:spec)
8
-
9
- gemspec = eval(File.read(File.join(Dir.pwd, "coded_options.gemspec")))
10
-
11
- task :build => "#{gemspec.full_name}.gem"
12
-
13
- task :test => :spec
14
-
15
- file "#{gemspec.full_name}.gem" => gemspec.files + ["coded_options.gemspec"] do
16
- system "gem build coded_options.gemspec"
17
- system "gem install coded_options-#{CodedOptions::VERSION}.gem"
18
- end
2
+ Bundler::GemHelper.install_tasks
@@ -1,4 +1,6 @@
1
- require File.expand_path("../lib/coded_options/version", __FILE__)
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "coded_options/version"
2
4
 
3
5
  Gem::Specification.new do |s|
4
6
  s.name = "coded_options"
@@ -10,23 +12,13 @@ Gem::Specification.new do |s|
10
12
  s.summary = "Making options easier"
11
13
  s.description = "A gem for making fields with coded values easier"
12
14
 
13
- s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "coded_options"
14
16
 
15
- # lol - required for validation
16
- s.rubyforge_project = "coded_options"
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
17
21
 
18
- # If you have other dependencies, add them here
19
- # s.add_dependency "another", "~> 1.2"
20
-
21
- # If you need to check in files that aren't .rb files, add them here
22
- s.files = Dir["{lib}/**/*.rb", "spec/*.rb", "LICENSE", "*.md", "Gemfile", "Rakefile", ".gemtest", "coded_options.gemspec"]
23
- s.require_path = 'lib'
24
-
25
- # If you need an executable, add it here
26
- # s.executables = ["coded_options"]
27
-
28
- # If you have C extensions, uncomment this line
29
- # s.extensions = "ext/extconf.rb"
30
-
31
- s.add_bundler_dependencies
22
+ s.add_development_dependency "rspec", "~>2.0.0"
23
+ s.add_development_dependency "rr"
32
24
  end
@@ -1,38 +1,50 @@
1
1
  module CodedOptions
2
2
 
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
3
+ def self.included base
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module PrivateClassMethods
8
+ attr_accessor :initial_value
9
+ @initial_value = 0
9
10
  end
11
+ extend PrivateClassMethods
12
+
13
+ module ClassMethods
14
+ def coded_options *args
15
+ case args.length
16
+ when 1 then args.first.each {|name, values| setup_coded_options name, values }
17
+ when 2 then setup_coded_options *args
18
+ else raise("Error in coded_options syntax, expecting name and values or a hash, got #{args.inspect}")
19
+ end
20
+ end
10
21
 
11
- private
22
+ private
12
23
 
13
- def setup_coded_options singular_name, values
14
- plural_name = singular_name.to_s.pluralize
24
+ def setup_coded_options singular_name, values
25
+ plural_name = singular_name.to_s.pluralize
15
26
 
16
- const_set plural_name.upcase, values
17
- const_set "#{singular_name}_options".upcase, to_options(values)
27
+ const_set plural_name.upcase, values
28
+ const_set "#{singular_name}_options".upcase, to_options(values)
18
29
 
19
- class_eval <<-EOT, __FILE__, (__LINE__+1)
20
- def #{singular_name} # def state
21
- return unless #{singular_name}_id # return unless state_id
22
- #{plural_name.upcase}[#{singular_name}_id.to_i] # STATES[state_id.to_i]
23
- end # end
30
+ class_eval <<-EOT, __FILE__, (__LINE__+1)
31
+ def #{singular_name} # def state
32
+ return unless #{singular_name}_id # return unless state_id
33
+ #{plural_name.upcase}[#{singular_name}_id.to_i] # STATES[state_id.to_i]
34
+ end # end
24
35
 
25
- def #{singular_name}= new_value # def state= new_value
26
- send :#{singular_name}_id=, #{plural_name.upcase}.index(new_value) # send :state_id=, STATES.index(new_value)
27
- end # end
28
- EOT
29
- end
36
+ def #{singular_name}= new_value # def state= new_value
37
+ send :#{singular_name}_id=, #{plural_name.upcase}.index(new_value) # send :state_id=, STATES.index(new_value)
38
+ end # end
39
+ EOT
40
+ end
30
41
 
31
- def to_options values
32
- if values.is_a? Hash
33
- values.sort{|a,b| a.first <=> b.first}.map{|a| a.reverse}
34
- else
35
- values.zip((0..values.length).to_a)
42
+ def to_options values
43
+ if values.is_a? Hash
44
+ values.sort{|a,b| a.first <=> b.first}.map{|a| a.reverse}
45
+ else
46
+ values.zip((CodedOptions.initial_value..values.length).to_a)
47
+ end
36
48
  end
37
49
  end
38
50
 
@@ -1,3 +1,3 @@
1
1
  module CodedOptions
2
- VERSION = "0.2.7"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  module CodedOptions
2
2
  class Railtie < Rails::Railtie
3
3
  initializer "coded_options.initialize" do |app|
4
- ActiveRecord::Base.send :extend, CodedOptions if defined?(ActiveRecord)
4
+ ActiveRecord::Base.send :include, CodedOptions if defined?(ActiveRecord)
5
5
  end
6
6
  end
7
7
  end
@@ -3,10 +3,26 @@ require "active_support/inflector"
3
3
 
4
4
  describe CodedOptions do
5
5
 
6
+ describe "changing initial value" do
7
+ it "should use the initial value given" do
8
+ CodedOptions.initial_value = 1
9
+ CodedOptions.initial_value.should == 1
10
+
11
+ class Foo
12
+ include CodedOptions
13
+ attr_accessor :state_id
14
+ coded_options :state, %w(initial active closed)
15
+ end
16
+
17
+ Foo::STATE_OPTIONS.should == [["initial", 1], ["active", 2], ["closed", 3]]
18
+ CodedOptions.initial_value = 0
19
+ end
20
+ end
21
+
6
22
  describe "allow setting the *_id field via the value" do
7
23
  it "should perform a lookup and set the proper id" do
8
24
  class Foo
9
- extend CodedOptions
25
+ include CodedOptions
10
26
  attr_accessor :state_id
11
27
  coded_options :state, %w(initial active closed)
12
28
  end
@@ -22,7 +38,7 @@ describe CodedOptions do
22
38
 
23
39
  before(:all) do
24
40
  class Foo
25
- extend CodedOptions
41
+ include CodedOptions
26
42
  coded_options :state, %w(initial active closed)
27
43
  end
28
44
 
@@ -55,7 +71,7 @@ describe CodedOptions do
55
71
  class Bar
56
72
  attr_accessor :state_id, :foo_id
57
73
 
58
- extend CodedOptions
74
+ include CodedOptions
59
75
  coded_options :state => %w(initial active closed),
60
76
  :foo => %w(bar quux baz)
61
77
  end
@@ -76,7 +92,7 @@ describe CodedOptions do
76
92
 
77
93
  before(:all) do
78
94
  class Foo
79
- extend CodedOptions
95
+ include CodedOptions
80
96
  coded_options :gender, {99 => 'other', 1 => 'male', 2 => 'female'}
81
97
  end
82
98
 
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:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 2
9
- - 7
10
- version: 0.2.7
9
+ - 0
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Dew
@@ -15,30 +15,13 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-03 00:00:00 -05:00
18
+ date: 2011-03-01 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- prerelease: false
23
- type: :runtime
24
- name: rails
25
- version_requirements: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ~>
29
- - !ruby/object:Gem::Version
30
- hash: 7
31
- segments:
32
- - 3
33
- - 0
34
- - 0
35
- version: 3.0.0
36
- requirement: *id001
37
- - !ruby/object:Gem::Dependency
38
- prerelease: false
39
- type: :development
40
22
  name: rspec
41
- version_requirements: &id002 !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
42
25
  none: false
43
26
  requirements:
44
27
  - - ~>
@@ -49,12 +32,12 @@ dependencies:
49
32
  - 0
50
33
  - 0
51
34
  version: 2.0.0
52
- requirement: *id002
53
- - !ruby/object:Gem::Dependency
54
- prerelease: false
55
35
  type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
56
38
  name: rr
57
- version_requirements: &id003 !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
58
41
  none: false
59
42
  requirements:
60
43
  - - ">="
@@ -63,7 +46,8 @@ dependencies:
63
46
  segments:
64
47
  - 0
65
48
  version: "0"
66
- requirement: *id003
49
+ type: :development
50
+ version_requirements: *id002
67
51
  description: A gem for making fields with coded values easier
68
52
  email:
69
53
  - jason.dew@gmail.com
@@ -74,18 +58,19 @@ extensions: []
74
58
  extra_rdoc_files: []
75
59
 
76
60
  files:
61
+ - .gemtest
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - coded_options.gemspec
68
+ - lib/coded_options.rb
77
69
  - lib/coded_options/base.rb
78
70
  - lib/coded_options/version.rb
79
- - lib/coded_options.rb
80
71
  - lib/rails/coded_options.rb
81
72
  - spec/coded_options_spec.rb
82
73
  - spec/spec_helper.rb
83
- - LICENSE
84
- - README.md
85
- - Gemfile
86
- - Rakefile
87
- - .gemtest
88
- - coded_options.gemspec
89
74
  has_rdoc: true
90
75
  homepage: http://github.com/jasondew/coded_options
91
76
  licenses: []
@@ -109,18 +94,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
94
  requirements:
110
95
  - - ">="
111
96
  - !ruby/object:Gem::Version
112
- hash: 23
97
+ hash: 3
113
98
  segments:
114
- - 1
115
- - 3
116
- - 6
117
- version: 1.3.6
99
+ - 0
100
+ version: "0"
118
101
  requirements: []
119
102
 
120
103
  rubyforge_project: coded_options
121
- rubygems_version: 1.5.0
104
+ rubygems_version: 1.5.2
122
105
  signing_key:
123
106
  specification_version: 3
124
107
  summary: Making options easier
125
- test_files: []
126
-
108
+ test_files:
109
+ - spec/coded_options_spec.rb
110
+ - spec/spec_helper.rb