objectreload-simple_enumerations 0.1.2

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,74 @@
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
+ Install the gem from gemcutter:
10
+
11
+ sudo gem install simple_enumerations -s http://gemcutter.org
12
+
13
+ or install as a plugin:
14
+
15
+ script/plugin install git://github.com/wijet/simple_enumerations.git
16
+
17
+ Usage
18
+ =====
19
+
20
+ # config/enumerations.yml
21
+ policy:
22
+ - open
23
+ - closed
24
+ - by approval
25
+
26
+ # app/models/group.rb
27
+ class Group < ActiveRecord::Base
28
+ has_enumerated :policy
29
+ end
30
+
31
+ You can generate sample enumerations file by running
32
+
33
+ script/generate enumerations
34
+
35
+ Plugin generates following methods:
36
+
37
+ @group.policy.open?
38
+ @group.policy.closed?
39
+ @group.policy.by_approval?
40
+
41
+ Fetching enumerations
42
+
43
+ Enumeration[:policy] # => ['open', 'closed', 'by approval']
44
+ Enumeration.all # => {'policy' => ['open', 'closed', 'by approval']}
45
+
46
+ Using enumeration in case statements is very easy
47
+
48
+ case @group.policy
49
+ when :open then ...
50
+ when :closed then ...
51
+ when :by_approval then ...
52
+ end
53
+
54
+ Plugin includes helper for creating select boxes
55
+
56
+ f.select_enumeration :policy
57
+
58
+ REQUIREMENTS
59
+ ============
60
+
61
+ rails >= 2.1
62
+
63
+ LICENSE
64
+ =======
65
+
66
+ (The MIT License)
67
+
68
+ Copyright (c) 2009 Mariusz Pietrzyk (wijet at wijet dot pl)
69
+
70
+ 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:
71
+
72
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
73
+
74
+ 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 = "objectreload-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 = "gems@objectreload.com"
11
+ gem.homepage = "http://github.com/objectreload/simple_enumerations"
12
+ gem.authors = ["Mariusz Pietrzyk", "Mateusz Drozdzynski"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.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
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,5 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 2
5
+ :build:
@@ -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
+ super(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,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{objectreload-simple_enumerations}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mariusz Pietrzyk", "Mateusz Drozdzynski"]
12
+ s.date = %q{2009-12-30}
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{gems@objectreload.com}
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
+ "objectreload-simple_enumerations.gemspec",
33
+ "rails/init.rb",
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/objectreload/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
70
+
@@ -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,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,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: objectreload-simple_enumerations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Mariusz Pietrzyk
8
+ - Mateusz Drozdzynski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-12-30 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ description: Provides simple and clean enumerations system for rails apps. Enumerations are defined in one file and stored in the database, as strings.
27
+ email: gems@objectreload.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.markdown
35
+ files:
36
+ - LICENSE
37
+ - README.markdown
38
+ - Rakefile
39
+ - VERSION.yml
40
+ - generators/enumerations/enumerations_generator.rb
41
+ - generators/enumerations/templates/enumerations.yml
42
+ - init.rb
43
+ - install.rb
44
+ - lib/simple_enumerations.rb
45
+ - lib/simple_enumerations/form_helper.rb
46
+ - lib/simple_enumerations/simple_enumerations.rb
47
+ - lib/simple_enumerations/symbol_extension.rb
48
+ - objectreload-simple_enumerations.gemspec
49
+ - rails/init.rb
50
+ - spec/app_root/app/models/group.rb
51
+ - spec/app_root/app/views/cars/new.html.erb
52
+ - spec/app_root/config/enumerations.yml
53
+ - spec/app_root/db/schema.rb
54
+ - spec/form_helper_spec.rb
55
+ - spec/simple_enumerations_spec.rb
56
+ - spec/spec_helper.rb
57
+ - tasks/simple_enumerations_tasks.rake
58
+ - uninstall.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/objectreload/simple_enumerations
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Simple enumerations system for rails apps
87
+ test_files:
88
+ - spec/app_root/app/models/group.rb
89
+ - spec/app_root/db/schema.rb
90
+ - spec/form_helper_spec.rb
91
+ - spec/simple_enumerations_spec.rb
92
+ - spec/spec_helper.rb