has_enumeration 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -10,6 +10,10 @@ GEM
10
10
  activesupport (= 3.0.0)
11
11
  arel (~> 1.0.0)
12
12
  tzinfo (~> 0.3.23)
13
+ activerecord-jdbc-adapter (0.9.7-java)
14
+ activerecord-jdbcsqlite3-adapter (0.9.7-java)
15
+ activerecord-jdbc-adapter (= 0.9.7)
16
+ jdbc-sqlite3 (>= 3.6.3.054)
13
17
  activesupport (3.0.0)
14
18
  arel (1.0.1)
15
19
  activesupport (~> 3.0.0)
@@ -23,7 +27,10 @@ GEM
23
27
  diff-lcs (1.1.2)
24
28
  gherkin (2.1.5)
25
29
  trollop (~> 1.16.2)
30
+ gherkin (2.1.5-java)
31
+ trollop (~> 1.16.2)
26
32
  i18n (0.4.1)
33
+ jdbc-sqlite3 (3.6.3.054)
27
34
  json_pure (1.4.6)
28
35
  meta_where (0.9.2)
29
36
  activerecord (~> 3.0.0.rc2)
@@ -43,6 +50,7 @@ GEM
43
50
  tzinfo (0.3.23)
44
51
 
45
52
  PLATFORMS
53
+ java
46
54
  ruby
47
55
 
48
56
  DEPENDENCIES
data/HISTORY.txt CHANGED
@@ -1,3 +1,8 @@
1
+ Version 0.5.0:
2
+ - Default name of underlying attribute can be overridden with the :attribute
3
+ option
4
+ - Internal cleanup: removed some unnecessary code and patching for Rails 3
5
+
1
6
  Version 0.4.0:
2
7
  - MetaWhere interoperability
3
8
  - Tested with Rails 3.0.0
data/README.txt CHANGED
@@ -7,9 +7,12 @@ class TestObject < ActiveRecord::Base
7
7
  # Integer attribute with explicit value mapping
8
8
  has_enumeration :color, :red => 1, :green => 2, :blue => 3
9
9
 
10
-
11
10
  # String attribute with implicit value mapping
12
11
  # has_enumeration :color, [:red, :green, :blue]
12
+
13
+ # Attribute with a name other than the enumeration's name
14
+ # has_enumeration :color, {:red => 1, :green => 2, :blue => 3},
15
+ # :attribute => :hue
13
16
  end
14
17
 
15
18
  # Value and predicates
@@ -44,7 +47,7 @@ has_enumeration has been tested with all combinations for the following:
44
47
  Rubies:
45
48
  - 1.8.7-p302
46
49
  - 1.9.2-p0
47
- - JRuby 1.5.2
50
+ - JRuby 1.5.3
48
51
  ActiveRecord:
49
- - 2.3.8
52
+ - 2.3.9
50
53
  - 3.0.0
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ in the database. Predicates are provided for each symbol in the enumeration
15
15
  and the symbols may be used in finder methods. When using ActiveRecord 3,
16
16
  the symbols may also be used when interacting with the underlying Arel attribute
17
17
  for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
18
- Ruby 1.9.2, JRuby 1.5.1, ActiveRecord 2.3.8, and ActiveRecord 3.0.0.
18
+ Ruby 1.9.2, JRuby 1.5.3, ActiveRecord 2.3.9, and ActiveRecord 3.0.0.
19
19
  EOF
20
20
  gemspec.email = "greg@rujubu.com"
21
21
  gemspec.homepage = "http://github.com/gregspurrier/has_enumeration"
@@ -31,7 +31,8 @@ namespace :features do
31
31
  task :all => [:common, :rails3]
32
32
 
33
33
  Cucumber::Rake::Task.new(:common) do |t|
34
- features = %w(explicitly_mapped_enumeration implicitly_mapped_enumeration)
34
+ features = %w(explicitly_mapped_enumeration implicitly_mapped_enumeration
35
+ nonstandard_attribute_enumeration)
35
36
  feature_files = features.map {|f| "features/#{f}.feature"}.join(' ')
36
37
  t.cucumber_opts = feature_files
37
38
  end
data/TODO CHANGED
@@ -1,4 +1,5 @@
1
1
  Prior to 1.0.0:
