active_enum 0.9.1 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ [0.9.3] : 2011-07-31
2
+ - Add Railtie for proper loading of ActiveRecord extensions.
3
+
4
+ [0.9.2] : 2011-07-31
5
+ - Deprecate :as_defined order in favour of :natural.
6
+ - Allow storage options to passed to ActiveEnum.storage = :foo, :bar => true.
7
+ - Also allow ActiveEnum.storage_options = { :bar => true }
8
+
1
9
  [0.9.1] : 2011-01-12
2
10
  - Fix SimpleForm helper for v1.3.0 with backwards compat.
3
11
 
@@ -75,7 +75,7 @@ To define the sorting of returned values use the order method. Which is useful f
75
75
  value :id => 2, :name => 'Female'
76
76
  end
77
77
 
78
- By default the order is ascending (:asc) but you can also choose descending (:desc) or in order of definition (:as_defined).
78
+ By default the order is ascending (:asc) but you can also choose descending (:desc) or in natural order of definition (:natural).
79
79
  The last option is useful when supplying id values but have a specific order needed to display them.
80
80
 
81
81
  === Enumerate model attributes
data/Rakefile CHANGED
@@ -1,36 +1,11 @@
1
- require 'rubygems'
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
2
4
  require 'rake/rdoctask'
3
- require 'rake/gempackagetask'
4
- require 'rubygems/specification'
5
5
  require 'rspec/core/rake_task'
