enum_simulator 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +15 -0
- data/README.rdoc +29 -0
- data/Rakefile +33 -0
- data/enum_simulator.gemspec +29 -0
- data/init.rb +3 -0
- data/install.rb +1 -0
- data/lib/enum_simulator.rb +34 -0
- data/spec/enum_simulator_spec.rb +73 -0
- data/spec/resources/schema +11 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/support/models/other_thingies.rb +2 -0
- data/spec/support/models/thingy.rb +4 -0
- data/uninstall.rb +1 -0
- metadata +70 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 [name of plugin creator]
|
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/Manifest
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gemfile
|
2
|
+
MIT-LICENSE
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
enum_simulator.gemspec
|
6
|
+
init.rb
|
7
|
+
install.rb
|
8
|
+
lib/enum_simulator.rb
|
9
|
+
spec/enum_simulator_spec.rb
|
10
|
+
spec/resources/schema
|
11
|
+
spec/spec_helper.rb
|
12
|
+
spec/support/models/other_thingies.rb
|
13
|
+
spec/support/models/thingy.rb
|
14
|
+
uninstall.rb
|
15
|
+
Manifest
|
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
= EnumSimulator
|
2
|
+
|
3
|
+
A simple plugin for abstracting out standard ActiveRecord enumerated attributes
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Let bundler do all the work for you by adding this to your Gemfile:
|
8
|
+
|
9
|
+
gem 'enum_simulator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
bundle install
|
14
|
+
|
15
|
+
Or install it manually:
|
16
|
+
|
17
|
+
sudo gem install enum_simulator
|
18
|
+
|
19
|
+
== Example
|
20
|
+
|
21
|
+
In order to mark an AR attribute as enumerated, make sure the column in question is a string and large enough to accommodate the string representation of your largest possible value. Then, in the model file, just call "enum", passing it the symbol of the attribute and an array of symbols you'd like to describe as the possible values:
|
22
|
+
|
23
|
+
class IceCream < ActiveRecord::Base
|
24
|
+
enum :flavor, [:chocolate, :vanilla, :strawberry, :rockyroad]
|
25
|
+
end
|
26
|
+
|
27
|
+
enum_simulator will handle validation of inclusion and conversion of the attribute values from symbols to strings and back internally. Honestly, that's really all it does - it's just such a common pattern that I figured we might as well have a more succinct way of using it. It will infer whether or not to allow assignment of nil to your attribute from the column definition.
|
28
|
+
|
29
|
+
Copyright (c) 2012 Centresource, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
require 'echoe'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the enum_simulator plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
desc 'Generate documentation for the enum_simulator plugin.'
|
18
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
19
|
+
rdoc.rdoc_dir = 'rdoc'
|
20
|
+
rdoc.title = 'EnumSimulator'
|
21
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
22
|
+
rdoc.rdoc_files.include('README')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
25
|
+
|
26
|
+
Echoe.new('enum_simulator', '1.0.4') do |p|
|
27
|
+
p.description = 'A simple plugin for abstracting out standard ActiveRecord enumerated attributes.'
|
28
|
+
p.url = 'https://github.com/centresource/enum_simulator.git'
|
29
|
+
p.author = ['Jeremy Holland', 'Brandon Valentine']
|
30
|
+
p.email = ['jeremy@jeremypholland.com', 'brandon@brandonvalentine.com']
|
31
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
32
|
+
p.development_dependencies = []
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "enum_simulator"
|
5
|
+
s.version = "1.0.4"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Jeremy Holland, Brandon Valentine"]
|
9
|
+
s.date = "2012-09-15"
|
10
|
+
s.description = "A simple plugin for abstracting out standard ActiveRecord enumerated attributes."
|
11
|
+
s.email = ["jeremy@jeremypholland.com", "brandon@brandonvalentine.com"]
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/enum_simulator.rb"]
|
13
|
+
s.files = ["Gemfile", "MIT-LICENSE", "README.rdoc", "Rakefile", "enum_simulator.gemspec", "init.rb", "install.rb", "lib/enum_simulator.rb", "spec/enum_simulator_spec.rb", "spec/resources/schema", "spec/spec_helper.rb", "spec/support/models/other_thingies.rb", "spec/support/models/thingy.rb", "uninstall.rb", "Manifest"]
|
14
|
+
s.homepage = "https://github.com/centresource/enum_simulator.git"
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Enum_simulator", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = "enum_simulator"
|
18
|
+
s.rubygems_version = "1.8.24"
|
19
|
+
s.summary = "A simple plugin for abstracting out standard ActiveRecord enumerated attributes."
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
s.specification_version = 3
|
23
|
+
|
24
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
25
|
+
else
|
26
|
+
end
|
27
|
+
else
|
28
|
+
end
|
29
|
+
end
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# EnumSimulator
|
2
|
+
module EnumSimulator
|
3
|
+
def self.included(base)
|
4
|
+
base.send :extend, ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def enum(attr, values)
|
9
|
+
values << nil if self.columns_hash[attr.to_s].respond_to? :null and self.columns_hash[attr.to_s].null
|
10
|
+
@enumerated_attributes ||= {}
|
11
|
+
@enumerated_attributes[attr] = values
|
12
|
+
validates_inclusion_of attr, :in => values
|
13
|
+
|
14
|
+
class_eval <<RUBY
|
15
|
+
def #{attr}
|
16
|
+
read_attribute(:#{attr}).to_sym if read_attribute(:#{attr})
|
17
|
+
end
|
18
|
+
|
19
|
+
def #{attr}=(value)
|
20
|
+
value = value.to_s unless value.nil?
|
21
|
+
write_attribute(:#{attr},value)
|
22
|
+
end
|
23
|
+
RUBY
|
24
|
+
end
|
25
|
+
|
26
|
+
def enumerated_attributes
|
27
|
+
@enumerated_attributes
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveSupport.on_load(:active_record) do
|
33
|
+
include EnumSimulator
|
34
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EnumSimulator do
|
4
|
+
describe "enum" do
|
5
|
+
it "should define an enum method on all classes" do
|
6
|
+
Thingy.should respond_to(:enum)
|
7
|
+
OtherThingy.should respond_to(:enum)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should cast incoming assignments of symbols to strings" do
|
11
|
+
x = Thingy.new
|
12
|
+
x.flavor = :cork
|
13
|
+
x.send(:read_attribute,:flavor).should == 'cork'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should cast internal representation of strings to symbols" do
|
17
|
+
x = Thingy.new
|
18
|
+
x.send(:write_attribute,:flavor,'basket')
|
19
|
+
x.flavor.should == :basket
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return nil if the value is nil" do
|
23
|
+
x = Thingy.new
|
24
|
+
x.flavor.should == nil
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should require the symbolized value of the attribute specified in the first argument to be a member of the array passed as the second argument" do
|
28
|
+
x = Thingy.new
|
29
|
+
x.should { validate_inclusion_of :flavor, :in => [:sweet, :sour, :salty, :bitter, :umami] }
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should require a value for the attribute if the column definition does not allow null" do
|
33
|
+
lambda { x = Thingy.create! }.should raise_error ActiveRecord::RecordInvalid
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should allow the setting of an attribute to nil if the column definition allows null" do
|
37
|
+
lambda { x = Thingy.create! :flavor => :sweet }.should_not raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "enumerated_attributes" do
|
42
|
+
it "should define an enumerated_attributes method on all classes" do
|
43
|
+
Thingy.should respond_to(:enumerated_attributes)
|
44
|
+
OtherThingy.should respond_to(:enumerated_attributes)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be nil for a class without any enumerated attributes" do
|
48
|
+
OtherThingy.enumerated_attributes.should be_nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return a hash, keyed with the names of all enumerated attributes" do
|
52
|
+
Thingy.enumerated_attributes.should be_a(Hash)
|
53
|
+
Thingy.enumerated_attributes.should have_key(:flavor)
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "each item" do
|
57
|
+
it "should be an array of all possible enumerable values for the key in question" do
|
58
|
+
Thingy.enumerated_attributes[:flavor].should include(:sweet)
|
59
|
+
Thingy.enumerated_attributes[:flavor].should include(:salty)
|
60
|
+
Thingy.enumerated_attributes[:flavor].should include(:sour)
|
61
|
+
Thingy.enumerated_attributes[:flavor].should include(:bitter)
|
62
|
+
Thingy.enumerated_attributes[:flavor].should include(:umami)
|
63
|
+
Thingy.enumerated_attributes[:flavor].should_not include(:magic)
|
64
|
+
Thingy.enumerated_attributes[:flavor].should_not include(:corned_beef)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should conditionally include nil in array of possible enumerable values when column allows null" do
|
68
|
+
Thingy.enumerated_attributes[:flavor].should_not include(nil)
|
69
|
+
Thingy.enumerated_attributes[:smell].should include(nil)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :thingies, :force => true do |t|
|
3
|
+
t.string :flavor, :null => false
|
4
|
+
t.string :smell
|
5
|
+
t.timestamps
|
6
|
+
end
|
7
|
+
create_table :other_thingies, :force => true do |t|
|
8
|
+
t.string :taste
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
def prep_db
|
4
|
+
dir = File.dirname(__FILE__)
|
5
|
+
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ":memory:"}}
|
6
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
7
|
+
|
8
|
+
ActiveRecord::Migration.verbose = false
|
9
|
+
|
10
|
+
load "#{dir}/resources/schema"
|
11
|
+
end
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
|
15
|
+
require 'rspec'
|
16
|
+
require 'active_record'
|
17
|
+
require 'enum_simulator'
|
18
|
+
|
19
|
+
prep_db
|
20
|
+
|
21
|
+
require File.dirname(__FILE__)+'/../init.rb'
|
22
|
+
|
23
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_simulator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Holland, Brandon Valentine
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-15 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple plugin for abstracting out standard ActiveRecord enumerated
|
15
|
+
attributes.
|
16
|
+
email:
|
17
|
+
- jeremy@jeremypholland.com
|
18
|
+
- brandon@brandonvalentine.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files:
|
22
|
+
- README.rdoc
|
23
|
+
- lib/enum_simulator.rb
|
24
|
+
files:
|
25
|
+
- Gemfile
|
26
|
+
- MIT-LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- enum_simulator.gemspec
|
30
|
+
- init.rb
|
31
|
+
- install.rb
|
32
|
+
- lib/enum_simulator.rb
|
33
|
+
- spec/enum_simulator_spec.rb
|
34
|
+
- spec/resources/schema
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
- spec/support/models/other_thingies.rb
|
37
|
+
- spec/support/models/thingy.rb
|
38
|
+
- uninstall.rb
|
39
|
+
- Manifest
|
40
|
+
homepage: https://github.com/centresource/enum_simulator.git
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --line-numbers
|
45
|
+
- --inline-source
|
46
|
+
- --title
|
47
|
+
- Enum_simulator
|
48
|
+
- --main
|
49
|
+
- README.rdoc
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '1.2'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project: enum_simulator
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: A simple plugin for abstracting out standard ActiveRecord enumerated attributes.
|
70
|
+
test_files: []
|