classy_enum 0.0.1

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Peter Brown
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.
@@ -0,0 +1,7 @@
1
+ = classy_enum
2
+
3
+ More Description love in the works
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2010 Peter Brown. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "classy_enum"
8
+ gem.summary = %Q{A class based enumerator utility for Ruby on Rails}
9
+ gem.description = %Q{A utility that adds class based enum functionaltiy to ActiveRecord attributes}
10
+ gem.email = "github@lette.us"
11
+ gem.homepage = "http://github.com/beerlington/classy_enum"
12
+ gem.authors = ["Peter Brown"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "activerecord", "~> 2.3"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: 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
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "classy_enum #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,59 @@
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{classy_enum}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Peter Brown"]
12
+ s.date = %q{2010-09-21}
13
+ s.description = %q{A utility that adds class based enum functionaltiy to ActiveRecord attributes}
14
+ s.email = %q{github@lette.us}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "classy_enum.gemspec",
27
+ "init.rb",
28
+ "lib/classy_enum.rb",
29
+ "spec/classy_enum_spec.rb",
30
+ "spec/spec.opts",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/beerlington/classy_enum}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{A class based enumerator utility for Ruby on Rails}
38
+ s.test_files = [
39
+ "spec/classy_enum_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
49
+ s.add_runtime_dependency(%q<activerecord>, ["~> 2.3"])
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
+ s.add_dependency(%q<activerecord>, ["~> 2.3"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ s.add_dependency(%q<activerecord>, ["~> 2.3"])
57
+ end
58
+ end
59
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Adds active_set_helper method to all active record classes
2
+ require 'classy_enum'
@@ -0,0 +1,105 @@
1
+ class ClassyEnumValue < Object
2
+
3
+ attr_reader :to_s, :index
4
+
5
+ def initialize(option, index)
6
+ @to_s = option.to_s.downcase
7
+ @index = index + 1
8
+ end
9
+
10
+ def name
11
+ to_s.titleize
12
+ end
13
+
14
+ def <=> other
15
+ @index <=> other.index
16
+ end
17
+
18
+ end
19
+
20
+ module ClassyEnum
21
+
22
+ module ClassMethods
23
+
24
+ def new(option)
25
+ self::OPTION_HASH[option] || TypeError.new("Valid #{self} options are #{self.valid_options}")
26
+ end
27
+
28
+ def all
29
+ self::OPTIONS.map {|e| self.new(e) }
30
+ end
31
+
32
+ # Uses the name field for select options
33
+ def all_with_name
34
+ self.all.map {|e| [e.name, e.to_s] }
35
+ end
36
+
37
+ def valid_options
38
+ self::OPTIONS.map(&:to_s).join(', ')
39
+ end
40
+
41
+ # Alias of new
42
+ def find(option)
43
+ new(option)
44
+ end
45
+
46
+ end
47
+
48
+ def self.included(other)
49
+ other.extend ClassMethods
50
+
51
+ other.const_set("OPTION_HASH", Hash.new)
52
+
53
+ other::OPTIONS.each do |option|
54
+ klass = Class.new(ClassyEnumValue) {
55
+ include other::Defaults if other.const_defined?("Defaults")
56
+ }
57
+
58
+ Object.const_set("#{other}#{option.to_s.camelize}", klass)
59
+
60
+ instance = klass.new(option, other::OPTIONS.index(option))
61
+
62
+ other::OPTION_HASH[option] = other::OPTION_HASH[option.to_s.downcase] = instance
63
+
64
+ ClassyEnum.const_set(option.to_s.upcase, instance)
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ module ClassyEnumAttributes
72
+ module ClassMethods
73
+ def classy_enum_attr(klass, method=nil)
74
+
75
+ method ||= klass
76
+
77
+ klass = klass.to_s.camelize.constantize
78
+
79
+ # Add ActiveRecord validation to ensure it won't be saved unless it's an option
80
+ self.send(:validates_inclusion_of, method, :in => klass.all)
81
+
82
+ self.instance_eval do
83
+
84
+ # Define getter method
85
+ define_method method do
86
+ klass.new(super)
87
+ end
88
+
89
+ # Define setter method
90
+ define_method "#{method}=" do |value|
91
+ super value.to_s
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+ end
98
+
99
+ def self.included(other)
100
+ other.extend ClassMethods
101
+ end
102
+
103
+ end
104
+
105
+ ActiveRecord::Base.send :include, ClassyEnumAttributes
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ module TestEnum
4
+ OPTIONS = [:one, :two, :three]
5
+
6
+ module Defaults
7
+ def do_something?
8
+ false
9
+ end
10
+ end
11
+
12
+ include ClassyEnum
13
+ end
14
+
15
+ class TestEnumTwo
16
+ def do_something?
17
+ true
18
+ end
19
+ end
20
+
21
+ describe ClassyEnum do
22
+
23
+ TestEnum::OPTIONS.each do |option|
24
+ it "should define a TestEnum#{option.to_s.capitalize} class" do
25
+ Object.const_defined?("TestEnum#{option.to_s.capitalize}").should be_true
26
+ end
27
+ end
28
+
29
+ it "should return an array of enums" do
30
+ TestEnum.all.should == TestEnum::OPTIONS.map {|o| TestEnum.new(o) }
31
+ end
32
+
33
+ it "should return an array of enums for a select tag" do
34
+ TestEnum.all_with_name.should == TestEnum::OPTIONS.map {|o| [TestEnum.new(o).name, TestEnum.new(o).to_s] }
35
+ end
36
+
37
+ it "should return a type error when adding an invalid option" do
38
+ TestEnum.new(:invalid_option).class.should == TypeError
39
+ end
40
+
41
+ context "with a collection of enums" do
42
+ before(:each) do
43
+ @one = TestEnum.new(:one)
44
+ @two = TestEnum.new(:two)
45
+ @three = TestEnum.new(:three)
46
+
47
+ @unordered = [@one, @three, @two]
48
+ end
49
+
50
+ it "should sort the enums" do
51
+ @unordered.sort.should == [@one, @two, @three]
52
+ end
53
+
54
+ it "should find the max enum based on its order" do
55
+ @unordered.max.should == @three
56
+ end
57
+ end
58
+
59
+ it "should find an enum by symbol" do
60
+ TestEnum.find(:one).class.should == TestEnumOne
61
+ end
62
+
63
+ it "should find an enum by string" do
64
+ TestEnum.find("one").class.should == TestEnumOne
65
+ end
66
+
67
+ end
68
+
69
+ describe "An ClassyEnumValue" do
70
+ before(:each) { @enum = TestEnum.new(:one) }
71
+
72
+ it "should have a name" do
73
+ @enum.name.should == "One"
74
+ end
75
+
76
+ it "should inherit the Default methods" do
77
+ @enum.do_something?.should be_false
78
+ end
79
+
80
+ it "should create the same instance with a string or symbol" do
81
+ @enum_string = TestEnum.new("one")
82
+
83
+ @enum.should == @enum_string
84
+ end
85
+ end
86
+
87
+ describe "An ClassyEnumValue" do
88
+ before(:each) { @enum = TestEnum.new(:two) }
89
+
90
+ it "should override the Default methods" do
91
+ @enum.do_something?.should be_true
92
+ end
93
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'rubygems'
5
+ require 'active_record'
6
+ require 'classy_enum'
7
+ require 'spec'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: classy_enum
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Peter Brown
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-21 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activerecord
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 5
46
+ segments:
47
+ - 2
48
+ - 3
49
+ version: "2.3"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: A utility that adds class based enum functionaltiy to ActiveRecord attributes
53
+ email: github@lette.us
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - LICENSE
60
+ - README.rdoc
61
+ files:
62
+ - .document
63
+ - .gitignore
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - VERSION
68
+ - classy_enum.gemspec
69
+ - init.rb
70
+ - lib/classy_enum.rb
71
+ - spec/classy_enum_spec.rb
72
+ - spec/spec.opts
73
+ - spec/spec_helper.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/beerlington/classy_enum
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.3.7
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: A class based enumerator utility for Ruby on Rails
108
+ test_files:
109
+ - spec/classy_enum_spec.rb
110
+ - spec/spec_helper.rb