6
- require 'lib/active_enum/version'
7
-
8
- GEM_NAME = "active_enum"
9
- GEM_VERSION = ActiveEnum::VERSION
10
-
11
- spec = Gem::Specification.new do |s|
12
- s.name = GEM_NAME
13
- s.version = GEM_VERSION
14
- s.platform = Gem::Platform::RUBY
15
- s.rubyforge_project = "active_enum"
16
- s.has_rdoc = true
17
- s.extra_rdoc_files = ["README.rdoc"]
18
- s.summary = "Define enum classes in Rails and use them to enumerate ActiveRecord attributes"
19
- s.description = s.summary
20
- s.author = "Adam Meehan"
21
- s.email = "adam.meehan@gmail.com"
22
- s.homepage = "http://github.com/adzap/active_enum"
23
- s.require_path = 'lib'
24
- s.files = %w(active_enum.gemspec MIT-LICENSE CHANGELOG README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
25
- end
26
-
27
- desc 'Default: run specs.'
28
- task :default => :spec
29
6
 
30
7
  desc "Run specs"
31
- RSpec::Core::RakeTask.new do |t|
32
- t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
33
- end
8
+ RSpec::Core::RakeTask.new(:spec)
34
9
 
35
10
  desc "Generate code coverage"
36
11
  RSpec::Core::RakeTask.new(:coverage) do |t|
@@ -47,18 +22,5 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
47
22
  rdoc.rdoc_files.include('lib/**/*.rb')
48
23
  end
49
24
 
50
- Rake::GemPackageTask.new(spec) do |pkg|
51
- pkg.gem_spec = spec
52
- end
53
-
54
- desc "install the gem locally"
55
- task :install => [:package] do
56
- sh %{gem install pkg/#{GEM_NAME}-#{GEM_VERSION}}
57
- end
58
-
59
- desc "create a gemspec file"
60
- task :make_spec do
61
- File.open("#{GEM_NAME}.gemspec", "w") do |file|
62
- file.puts spec.to_ruby
63
- end
64
- end
25
+ desc 'Default: run specs.'
26
+ task :default => :spec
@@ -1,29 +1,20 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active_enum/version"
2
4
 
3
5
  Gem::Specification.new do |s|
4
- s.name = %q{active_enum}
5
- s.version = "0.9.1"
6
-
7
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Adam Meehan"]
9
- s.date = %q{2011-01-12}
6
+ s.name = "active_enum"
7
+ s.version = ActiveEnum::VERSION
8
+ s.authors = ["Adam Meehan"]
9
+ s.summary = %q{Define enum classes in Rails and use them to enumerate ActiveRecord attributes}
10
10
  s.description = %q{Define enum classes in Rails and use them to enumerate ActiveRecord attributes}
11
- s.email = %q{adam.meehan@gmail.com}
12
- s.extra_rdoc_files = ["README.rdoc"]
13
- s.files = ["active_enum.gemspec", "MIT-LICENSE", "CHANGELOG", "README.rdoc", "Rakefile", "lib/active_enum", "lib/active_enum/acts_as_enum.rb", "lib/active_enum/base.rb", "lib/active_enum/extensions.rb", "lib/active_enum/form_helpers", "lib/active_enum/form_helpers/formtastic.rb", "lib/active_enum/form_helpers/simple_form.rb", "lib/active_enum/storage", "lib/active_enum/storage/abstract_store.rb", "lib/active_enum/storage/memory_store.rb", "lib/active_enum/version.rb", "lib/active_enum.rb", "lib/generators", "lib/generators/active_enum", "lib/generators/active_enum/install_generator.rb", "lib/generators/active_enum/templates", "lib/generators/active_enum/templates/active_enum.rb", "spec/active_enum", "spec/active_enum/acts_as_enum_spec.rb", "spec/active_enum/base_spec.rb", "spec/active_enum/extensions_spec.rb", "spec/active_enum/form_helpers", "spec/active_enum/form_helpers/formtastic_spec.rb", "spec/active_enum/form_helpers/simple_form_spec.rb", "spec/active_enum/storage", "spec/active_enum/storage/memory_store_spec.rb", "spec/active_enum_spec.rb", "spec/schema.rb", "spec/spec_helper.rb"]
14
- s.homepage = %q{http://github.com/adzap/active_enum}
15
- s.require_paths = ["lib"]
16
- s.rubyforge_project = %q{active_enum}
17
- s.rubygems_version = %q{1.3.7}
18
- s.summary = %q{Define enum classes in Rails and use them to enumerate ActiveRecord attributes}
11
+ s.email = %q{adam.meehan@gmail.com}
12
+ s.homepage = %q{http://github.com/adzap/active_enum}
19
13
 
20
- if s.respond_to? :specification_version then
21
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
- s.specification_version = 3
14
+ s.require_paths = ["lib"]
15
+ s.files = `git ls-files`.split("\n") - %w{ .gitignore .rspec Gemfile Gemfile.lock autotest/discover.rb }
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.extra_rdoc_files = ["README.rdoc", "CHANGELOG", "MIT-LICENSE"]
23
18
 
24
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- else
26
- end
27
- else
28
- end
19
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0"])
29
20
  end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'active_enum'
@@ -1,6 +1,5 @@
1
1
  require 'active_enum/base'
2
2
  require 'active_enum/extensions'
3
- require 'active_enum/acts_as_enum' if defined?(ActiveRecord)
4
3
  require 'active_enum/storage/abstract_store'
5
4
 
6
5
  module ActiveEnum
@@ -15,17 +14,21 @@ module ActiveEnum
15
14
  mattr_accessor :storage
16
15
  @@storage = :memory
17
16
 
18
- mattr_accessor :extend_classes
19
- @@extend_classes = [ defined?(ActiveRecord) && ActiveRecord::Base ].compact
17
+ mattr_accessor :storage_options
18
+ @@storage_options = {}
20
19
 
21
- def self.extend_classes=(klasses)
22
- @@extend_classes = klasses
23
- klasses.each {|klass| klass.send(:include, ActiveEnum::Extensions) }
20
+ def storage=(*args)
21
+ @@storage_options = args.extract_options!
22
+ @@storage = args.first
24
23
  end
25
24
 
25
+ mattr_accessor :extend_classes
26
+ @@extend_classes = []
27
+
26
28
  # Setup method for plugin configuration
27
29
  def self.setup
28
30
  yield self
31
+ extend_classes!
29
32
  end
30
33
 
31
34
  class EnumDefinitions
@@ -43,4 +46,14 @@ module ActiveEnum
43
46
  EnumDefinitions.new.instance_eval(&block)
44
47
  end
45
48
 
49
+ def self.storage_class
50
+ @@storage_class ||= "ActiveEnum::Storage::#{storage.to_s.classify}Store".constantize
51
+ end
52
+
53
+ private
54
+
55
+ def self.extend_classes!
56
+ extend_classes.each {|klass| klass.send(:include, ActiveEnum::Extensions) }
57
+ end
58
+
46
59
  end
@@ -23,9 +23,13 @@ module ActiveEnum
23
23
  end
24
24
 
25
25
  # Specify order enum values are returned.
26
- # Allowed values are :asc, :desc or :as_defined
26
+ # Allowed values are :asc, :desc or :natural
27
27
  #
28
28
  def order(order)
29
+ if order == :as_defined
30
+ ActiveSupport::Deprecation.warn("You are using the order :as_defined which has been deprecated. Use :natural.")
31
+ order = :natural
32
+ end
29
33
  @order = order
30
34
  end
31
35
 
@@ -90,11 +94,7 @@ module ActiveEnum
90
94
  end
91
95
 
92
96
  def store
93
- @store ||= storage_class.new(self, @order || :asc)
94
- end
95
-
96
- def storage_class
97
- "ActiveEnum::Storage::#{ActiveEnum.storage.to_s.classify}Store".constantize
97
+ @store ||= ActiveEnum.storage_class.new(self, @order || :asc, ActiveEnum.storage_options)
98
98
  end
99
99
 
100
100
  end
@@ -22,38 +22,49 @@ module ActiveEnum
22
22
  # enumerate :sex do
23
23
  # value :id => 1, :name => 'Male'
24
24
  # end
25
- #
26
- # # Multiple attributes with same enum
25
+ #
26
+ # # Multiple attributes with same enum
27
27
  # enumerate :to, :from, :with => Sex
28
28
  #
29
29
  def enumerate(*attributes, &block)
30
30
  self.enumerated_attributes ||= {}
31
- options = attributes.extract_options!
32
- attributes.each do |attribute|
33
- begin
34
- if block_given?
35
- enum_class_name = "#{self.name}::#{attribute.to_s.camelize}"
36
- eval("class #{enum_class_name} < ActiveEnum::Base; end")
37
- enum = enum_class_name.constantize
38
- enum.class_eval &block
39
- else
40
- enum = options[:with] || attribute.to_s.classify.constantize
41
- end
42
- attribute = attribute.to_sym
43
- self.enumerated_attributes[attribute] = enum
44
-
45
- define_active_enum_read_method(attribute)
46
- define_active_enum_write_method(attribute)
47
- define_active_enum_question_method(attribute)
48
- rescue NameError => e
49
- raise e unless e.message =~ /uninitialized constant/
50
- raise ActiveEnum::EnumNotFound, "Enum class could not be found for attribute '#{attribute}' in class #{self}. Specify the enum class using the :with option."
51
- end
52
- end
31
+ options = attributes.extract_options!
32
+
33
+ attributes.each do |attribute|
34
+ begin
35
+ if block_given?
36
+ enum = define_implicit_enum_class_for_attribute(attribute, block)
37
+ else
38
+ enum = options[:with] || attribute.to_s.camelize.constantize
39
+ end
40
+
41
+ attribute = attribute.to_sym
42
+ enumerated_attributes[attribute] = enum
43
+
44
+ define_active_enum_methods_for_attribute(attribute)
45
+ rescue NameError => e
46
+ raise e unless e.message =~ /uninitialized constant/
47
+ raise ActiveEnum::EnumNotFound, "Enum class could not be found for attribute '#{attribute}' in class #{self}. Specify the enum class using the :with option."
48
+ end
49
+ end
53
50
  end
54
51
 
55
52
  def active_enum_for(attribute)
56
- self.enumerated_attributes && self.enumerated_attributes[attribute.to_sym]
53
+ enumerated_attributes[attribute.to_sym]
54
+ end
55
+
56
+ def define_active_enum_methods_for_attribute(attribute)
57
+ define_active_enum_read_method(attribute)
58
+ define_active_enum_write_method(attribute)
59
+ define_active_enum_question_method(attribute)
60
+ end
61
+
62
+ def define_implicit_enum_class_for_attribute(attribute, block)
63
+ enum_class_name = "#{name}::#{attribute.to_s.camelize}"
64
+ eval("class #{enum_class_name} < ActiveEnum::Base; end")
65
+ enum = enum_class_name.constantize
66
+ enum.class_eval &block
67
+ enum
57
68
  end
58
69
 
59
70
  # Define read method to allow an argument for the enum component
@@ -118,7 +129,7 @@ module ActiveEnum
118
129
  if arg
119
130
  send(attribute) == self.class.active_enum_for(attribute)[arg]
120
131
  else
121
- super
132
+ super()
122
133
  end
123
134
  end
124
135
  end
@@ -0,0 +1,11 @@
1
+ module ActiveEnum
2
+ class Railtie < Rails::Railtie
3
+ initializer "active_enum.active_record_extensions" do
4
+ ActiveSupport.on_load(:active_record) do
5
+ require 'active_enum/acts_as_enum'
6
+
7
+ ActiveEnum.extend_orms = [ ActiveRecord::Base ]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -5,8 +5,8 @@ module ActiveEnum
5
5
  class NotImplemented < StandardError; end
6
6
 
7
7
  class AbstractStore
8
- def initialize(enum_class, order)
9
- @enum, @order = enum_class, order
8
+ def initialize(enum_class, order, options={})
9
+ @enum, @order, @options = enum_class, order, options
10
10
  end
11
11
 
12
12
  def set(id, name, meta=nil)
@@ -26,7 +26,7 @@ module ActiveEnum
26
26
  end
27
27
 
28
28
  def sort!
29
- return if @order == :as_defined
29
+ return if @order == :natural
30
30
 
31
31
  case @order
32
32
  when :asc
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '0.9.1'
2
+ VERSION = '0.9.3'
3
3
  end
@@ -131,7 +131,7 @@ describe ActiveEnum::Base do
131
131
 
132
132
  it 'should return sorted values by id using order setting' do
133
133
  enum = define_enum do
134
- order :as_defined
134
+ order :natural
135
135
  value :id => 3, :name => 'Name 3'
136
136
  value :id => 1, :name => 'Name 1'
137
137
  value :id => 2, :name => 'Name 2'
@@ -94,8 +94,8 @@ describe ActiveEnum::Storage::MemoryStore do
94
94
  store.values.should == [[2, 'Name 2'], [1,'Name 1']]
95
95
  end
96
96
 
97
- it 'should not sort values when passed :as_defined' do
98
- @order = :as_defined
97
+ it 'should not sort values when passed :natural' do
98
+ @order = :natural
99
99
  store.set 1, 'Name 1'
100
100
  store.set 3, 'Name 3'
101
101
  store.set 2, 'Name 2'
@@ -34,9 +34,14 @@ describe ActiveEnum do
34
34
  end
35
35
  end
36
36
 
37
- describe ".extend_classes" do
37
+ describe ".extend_classes!" do
38
38
  it 'should add enumerate extensions to given classes' do
39
- ActiveRecord::Base.should respond_to(:enumerate)
39
+ ActiveEnum.extend_classes = [NotActiveRecord]
40
+
41
+ NotActiveRecord.should_not respond_to(:enumerate)
42
+
43
+ ActiveEnum.extend_classes!
44
+
40
45
  NotActiveRecord.should respond_to(:enumerate)
41
46
  end
42
47
  end
@@ -7,6 +7,7 @@ require 'action_view'
7
7
  require 'action_mailer'
8
8
 
9
9
  require 'active_enum'
10
+ require 'active_enum/acts_as_enum'
10
11
 
11
12
  module Config
12
13
  class Application < Rails::Application
@@ -31,7 +32,8 @@ class NotActiveRecord
31
32
  attr_accessor :name
32
33
  end
33
34
 
34
- ActiveEnum.extend_classes = [ActiveRecord::Base, NotActiveRecord]
35
+ ActiveEnum.extend_classes = [ActiveRecord::Base]
36
+ ActiveEnum.extend_classes!
35
37
 
36
38
  module SpecHelper
37
39
  def reset_class(klass, &block)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- hash: 57
5
- prerelease: false
4
+ hash: 61
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 1
10
- version: 0.9.1
9
+ - 3
10
+ version: 0.9.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Meehan
@@ -15,10 +15,24 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-12 00:00:00 +11:00
18
+ date: 2011-09-01 00:00:00 +10:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ version: "3.0"
34
+ type: :runtime
35
+ version_requirements: *id001
22
36
  description: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
23
37
  email: adam.meehan@gmail.com
24
38
  executables: []
@@ -27,21 +41,25 @@ extensions: []
27
41
 
28
42
  extra_rdoc_files:
29
43
  - README.rdoc
30
- files:
31
- - active_enum.gemspec
44
+ - CHANGELOG
32
45
  - MIT-LICENSE
46
+ files:
33
47
  - CHANGELOG
48
+ - MIT-LICENSE
34
49
  - README.rdoc
35
50
  - Rakefile
51
+ - active_enum.gemspec
52
+ - init.rb
53
+ - lib/active_enum.rb
36
54
  - lib/active_enum/acts_as_enum.rb
37
55
  - lib/active_enum/base.rb
38
56
  - lib/active_enum/extensions.rb
39
57
  - lib/active_enum/form_helpers/formtastic.rb
40
58
  - lib/active_enum/form_helpers/simple_form.rb
59
+ - lib/active_enum/railtie.rb
41
60
  - lib/active_enum/storage/abstract_store.rb
42
61
  - lib/active_enum/storage/memory_store.rb
43
62
  - lib/active_enum/version.rb
44
- - lib/active_enum.rb
45
63
  - lib/generators/active_enum/install_generator.rb
46
64
  - lib/generators/active_enum/templates/active_enum.rb
47
65
  - spec/active_enum/acts_as_enum_spec.rb
@@ -82,10 +100,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
100
  version: "0"
83
101
  requirements: []
84
102
 
85
- rubyforge_project: active_enum
86
- rubygems_version: 1.3.7
103
+ rubyforge_project:
104
+ rubygems_version: 1.5.2
87
105
  signing_key:
88
106
  specification_version: 3
89
107
  summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes
90
- test_files: []
91
-
108
+ test_files:
109
+ - spec/active_enum/acts_as_enum_spec.rb
110
+ - spec/active_enum/base_spec.rb
111
+ - spec/active_enum/extensions_spec.rb
112
+ - spec/active_enum/form_helpers/formtastic_spec.rb
113
+ - spec/active_enum/form_helpers/simple_form_spec.rb
114
+ - spec/active_enum/storage/memory_store_spec.rb
115
+ - spec/active_enum_spec.rb
116
+ - spec/schema.rb
117
+ - spec/spec_helper.rb