attribute_mapper 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -39,11 +39,13 @@ module AttributeMapper
39
39
  # @param [Hash] options the options for this attribute
40
40
  # @option options [Hash] :to The enumeration to use for this
41
41
  # attribute. See example above.
42
+ # @option options :predicate_methods Generate methods for checking
43
+ # whether an object has a certain attribute set
42
44
  def map_attribute(attribute, options)
43
45
  mapping = options[:to]
44
46
  verify_existence_of attribute
45
47
  add_accessor_for attribute, mapping
46
- add_predicates_for attribute, mapping.keys
48
+ add_predicates_for attribute, mapping.keys if options.fetch(:predicate_methods) { true }
47
49
  override attribute
48
50
  add_options_helper_for attribute, mapping
49
51
  end
@@ -51,7 +53,7 @@ module AttributeMapper
51
53
  private
52
54
 
53
55
  def add_accessor_for(attribute, mapping)
54
- class_eval(<<-EVAL)
56
+ class_eval(<<-EVAL, __FILE__, __LINE__)
55
57
  class << self
56
58
  def #{attribute.to_s.pluralize}
57
59
  #{mapping.inspect}
@@ -62,7 +64,7 @@ module AttributeMapper
62
64
 
63
65
  def add_predicates_for(attribute, names)
64
66
  names.each do |name|
65
- class_eval(<<-RUBY)
67
+ class_eval(<<-RUBY, __FILE__, __LINE__)
66
68
  def #{name}?
67
69
  self.#{attribute} == :#{name}
68
70
  end
@@ -89,7 +91,7 @@ module AttributeMapper
89
91
  end
90
92
 
91
93
  def override_getters(attribute)
92
- class_eval(<<-EVAL)
94
+ class_eval(<<-EVAL, __FILE__, __LINE__)
93
95
  def #{attribute}
94
96
  self.class.#{attribute.to_s.pluralize}.invert[read_attribute(:#{attribute})]
95
97
  end
@@ -97,7 +99,7 @@ module AttributeMapper
97
99
  end
98
100
 
99
101
  def override_setters(attribute)
100
- class_eval(<<-EVAL)
102
+ class_eval(<<-EVAL, __FILE__, __LINE__)
101
103
  def #{attribute}=(raw_value)
102
104
  value = resolve_value_of :#{attribute}, raw_value
