enumlogic 1.0.0
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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +35 -0
- data/VERSION.yml +4 -0
- data/lib/enumlogic.rb +61 -0
- data/spec/enumlogic_spec.rb +72 -0
- data/spec/spec_helper.rb +27 -0
- metadata +74 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 binarylogic
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= enumlogic
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 binarylogic. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "enumlogic"
|
8
|
+
gem.summary = "Adds enumerations to your models"
|
9
|
+
gem.description = "Adds enumerations to your models"
|
10
|
+
gem.email = "bjohnson@binarylogic.com"
|
11
|
+
gem.homepage = "http://github.com/binarylogic/enumlogic"
|
12
|
+
gem.authors = ["binarylogic"]
|
13
|
+
gem.rubyforge_project = "enumlogic"
|
14
|
+
gem.add_development_dependency "rspec"
|
15
|
+
end
|
16
|
+
Jeweler::RubyforgeTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
data/VERSION.yml
ADDED
data/lib/enumlogic.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require "activerecord"
|
2
|
+
|
3
|
+
# See the enum class level method for more info.
|
4
|
+
module Enumlogic
|
5
|
+
# Allows you to easily specify enumerations for your models. Specify enumerations like:
|
6
|
+
#
|
7
|
+
# class Computer < ActiveRecord::Base
|
8
|
+
# enum :kind, ["apple", "dell", "hp"]
|
9
|
+
# enum :kind, {"apple" => "Apple", "dell" => "Dell", "hp" => "HP"}
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# You can now do the following:
|
13
|
+
#
|
14
|
+
# Computer::KINDS # passes back exactly what you specified, Array or Hash
|
15
|
+
# Computer.kind_options # gives you a friendly hash that you can easily pass into the select helper for forms
|
16
|
+
# Computer.new(:kind => "unknown").valid? # false, automatically validates inclusion of the enum field
|
17
|
+
#
|
18
|
+
# c = Computer.new(:kind => "apple")
|
19
|
+
# c.apple? # true
|
20
|
+
# c.apple_key # :apple
|
21
|
+
# c.apple_text # "apple" or "Apple" if you gave a hash with a user friendly text value
|
22
|
+
def enum(field, values, options = {})
|
23
|
+
values_hash = if values.is_a?(Array)
|
24
|
+
hash = {}
|
25
|
+
values.each { |value| hash[value] = value }
|
26
|
+
hash
|
27
|
+
else
|
28
|
+
values
|
29
|
+
end
|
30
|
+
|
31
|
+
values_array = values.is_a?(Hash) ? values.keys : values
|
32
|
+
|
33
|
+
message = options[:message] || "#{field} is not included in the list"
|
34
|
+
constant_name = options[:constant] || field.to_s.pluralize.upcase
|
35
|
+
const_set constant_name, values_array unless const_defined?(constant_name)
|
36
|
+
|
37
|
+
new_hash = {}
|
38
|
+
values_hash.each { |key, text| new_hash[text] = key }
|
39
|
+
(class << self; self; end).send(:define_method, "#{field}_options") { new_hash }
|
40
|
+
|
41
|
+
define_method("#{field}_key") do
|
42
|
+
send(field).to_s.gsub(/[-\s]/, '_').downcase.to_sym
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method("#{field}_text") do
|
46
|
+
values_hash.find { |key, text| key == send(field) }.last
|
47
|
+
end
|
48
|
+
|
49
|
+
values_array.each do |value|
|
50
|
+
method_name = value.downcase.gsub(/[-\s]/, '_')
|
51
|
+
method_name = "#{method_name}_#{field}" if options[:namespace]
|
52
|
+
define_method("#{method_name}?") do
|
53
|
+
self.send(field) == value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
validates_inclusion_of field, :in => values_array, :message => message
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
ActiveRecord::Base.extend Enumlogic
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Enumlogic" do
|
4
|
+
it "should have a constant" do
|
5
|
+
Computer.enum :kind, ["apple", "dell", "hp"], :namespace => true
|
6
|
+
Computer::KINDS.should == ["apple", "dell", "hp"]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "constant should always return an array" do
|
10
|
+
hash = ActiveSupport::OrderedHash.new
|
11
|
+
hash["apple"] = "Apple"
|
12
|
+
hash["dell"] = "Dell"
|
13
|
+
hash["hp"] = "HP"
|
14
|
+
|
15
|
+
Computer.enum :kind, hash, :namespace => true
|
16
|
+
Computer::KINDS.should == ["apple", "dell", "hp"]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should create a class level options method" do
|
20
|
+
Computer.enum :kind, ["apple", "dell", "hp"]
|
21
|
+
Computer.kind_options.should == {"apple" => "apple", "dell" => "dell", "hp" => "hp"}
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should create a class level options method for hashes" do
|
25
|
+
Computer.enum :kind, {"apple" => "Apple", "dell" => "Dell", "hp" => "HP"}
|
26
|
+
Computer.kind_options.should == {"Apple" => "apple", "Dell" => "dell", "HP" => "hp"}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should create key methods" do
|
30
|
+
Computer.enum :kind, ["apple", "dell", "hp"]
|
31
|
+
c = Computer.new(:kind => "apple")
|
32
|
+
c.kind_key.should == :apple
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should create key methods for hashes" do
|
36
|
+
Computer.enum :kind, {"apple" => "Apple", "dell" => "Dell", "hp" => "HP"}
|
37
|
+
c = Computer.new(:kind => "apple")
|
38
|
+
c.kind_key.should == :apple
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should create text methods" do
|
42
|
+
Computer.enum :kind, {"apple" => "Apple", "dell" => "Dell", "hp" => "HP"}
|
43
|
+
c = Computer.new(:kind => "hp")
|
44
|
+
c.kind_text.should == "HP"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should create text methods for hashes" do
|
48
|
+
Computer.enum :kind, {"apple" => "Apple", "dell" => "Dell", "hp" => "HP"}
|
49
|
+
c = Computer.new(:kind => "hp")
|
50
|
+
c.kind_text.should == "HP"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should create boolean methods" do
|
54
|
+
Computer.enum :kind, ["apple", "dell", "hp"]
|
55
|
+
c = Computer.new(:kind => "apple")
|
56
|
+
c.should be_apple
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should namespace boolean methods" do
|
60
|
+
Computer.enum :kind, ["apple", "dell", "hp"], :namespace => true
|
61
|
+
c = Computer.new(:kind => "apple")
|
62
|
+
c.should be_apple_kind
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should validate inclusion" do
|
66
|
+
Computer.enum :kind, ["apple", "dell", "hp"]
|
67
|
+
c = Computer.new
|
68
|
+
c.kind = "blah"
|
69
|
+
c.should_not be_valid
|
70
|
+
c.errors[:kind].should include("kind is not included in the list")
|
71
|
+
end
|
72
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'spec'
|
4
|
+
require 'spec/autorun'
|
5
|
+
require 'rubygems'
|
6
|
+
require 'enumlogic'
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
9
|
+
ActiveRecord::Base.configurations = true
|
10
|
+
|
11
|
+
ActiveRecord::Schema.verbose = false
|
12
|
+
ActiveRecord::Schema.define(:version => 1) do
|
13
|
+
create_table :computers do |t|
|
14
|
+
t.string :kind
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Spec::Runner.configure do |config|
|
19
|
+
config.before(:each) do
|
20
|
+
class Computer < ActiveRecord::Base
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
config.after(:each) do
|
25
|
+
Object.send(:remove_const, :Computer)
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumlogic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- binarylogic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-01 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Adds enumerations to your models
|
26
|
+
email: bjohnson@binarylogic.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION.yml
|
41
|
+
- lib/enumlogic.rb
|
42
|
+
- spec/enumlogic_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/binarylogic/enumlogic
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: enumlogic
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Adds enumerations to your models
|
72
|
+
test_files:
|
73
|
+
- spec/enumlogic_spec.rb
|
74
|
+
- spec/spec_helper.rb
|