2
- - Raise exception when assigning an invalid value
3
- - rdoc documentation for has_enumeration
2
+ - Raise exception when assigning an invalid value (?)
3
+ - Configurable support for nil
4
+ - rdoc documentation for has_enumeration
4
5
  - Rake task to automate testing the supported Rubies and ActiveRecord versions
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -0,0 +1,51 @@
1
+ Feature:
2
+
3
+ As a programmer using an ActiveRecord model with an integer column
4
+ representing an enumerated value
5
+ I want to be able to refer to the enumeration symbolically and have
6
+ predicates to test its value
7
+ So that I can write pretty, concise code.
8
+
9
+ Scenario Outline: set an enumerated value on an unsaved object
10
+ Given a model with an nonstandard-attribute enumeration of red, green, and blue
11
+ And an unsaved instance of that model
12
+ When I assign a symbolic value <assigned> to the enumeration
13
+ Then it should have the assigned value as its value
14
+ And the red? predicate should be <red?>
15
+ And the green? predicate should be <green?>
16
+ And the blue? predicate should be <blue?>
17
+
18
+ Scenarios: setting various values
19
+ | assigned | red? | green? | blue? |
20
+ | :red | true | false | false |
21
+ | :green | false | true | false |
22
+ | :blue | false | false | true |
23
+
24
+ Scenario Outline: set an enumerated value then save and reload
25
+ Given a model with an nonstandard-attribute enumeration of red, green, and blue
26
+ And an unsaved instance of that model
27
+ When I assign a symbolic value <assigned> to the enumeration
28
+ And I save and reload the object
29
+ Then it should have the assigned value as its value
30
+ And the red? predicate should be <red?>
31
+ And the green? predicate should be <green?>
32
+ And the blue? predicate should be <blue?>
33
+
34
+ Scenarios: setting various values
35
+ | assigned | red? | green? | blue? |
36
+ | :red | true | false | false |
37
+ | :green | false | true | false |
38
+ | :blue | false | false | true |
39
+
40
+ Scenario Outline: find objects with a specific enumerated value
41
+ Given a model with an nonstandard-attribute enumeration of red, green, and blue
42
+ And a set of objects with a variety of values for the enumeration
43
+ When I query for objects with the value <value>
44
+ Then I should get all of the objects having that value
45
+ And I should not get any objects having other values
46
+
47
+ Scenarios: querying various values
48
+ | value |
49
+ | :red |
50
+ | :green |
51
+ | :blue |
@@ -6,6 +6,10 @@ Given /^a model with an implicitly-mapped enumeration of red, green, and blue$/
6
6
  @model_class = ImplicitlyMappedModel
7
7
  end
8
8
 
9
+ Given /^a model with an nonstandard-attribute enumeration of red, green, and blue$/ do
10
+ @model_class = NonstandardAttributeModel
11
+ end
12
+
9
13
  Given /^an unsaved instance of that model$/ do
10
14
  @object = @model_class.new
11
15
  end
@@ -23,4 +23,8 @@ class CreateTables < ActiveRecord::Migration
23
23
  create_table :implicitly_mapped_models, :force => true do |t|
24
24
  t.string :color
25
25
  end
26
+
27
+ create_table :nonstandard_attribute_models, :force => true do |t|
28
+ t.integer :hue
29
+ end
26
30
  end
@@ -0,0 +1,4 @@
1
+ class NonstandardAttributeModel < ActiveRecord::Base
2
+ has_enumeration :color, {:red => 1, :green => 2, :blue => 3},
3
+ :attribute => :hue
4
+ end
@@ -1,6 +1,6 @@
1
1
  module HasEnumeration
2
2
  module ClassMethods
3
- def has_enumeration(attribute, mapping)
3
+ def has_enumeration(enumeration, mapping, options = {})
4
4
  unless mapping.is_a?(Hash)
5
5
  # Recast the mapping as a symbol -> string hash
6
6
  mapping_hash = {}
@@ -8,34 +8,36 @@ module HasEnumeration
8
8
  mapping = mapping_hash
9
9
  end
10
10
 
11
+ # The underlying attribute
12
+ attribute = options[:attribute] || enumeration
13
+
11
14
  # ActiveRecord's composed_of method will do most of the work for us.
12
15
  # All we have to do is cons up a class that implements the bidirectional
