attribute_mapper 1.0.0 → 1.1.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.
- data/.gitignore +3 -1
- data/Rakefile +12 -2
- data/VERSION.yml +3 -3
- data/attribute_mapper.gemspec +29 -7
- data/lib/attribute_mapper.rb +40 -8
- data/test/attribute_mapper_test.rb +6 -21
- data/test/test_helper.rb +22 -0
- metadata +4 -2
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -5,13 +5,23 @@ task :default => :test
|
|
5
5
|
|
6
6
|
Rake::TestTask.new do |t|
|
7
7
|
t.test_files = FileList['test/*_test.rb']
|
8
|
-
t.ruby_opts = ['-rubygems']
|
8
|
+
t.ruby_opts = ['-Itest', '-rubygems']
|
9
9
|
end
|
10
10
|
|
11
11
|
Rcov::RcovTask.new do |t|
|
12
12
|
t.test_files = FileList['test/*_test.rb']
|
13
13
|
end
|
14
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
|
+
|
15
25
|
begin
|
16
26
|
require 'jeweler'
|
17
27
|
Jeweler::Tasks.new do |s|
|
@@ -24,4 +34,4 @@ begin
|
|
24
34
|
end
|
25
35
|
rescue LoadError
|
26
36
|
puts "Jeweler not available. Install it with: gem install technicalpickles-jeweler -s http://gems.github.com"
|
27
|
-
end
|
37
|
+
end
|
data/VERSION.yml
CHANGED
data/attribute_mapper.gemspec
CHANGED
@@ -1,25 +1,46 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
1
4
|
# -*- encoding: utf-8 -*-
|
2
5
|
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = %q{attribute_mapper}
|
5
|
-
s.version = "
|
8
|
+
s.version = "1.1.1"
|
6
9
|
|
7
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
11
|
s.authors = ["Marcel Molina Jr.", "Bruce Williams", "Adam Keys"]
|
9
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-01-08}
|
10
13
|
s.description = %q{Provides a transparent interface for mapping symbolic representations to a column in the database of a more primitive type.}
|
11
14
|
s.email = %q{adamkkeys@gmail.com}
|
12
|
-
s.
|
13
|
-
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION.yml",
|
25
|
+
"attribute_mapper.gemspec",
|
26
|
+
"init.rb",
|
27
|
+
"lib/attribute_mapper.rb",
|
28
|
+
"test/attribute_mapper_test.rb",
|
29
|
+
"test/test_helper.rb"
|
30
|
+
]
|
14
31
|
s.homepage = %q{http://github.com/therealadam/attribute_mapper}
|
15
|
-
s.rdoc_options = ["--
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
16
33
|
s.require_paths = ["lib"]
|
17
|
-
s.rubygems_version = %q{1.3.
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
18
35
|
s.summary = %q{Map symbolic types to primitive types and stash them in a column.}
|
36
|
+
s.test_files = [
|
37
|
+
"test/attribute_mapper_test.rb",
|
38
|
+
"test/test_helper.rb"
|
39
|
+
]
|
19
40
|
|
20
41
|
if s.respond_to? :specification_version then
|
21
42
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
22
|
-
s.specification_version =
|
43
|
+
s.specification_version = 3
|
23
44
|
|
24
45
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
25
46
|
else
|
@@ -27,3 +48,4 @@ Gem::Specification.new do |s|
|
|
27
48
|
else
|
28
49
|
end
|
29
50
|
end
|
51
|
+
|
data/lib/attribute_mapper.rb
CHANGED
@@ -5,14 +5,36 @@ module AttributeMapper
|
|
5
5
|
model.send(:include, InstanceMethods)
|
6
6
|
end
|
7
7
|
|
8
|
-
module ClassMethods
|
8
|
+
module ClassMethods # @private
|
9
|
+
|
10
|
+
# Map a column in your table to a human-friendly attribute on your model. When
|
11
|
+
# ++attribute is accessed, it will return the key from the mapping hash. When
|
12
|
+
# the attribute is updated, the value from the mapping hash is written to the
|
13
|
+
# database.
|
14
|
+
#
|
15
|
+
# A class method is also added providing access to the mapping
|
16
|
+
# hash, i.e. defining an attribute ++status++ will add a
|
17
|
+
# ++statuses++ class method that returns the hash passed to the
|
18
|
+
# ++:to++ option.
|
19
|
+
#
|
20
|
+
# Predicates are also added to each object for each attribute. If you have a key
|
21
|
+
# ++open++ in your mapping, your objects will have an ++open?++ method that
|
22
|
+
# returns true if the attribute value is ++:open++
|
23
|
+
#
|
24
|
+
# @example Define a Ticket model with a status column that maps to open or closed
|
25
|
+
# map_attribute :status, :to => {:open => 1, :closed => 2}
|
26
|
+
#
|
27
|
+
# @param [String] attribute the column to map on
|
28
|
+
# @param [Hash] options the options for this attribute
|
29
|
+
# @option options [Hash] :to The enumeration to use for this attribute. See example above.
|
9
30
|
def map_attribute(attribute, options)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
31
|
+
mapping = options[:to]
|
32
|
+
verify_existence_of attribute
|
33
|
+
add_accessor_for attribute, mapping
|
34
|
+
add_predicates_for attribute, mapping.keys
|
35
|
+
override attribute
|
14
36
|
end
|
15
|
-
|
37
|
+
|
16
38
|
private
|
17
39
|
def add_accessor_for(attribute, mapping)
|
18
40
|
class_eval(<<-EVAL)
|
@@ -22,7 +44,17 @@ module AttributeMapper
|
|
22
44
|
end
|
23
45
|
end
|
24
46
|
EVAL
|
25
|
-
|
47
|
+
end
|
48
|
+
|
49
|
+
def add_predicates_for(attribute, names)
|
50
|
+
names.each do |name|
|
51
|
+
class_eval(<<-RUBY)
|
52
|
+
def #{name}?
|
53
|
+
self.#{attribute} == :#{name}
|
54
|
+
end
|
55
|
+
RUBY
|
56
|
+
end
|
57
|
+
end
|
26
58
|
|
27
59
|
def override(*args)
|
28
60
|
override_getters *args
|
@@ -60,4 +92,4 @@ module AttributeMapper
|
|
60
92
|
mapping[check_value] || check_value
|
61
93
|
end
|
62
94
|
end
|
63
|
-
end
|
95
|
+
end
|
@@ -1,24 +1,4 @@
|
|
1
|
-
require '
|
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
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# Pseudo model for testing purposes
|
19
|
-
class Ticket < ActiveRecord::Base
|
20
|
-
include AttributeMapper
|
21
|
-
end
|
1
|
+
require 'test_helper'
|
22
2
|
|
23
3
|
class AttributeMapperTest < Test::Unit::TestCase
|
24
4
|
|
@@ -47,6 +27,11 @@ class AttributeMapperTest < Test::Unit::TestCase
|
|
47
27
|
assert_equal :closed, ticket.status
|
48
28
|
assert_equal mapping[:closed], ticket[:status]
|
49
29
|
end
|
30
|
+
|
31
|
+
should 'define predicates for each attribute' do
|
32
|
+
ticket.status = :open
|
33
|
+
assert ticket.open?
|
34
|
+
end
|
50
35
|
|
51
36
|
should "allow indifferent access to setters" do
|
52
37
|
assert_nothing_raised do
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Pseudo model for testing purposes
|
19
|
+
class Ticket < ActiveRecord::Base
|
20
|
+
include AttributeMapper
|
21
|
+
end
|
22
|
+
|
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.
|
4
|
+
version: 1.1.1
|
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:
|
14
|
+
date: 2010-01-08 00:00:00 -06:00
|
15
15
|
default_executable:
|
16
16
|
dependencies: []
|
17
17
|
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- init.rb
|
35
35
|
- lib/attribute_mapper.rb
|
36
36
|
- test/attribute_mapper_test.rb
|
37
|
+
- test/test_helper.rb
|
37
38
|
has_rdoc: true
|
38
39
|
homepage: http://github.com/therealadam/attribute_mapper
|
39
40
|
licenses: []
|
@@ -64,3 +65,4 @@ specification_version: 3
|
|
64
65
|
summary: Map symbolic types to primitive types and stash them in a column.
|
65
66
|
test_files:
|
66
67
|
- test/attribute_mapper_test.rb
|
68
|
+
- test/test_helper.rb
|