103
105
  write_attribute(:#{attribute}, value)
@@ -114,14 +116,15 @@ module AttributeMapper
114
116
  module InstanceMethods
115
117
 
116
118
  private
117
-
119
+
118
120
  def resolve_value_of(attribute, raw_value)
121
+ return raw_value if raw_value.blank?
119
122
  check_value = raw_value.is_a?(String) ? raw_value.to_sym : raw_value
120
123
  mapping = self.class.send(attribute.to_s.pluralize)
121
124
  raise ArgumentError, "`#{check_value}' not present in attribute mapping `#{mapping.inspect}'" unless mapping.to_a.flatten.include? check_value
122
- mapping[check_value] || check_value
125
+ mapping.include?(check_value) ? mapping[check_value] : check_value
123
126
  end
124
-
127
+
125
128
  end
126
129
 
127
130
  end
@@ -96,6 +96,58 @@ class AttributeMapperTest < Test::Unit::TestCase
96
96
  assert_equal [[["Open", :open], ["Closed", :closed]], {:selected => :open}], ticket.status_options(false)
97
97
  end
98
98
 
99
+ context "setting nil as a valid value in the mapping" do
100
+ setup do
101
+ Ticket.map_attribute :status, :to => mapping(:unanswered => nil)
102
+ end
103
+
104
+ should "allow setting the status by primitive value" do
105
+ assert_nothing_raised do
106
+ ticket.status = mapping[:unanswered]
107
+ end
108
+ assert_equal :unanswered, ticket.status
109
+ assert ticket.unanswered?
110
+ end
111
+
112
+
113
+ should "allow setting the status by symbol" do
114
+ assert_nothing_raised do
115
+ ticket.status = :unanswered
116
+ end
117
+ assert_equal :unanswered, ticket.status
118
+ assert ticket.unanswered?
119
+ end
120
+
121
+ end
122
+
123
+ should "not raise an error when setting to a blank value" do
124
+ assert_nothing_raised {
125
+ ticket.update_attributes(:status => "")
126
+ }
127
+ end
128
+
129
+ should "not raise an error when setting to a nil value" do
130
+ assert_nothing_raised {
131
+ ticket.update_attributes(:status => nil)
132
+ }
133
+ end
134
+
135
+ should "turn off generation of predicate methods" do
136
+ new_ticket = Class.new(ActiveRecord::Base) do
137
+ set_table_name "tickets"
138
+
139
+ include AttributeMapper
140
+ map_attribute :status, :to => {:open => 1, :closed => 2}, :predicate_methods => false
141
+ end
142
+
143
+ t = new_ticket.new
144
+ t.status = :open
145
+
146
+ assert_raises(NoMethodError) {
147
+ t.open?
148
+ }
149
+ end
150
+
99
151
  end
100
152
 
101
153
  #######
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcel Molina Jr.
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2010-02-23 00:00:00 -06:00
14
+ date: 2009-03-10 00:00:00 -05:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
@@ -21,28 +21,19 @@ executables: []
21
21
 
22
22
  extensions: []
23
23
 
24
- extra_rdoc_files:
25
- - LICENSE
26
- - README.md
24
+ extra_rdoc_files: []
25
+
27
26
  files:
28
- - .gitignore
29
- - .kick
30
- - HISTORY.rdoc
31
- - LICENSE
32
27
  - README.md
33
- - Rakefile
34
- - VERSION.yml
35
- - attribute_mapper.gemspec
36
- - init.rb
37
28
  - lib/attribute_mapper.rb
38
29
  - test/attribute_mapper_test.rb
39
- - test/test_helper.rb
40
30
  has_rdoc: true
41
31
  homepage: http://github.com/therealadam/attribute_mapper
42
32
  licenses: []
43
33
 
44
34
  post_install_message:
45
35
  rdoc_options:
36
+ - --inline-source
46
37
  - --charset=UTF-8
47
38
  require_paths:
48
39
  - lib
@@ -65,6 +56,5 @@ rubygems_version: 1.3.5
65
56
  signing_key:
66
57
  specification_version: 3
67
58
  summary: Map symbolic types to primitive types and stash them in a column.
68
- test_files:
69
- - test/attribute_mapper_test.rb
70
- - test/test_helper.rb
59
+ test_files: []
60
+
data/.gitignore DELETED
@@ -1,4 +0,0 @@
1
- coverage
2
- pkg
3
- doc
4
- .yardoc
data/.kick DELETED
@@ -1,6 +0,0 @@
1
- recipe :ruby
2
-
3
- startup { log "Here we go." }
4
-
5
- Ruby.test_options << "-Itest"
6
- Ruby.test_options << "-rubygems"
data/HISTORY.rdoc DELETED
@@ -1,8 +0,0 @@
1
- = 1.2.0 (2010-02-23)
2
-
3
- * Verified compatibility with +update_attributes+ and +attr_accessible+.
4
- * Add +_options+ helper to make generating select forms for mappings easier
5
-
6
- = 1.1.1 (2010-01-08)
7
-
8
- * Changelog epoch.
data/LICENSE DELETED
@@ -1,19 +0,0 @@
1
- Copyright (c) 2007-2010 Marcel Molina, Jr., Bruce Williams, Adam Keys
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of
4
- this software and associated documentation files (the "Software"), to deal in
5
- the Software without restriction, including without limitation the rights to
6
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
- of the Software, and to permit persons to whom the Software is furnished to do
8
- so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all
11
- copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- SOFTWARE.
data/Rakefile DELETED
@@ -1,37 +0,0 @@
1
- require 'rake/testtask'
2
- require 'rcov/rcovtask'
3
-
4
- task :default => :test
5
-
6
- Rake::TestTask.new do |t|
7
- t.test_files = FileList['test/*_test.rb']
8
- t.ruby_opts = ['-Itest', '-rubygems']
9
- end
10
-
11
- Rcov::RcovTask.new do |t|
12
- t.test_files = FileList['test/*_test.rb']
13
- end
14
-
15
- begin
16
- require 'yard'
17
- YARD::Rake::YardocTask.new(:doc) do |t|
18
- t.options = ['--no-private']
19
- t.files = FileList['lib/**/*.rb']
20
- end
21
- rescue LoadError => e
22
- puts "yard not available. Install it with: gem install yard"
23
- end
24
-
25
- begin
26
- require 'jeweler'
27
- Jeweler::Tasks.new do |s|
28
- s.name = 'attribute_mapper'
29
- s.summary = 'Map symbolic types to primitive types and stash them in a column.'
30
- s.email = 'adamkkeys@gmail.com'
31
- s.homepage = 'http://github.com/therealadam/attribute_mapper'
32
- s.description = 'Provides a transparent interface for mapping symbolic representations to a column in the database of a more primitive type.'
33
- s.authors = ['Marcel Molina Jr.', 'Bruce Williams', 'Adam Keys']
34
- end
35
- rescue LoadError
36
- puts "Jeweler not available. Install it with: gem install technicalpickles-jeweler -s http://gems.github.com"
37
- end
data/VERSION.yml DELETED
@@ -1,5 +0,0 @@
1
- ---
2
- :minor: 2
3
- :patch: 0
4
- :build:
5
- :major: 1
@@ -1,29 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- Gem::Specification.new do |s|
4
- s.name = %q{attribute_mapper}
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 = ["Marcel Molina Jr.", "Bruce Williams", "Adam Keys"]
9
- s.date = %q{2009-03-10}
10
- s.description = %q{Provides a transparent interface for mapping symbolic representations to a column in the database of a more primitive type.}
11
- s.email = %q{adamkkeys@gmail.com}
12
- s.files = ["README.md", "VERSION.yml", "lib/attribute_mapper.rb", "test/attribute_mapper_test.rb"]
13
- s.has_rdoc = true
14
- s.homepage = %q{http://github.com/therealadam/attribute_mapper}
15
- s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
16
- s.require_paths = ["lib"]
17
- s.rubygems_version = %q{1.3.1}
18
- s.summary = %q{Map symbolic types to primitive types and stash them in a column.}
19
-
20
- if s.respond_to? :specification_version then
21
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
- s.specification_version = 2
23
-
24
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
- else
26
- end
27
- else
28
- end
29
- end
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "/lib/attribute_mapper")
data/test/test_helper.rb DELETED
@@ -1,23 +0,0 @@
1
- require 'test/unit'
2
- require 'shoulda'
3
-
4
- require 'active_record'
5
- require 'attribute_mapper'
6
-
7
- ActiveRecord::Base.establish_connection(
8
- :adapter => 'sqlite3',
9
- :database => ':memory:'
10
- )
11
-
12
- ActiveRecord::Schema.define do
13
- create_table :tickets do |table|
14
- table.column :status, :integer
15
- table.column :name, :string
16
- end
17
- end
18
-
19
- # Pseudo model for testing purposes
20
- class Ticket < ActiveRecord::Base
21
- include AttributeMapper
22
- end
23
-