simple_enumerations 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Mariusz Pietrzyk (wijet at wijet dot pl)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ Simple Enumerations
2
+ ==================
3
+
4
+ Provides simple and clean enumerations system for rails apps.
5
+ Enumerations are defined in one file and stored in the database, as strings.
6
+
7
+ Installation
8
+ ============
9
+
10
+ script/plugin install git://github.com/wijet/simple_enumerations.git
11
+
12
+ Usage
13
+ =====
14
+
15
+ # config/enumerations.yml
16
+ policy:
17
+ - open
18
+ - closed
19
+ - by approval
20
+
21
+ # app/models/group.rb
22
+ class Group < ActiveRecord::Base
23
+ has_enumerated :policy
24
+ end
25
+
26
+ You can generate sample enumerations file by running
27
+
28
+ script/generate enumerations
29
+
30
+ Plugin generates following methods:
31
+
32
+ @group.policy.open?
33
+ @group.policy.closed?
34
+ @group.policy.by_approval?
35
+
36
+ Fetching enumerations
37
+
38
+ Enumeration[:policy] # => ['open', 'closed', 'by approval']
39
+ Enumeration.all # => {'policy' => ['open', 'closed', 'by approval']}
40
+
41
+ Using enumeration in case statements is very easy
42
+
43
+ case @group.policy
44
+ when :open then ...
45
+ when :closed then ...
46
+ when :by_approval then ...
47
+ end
48
+
49
+ Plugin includes helper for creating select boxes
50
+
51
+ f.select_enumeration :policy
52
+
53
+ REQUIREMENTS
54
+ ============
55
+
56
+ rails >= 2.1
57
+
58
+ LICENSE
59
+ =======
60
+
61
+ (The MIT License)
62
+
63
+ Copyright (c) 2009 Mariusz Pietrzyk (wijet at wijet dot pl)
64
+
65
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66
+
67
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
68
+
69
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "simple_enumerations"
8
+ gem.summary = %Q{Simple enumerations system for rails apps}
9
+ gem.description = %Q{Provides simple and clean enumerations system for rails apps. Enumerations are defined in one file and stored in the database, as strings.}
10
+ gem.email = "wijet@wijet.pl"
11
+ gem.homepage = "http://github.com/wijet/simple_enumerations"
12
+ gem.authors = ["Mariusz Pietrzyk"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem.files.include FileList['lib/**/*.rb', 'rails/init.rb']
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
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
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "simple_enumerations #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 1
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,7 @@
1
+ class EnumerationsGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file 'enumerations.yml', "config/enumerations.yml"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # Here define your enumerations
2
+ #enumeration_name:
3
+ # - value1
4
+ # - value2
5
+ # - value3
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/rails/init.rb'
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../rails/init.rb'
@@ -0,0 +1,8 @@
1
+ module Wijet
2
+ module FormHelper
3
+ def select_enumeration(method)
4
+ @template.select_tag "#{@object_name}[#{method}]",
5
+ @template.options_from_collection_for_select(Enumeration[method], :to_s, :to_s , @object.send(method).to_s)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,56 @@
1
+ module Wijet
2
+ module SimpleEnumerations
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def has_enumerated(name)
9
+ name = name.to_s
10
+
11
+ define_method("#{name}=") do |value|
12
+ value = value.to_s
13
+ unless Enumeration[name].include?(value) or value.blank?
14
+ raise ArgumentError, "'#{value}' is not a value of enumeration '#{name}'"
15
+ end
16
+ write_attribute(name, value)
17
+ end
18
+
19
+ define_method(name) do
20
+ EnumerationString::Enumeration.new(attributes[name])
21
+ end
22
+ end
23
+ end
24
+
25
+ module EnumerationString
26
+ class Enumeration < String
27
+ def initialize(value = "")
28
+ super(value)
29
+ end
30
+
31
+ class << self
32
+ def [](name)
33
+ raise ArgumentError, "Enumeration '#{name}' doesn't exist" unless all.has_key?(name.to_s)
34
+ all[name.to_s]
35
+ end
36
+
37
+ def all
38
+ Rails.cache.fetch('_simple_enumerations_') do
39
+ YAML::load(File.read(File.join(RAILS_ROOT, 'config', 'enumerations.yml')))
40
+ end
41
+ end
42
+ end
43
+
44
+ all.values.flatten.uniq.each do |value|
45
+ define_method("#{value.gsub(/[^[:alnum:]]/, '_')}?") { self == value }
46
+ end unless all.blank?
47
+
48
+ def ===(object)
49
+ self == object.to_s
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ include Wijet::SimpleEnumerations::EnumerationString
@@ -0,0 +1,5 @@
1
+ class Symbol
2
+ def ===(object)
3
+ object.kind_of?(Enumeration) ? object === self : super
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ if File.exists?(File.join(RAILS_ROOT, 'config', 'enumerations.yml'))
2
+ require 'simple_enumerations/simple_enumerations'
3
+ require 'simple_enumerations/symbol_extension'
4
+ require 'simple_enumerations/form_helper'
5
+ ActiveRecord::Base.send(:include, Wijet::SimpleEnumerations)
6
+ ActionView::Helpers::FormBuilder.send(:include, Wijet::FormHelper)
7
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{simple_enumerations}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mariusz Pietrzyk"]
12
+ s.date = %q{2009-10-15}
13
+ s.description = %q{Provides simple and clean enumerations system for rails apps. Enumerations are defined in one file and stored in the database, as strings.}
14
+ s.email = %q{wijet@wijet.pl}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.markdown",
22
+ "Rakefile",
23
+ "VERSION.yml",
24
+ "generators/enumerations/enumerations_generator.rb",
25
+ "generators/enumerations/templates/enumerations.yml",
26
+ "init.rb",
27
+ "install.rb",
28
+ "lib/simple_enumerations.rb",
29
+ "lib/simple_enumerations/form_helper.rb",
30
+ "lib/simple_enumerations/simple_enumerations.rb",
31
+ "lib/simple_enumerations/symbol_extension.rb",
32
+ "rails/init.rb",
33
+ "simple_enumerations.gemspec",
34
+ "spec/app_root/app/models/group.rb",
35
+ "spec/app_root/app/views/cars/new.html.erb",
36
+ "spec/app_root/config/enumerations.yml",
37
+ "spec/app_root/db/schema.rb",
38
+ "spec/form_helper_spec.rb",
39
+ "spec/simple_enumerations_spec.rb",
40
+ "spec/spec_helper.rb",
41
+ "tasks/simple_enumerations_tasks.rake",
42
+ "uninstall.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/wijet/simple_enumerations}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.5}
48
+ s.summary = %q{Simple enumerations system for rails apps}
49
+ s.test_files = [
50
+ "spec/app_root/app/models/group.rb",
51
+ "spec/app_root/db/schema.rb",
52
+ "spec/form_helper_spec.rb",
53
+ "spec/simple_enumerations_spec.rb",
54
+ "spec/spec_helper.rb"
55
+ ]
56
+
57
+ if s.respond_to? :specification_version then
58
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
59
+ s.specification_version = 3
60
+
61
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
62
+ s.add_development_dependency(%q<rspec>, [">= 0"])
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 0"])
65
+ end
66
+ else
67
+ s.add_dependency(%q<rspec>, [">= 0"])
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ class Group < ActiveRecord::Base
2
+ has_enumerated :policy
3
+ end
@@ -0,0 +1,3 @@
1
+ <% form_for(@car) do |f| %>
2
+ <%= f.select_enumeration :color %>
3
+ <% end %>
@@ -0,0 +1,11 @@
1
+ policy:
2
+ - open
3
+ - closed
4
+ - secret
5
+ color:
6
+ - red
7
+ - green
8
+ - blue
9
+ - yellow
10
+ - light green
11
+
@@ -0,0 +1,13 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :groups, :force => true do |t|
3
+ t.string :policy
4
+ end
5
+
6
+ create_table :cars, :force => true do |t|
7
+ t.string :color
8
+ end
9
+
10
+ %w(open closed closed secret).each { |policy| Group.create(:policy => policy) }
11
+
12
+ %w(red green).each { |color| execute("INSERT INTO cars(color) VALUES('#{color}')") }
13
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require File.dirname(__FILE__) + '/app_root/app/models/group.rb'
3
+
4
+ describe Wijet::FormHelper do
5
+ include Wijet::FormHelper
6
+ include ActionView::Helpers
7
+
8
+ before do
9
+ stub!(:protect_against_forgery?).and_return(false)
10
+ stub!(:output_buffer).and_return('')
11
+ @group = mock_model(Group, :policy => nil)
12
+ end
13
+
14
+ it "should generate selectbox for enumeration" do
15
+ render_select.should == '<select id="group_policy" name="group[policy]"><option value="open">open</option>
16
+ <option value="closed">closed</option>
17
+ <option value="secret">secret</option></select>'
18
+ end
19
+
20
+ it "should generate selectbox for enumeration with selected value" do
21
+ @group = mock_model(Group, :policy => :open)
22
+ render_select.should == '<select id="group_policy" name="group[policy]"><option value="open" selected="selected">open</option>
23
+ <option value="closed">closed</option>
24
+ <option value="secret">secret</option></select>'
25
+ end
26
+ end
27
+
28
+ def render_select
29
+ returning "" do |select|
30
+ form_for @group, :url => "/groups" do |f|
31
+ select << f.select_enumeration(:policy)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ include Wijet::SimpleEnumerations::EnumerationString
3
+
4
+ describe Enumeration do
5
+ before :all do
6
+ @policies = %w(open closed secret)
7
+ @colors = ['red', 'green', 'blue', 'yellow', 'light green']
8
+ end
9
+
10
+ describe "class methods" do
11
+ it "should respond to :all and return all enumerations" do
12
+ Enumeration.all.should == {'policy' => @policies, 'color' => @colors}
13
+ end
14
+
15
+ it "should respond to :[] and return requested enumeration" do
16
+ Enumeration[:policy].should == @policies
17
+ Enumeration['color'].should == @colors
18
+ end
19
+
20
+ it "should respond to :[] and raise ArgumentError when requested enumeration doesn't exist" do
21
+ lambda {
22
+ Enumeration[:fooooo]
23
+ }.should raise_error(ArgumentError)
24
+ end
25
+
26
+ describe "generating instance methods" do
27
+ it "should generate instance methods, named in format '#\{value\}?'" do
28
+ (@policies + @colors).each do |value|
29
+ Enumeration.new.should respond_to("#{value.gsub(/[^[:alnum:]]/, '_')}?")
30
+ end
31
+ end
32
+
33
+ it "should replace non-alphanumeric characters with underscore" do
34
+ Enumeration.new.should respond_to(:light_green?)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "generated instance methods" do
40
+ it "should return true if enumeration has particular value" do
41
+ enum = Enumeration.new('red')
42
+
43
+ enum.should be_red # calls enum.red?
44
+ enum.should_not be_light_green
45
+ enum.should_not be_open
46
+ end
47
+ end
48
+ end
49
+
50
+ describe Wijet::SimpleEnumerations::ClassMethods do
51
+ describe "has_enumerated" do
52
+ before do
53
+ @group = Group.new(:policy => :open)
54
+ end
55
+
56
+ it "should generate reader methods which return Enumeration object" do
57
+ @group.policy.should == 'open'
58
+ @group.policy.should be_a(Enumeration)
59
+ end
60
+
61
+ it "should generate writer methods which assign enumeration value" do
62
+ @group.policy = :open
63
+ @group.policy.should == Enumeration.new('open')
64
+ end
65
+
66
+ it "should raise ArgumentError when assigning non-enumeration value" do
67
+ lambda {
68
+ @group.policy = :foooooo
69
+ }.should raise_error(ArgumentError)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,22 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+
4
+ rescue LoadError
5
+ puts "You need to install rspec in your base app"
6
+ exit
7
+ end
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.before(:all) do
11
+ path = [File.dirname(__FILE__), 'app_root', 'config', 'enumerations.yml'] * '/'
12
+ File.stub!(:join).and_return(path)
13
+ load File.dirname(__FILE__) + '/../lib/simple_enumerations/simple_enumerations.rb'
14
+ Rails.cache.delete('_simple_enumerations_')
15
+ end
16
+ end
17
+
18
+ require File.dirname(__FILE__) + '/app_root/app/models/group.rb'
19
+ require File.dirname(__FILE__) + '/app_root/db/schema.rb'
20
+
21
+ plugin_spec_dir = File.dirname(__FILE__)
22
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :simple_enumerations do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_enumerations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mariusz Pietrzyk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-15 00:00:00 +02: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: Provides simple and clean enumerations system for rails apps. Enumerations are defined in one file and stored in the database, as strings.
26
+ email: wijet@wijet.pl
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - LICENSE
36
+ - README.markdown
37
+ - Rakefile
38
+ - VERSION.yml
39
+ - generators/enumerations/enumerations_generator.rb
40
+ - generators/enumerations/templates/enumerations.yml
41
+ - init.rb
42
+ - install.rb
43
+ - lib/simple_enumerations.rb
44
+ - lib/simple_enumerations/form_helper.rb
45
+ - lib/simple_enumerations/simple_enumerations.rb
46
+ - lib/simple_enumerations/symbol_extension.rb
47
+ - rails/init.rb
48
+ - simple_enumerations.gemspec
49
+ - spec/app_root/app/models/group.rb
50
+ - spec/app_root/app/views/cars/new.html.erb
51
+ - spec/app_root/config/enumerations.yml
52
+ - spec/app_root/db/schema.rb
53
+ - spec/form_helper_spec.rb
54
+ - spec/simple_enumerations_spec.rb
55
+ - spec/spec_helper.rb
56
+ - tasks/simple_enumerations_tasks.rake
57
+ - uninstall.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/wijet/simple_enumerations
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.5
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Simple enumerations system for rails apps
86
+ test_files:
87
+ - spec/app_root/app/models/group.rb
88
+ - spec/app_root/db/schema.rb
89
+ - spec/form_helper_spec.rb
90
+ - spec/simple_enumerations_spec.rb
91
+ - spec/spec_helper.rb