13
16
  # mapping described by the provided hash.
14
17
  klass = create_enumeration_mapping_class(mapping)
15
- attr_enumeration_mapping_classes[attribute] = klass
18
+ attr_enumeration_mapping_classes[enumeration] = klass
16
19
 
17
20
  # Bind the class to a name within the scope of this class
18
- attribute_name = attribute.to_s
19
- mapping_class_name = attribute_name.camelize
21
+ mapping_class_name = enumeration.to_s.camelize
20
22
  const_set(mapping_class_name, klass)
21
23
  scoped_class_name = [self.name, mapping_class_name].join('::')
22
24
 
23
- composed_of(attribute,
25
+ composed_of(enumeration,
24
26
  :class_name => scoped_class_name,
25
- :mapping => [attribute_name, 'raw_value'],
27
+ :mapping => [attribute.to_s, 'raw_value'],
26
28
  :converter => :from_sym
27
29
  )
28
30
 
29
- # Install our aggregate condition handling override, but only once
30
- unless @aggregate_conditions_override_installed
31
- extend HasEnumeration::AggregateConditionsOverride
32
- @aggregate_conditions_override_installed = true
33
- end
34
-
35
- if respond_to?(:arel_table)
31
+ if ActiveRecord::VERSION::MAJOR >= 3
36
32
  # Install this attributes mapping for use later when extending
37
33
  # Arel attributes on the fly.
38
34
  ::Arel::Table.has_enumeration_mappings[table_name][attribute] = mapping
35
+ else
36
+ # Install our aggregate condition handling override, but only once
37
+ unless @aggregate_conditions_override_installed
38
+ extend HasEnumeration::AggregateConditionsOverride
39
+ @aggregate_conditions_override_installed = true
40
+ end
39
41
  end
40
42
  end
41
43
 
@@ -1,13 +1,14 @@
1
1
  require 'active_record'
2
2
 
3
+ # Install our common ActiveRecord extentions
3
4
  require 'has_enumeration/class_methods'
4
- require 'has_enumeration/aggregate_conditions_override'
5
-
6
- # Install our ActiveRecord extentions
7
5
  ActiveRecord::Base.extend(HasEnumeration::ClassMethods)
8
6
 
9
- # If Arel is present, extend it
10
- if defined? Arel::Table
7
+ # For ActiveRecord 3, extend Arel::Table, otherwise we'll need
8
+ # our specialization of aggregate_conditions_override
9
+ if ActiveRecord::VERSION::MAJOR >= 3
11
10
  require 'has_enumeration/arel/table_extensions'
12
11
  Arel::Table.send(:include, HasEnumeration::Arel::TableExtensions)
12
+ else
13
+ require 'has_enumeration/aggregate_conditions_override'
13
14
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_enumeration
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Greg Spurrier
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-01 00:00:00 -07:00
18
+ date: 2010-10-05 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -27,7 +27,7 @@ description: |
27
27
  and the symbols may be used in finder methods. When using ActiveRecord 3,
28
28
  the symbols may also be used when interacting with the underlying Arel attribute
29
29
  for the enumeration. has_enumeration has been tested with Ruby 1.8.7,
30
- Ruby 1.9.2, JRuby 1.5.1, ActiveRecord 2.3.8, and ActiveRecord 3.0.0.
30
+ Ruby 1.9.2, JRuby 1.5.3, ActiveRecord 2.3.9, and ActiveRecord 3.0.0.
31
31
 
32
32
  email: greg@rujubu.com
33
33
  executables: []
@@ -51,10 +51,12 @@ files:
51
51
  - features/explicitly_mapped_enumeration.feature
52
52
  - features/implicitly_mapped_enumeration.feature
53
53
  - features/meta_where_queries.feature
54
+ - features/nonstandard_attribute_enumeration.feature
54
55
  - features/step_definitions/has_enumeration_steps.rb
55
56
  - features/support/env.rb
56
57
  - features/support/explicitly_mapped_model.rb
57
58
  - features/support/implicitly_mapped_model.rb
59
+ - features/support/nonstandard_attribute_model.rb
58
60
  - lib/has_enumeration.rb
59
61
  - lib/has_enumeration/aggregate_conditions_override.rb
60
62
  - lib/has_enumeration/arel/table_extensions.rb