datamapper2 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Guardfile +6 -0
- data/Rakefile +1 -0
- data/dm2.gemspec +23 -0
- data/lib/dm2.rb +5 -0
- data/lib/dm2/mapper.rb +23 -0
- data/lib/dm2/unit_of_work/observer.rb +9 -0
- data/lib/dm2/unit_of_work/observer_definition.rb +37 -0
- data/lib/dm2/version.rb +3 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/unit/mapper_spec.rb +45 -0
- data/spec/unit/unit_of_work/observer_definition_spec.rb +110 -0
- data/spec/unit/unit_of_work/observer_spec.rb +12 -0
- metadata +86 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/dm2.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "dm2/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "datamapper2"
|
7
|
+
s.version = Dm2::VERSION
|
8
|
+
s.authors = ["kurko"]
|
9
|
+
s.email = ["chavedomundo@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Implements the DataMapper pattern for you.}
|
12
|
+
s.description = %q{Implements the DataMapper pattern for you.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "dm2"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "guard"
|
23
|
+
end
|
data/lib/dm2.rb
ADDED
data/lib/dm2/mapper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module DataMapper
|
2
|
+
class Mapper
|
3
|
+
@@observed_attributes = []
|
4
|
+
@@attributes_values = {}
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def attributes(*attributes)
|
8
|
+
@@observed_attributes = attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
def unit_of_work(object, method, values)
|
12
|
+
@@attributes_values = unit_of_work_observer.observe(object, self, method, values)
|
13
|
+
end
|
14
|
+
|
15
|
+
def unit_of_work_observer
|
16
|
+
DataMapper::UnitOfWork::Observer
|
17
|
+
end
|
18
|
+
|
19
|
+
def observed_attributes; @@observed_attributes; end
|
20
|
+
def attributes_values; @@attributes_values; end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Object
|
2
|
+
def self.method_added name
|
3
|
+
return true unless has_mapper?
|
4
|
+
return if @__last_methods_added && @__last_methods_added.include?(name)
|
5
|
+
return if self.methods.include?(name) && name != :name
|
6
|
+
send :set_unit_of_work_observer, name
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.set_unit_of_work_observer method_name
|
10
|
+
modified_method = :"modified_#{method_name}"
|
11
|
+
@__last_methods_added = [method_name, modified_method]
|
12
|
+
|
13
|
+
alias_method modified_method, method_name
|
14
|
+
define_value_method method_name
|
15
|
+
|
16
|
+
@__last_methods_added = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.has_mapper?
|
20
|
+
Module.const_defined?("#{self}Mapper")
|
21
|
+
rescue
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.define_value_method method_name
|
26
|
+
modified_method = modified_method_name method_name
|
27
|
+
define_method method_name.to_s do |*args, &block|
|
28
|
+
returned = send modified_method, *args, &block
|
29
|
+
Kernel.const_get("#{self.class}Mapper").unit_of_work self, method_name, args
|
30
|
+
returned
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.modified_method_name method_name
|
35
|
+
:"modified_#{method_name}"
|
36
|
+
end
|
37
|
+
end
|
data/lib/dm2/version.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
Dir["lib/**/*.rb"].each { |f| require "./"+f }
|
5
|
+
Dir["spec/support/**/*.rb"].each { |f| require "./"+f }
|
6
|
+
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.filter_run wip: true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe DataMapper::Mapper do
|
4
|
+
|
5
|
+
class Mapper < DataMapper::Mapper
|
6
|
+
end
|
7
|
+
|
8
|
+
subject { Mapper }
|
9
|
+
|
10
|
+
before do
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#attributes" do
|
15
|
+
it "should define attributes to map" do
|
16
|
+
expect { subject.attributes(:name, :age) }
|
17
|
+
.to change { subject.observed_attributes }
|
18
|
+
.from([])
|
19
|
+
.to([:name, :age])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#unit_of_work" do
|
24
|
+
class DummyMapperMapper < DataMapper::Mapper; end
|
25
|
+
class DummyMapper
|
26
|
+
attr_accessor :age
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
@observer = double("Observer")
|
31
|
+
@dummy = DummyMapper.new
|
32
|
+
@dummy.age = 100
|
33
|
+
DummyMapperMapper.stub(:unit_of_work_observer).and_return(@observer)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "transmits the message to UnitOfWork::Observer" do
|
37
|
+
@observer.should_receive(:observe)
|
38
|
+
.with(anything, DummyMapperMapper, an_instance_of(Symbol), an_instance_of(Array))
|
39
|
+
.and_return(@observer)
|
40
|
+
|
41
|
+
DummyMapperMapper.unit_of_work(@dummy, :a_method, [])
|
42
|
+
DummyMapperMapper.attributes_values.should == @observer
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Observer definition" do
|
4
|
+
|
5
|
+
class Person; end
|
6
|
+
class PersonMapper; def unit_of_work; end; end
|
7
|
+
class Account; end
|
8
|
+
class DoubleMapper; def unit_of_work; end; end
|
9
|
+
|
10
|
+
describe "objects without a unit of work observer" do
|
11
|
+
context "mapper observer is not set" do
|
12
|
+
before do
|
13
|
+
Object.should_not_receive(:set_unit_of_work_observer)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "when a method is added" do
|
17
|
+
class Account; def new_method; end; end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "when an attr_accessor is set" do
|
21
|
+
class Account; attr_accessor :attr_accessor_method; end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "when an attr_reader is set" do
|
25
|
+
class Account; attr_reader :attr_reader_method; end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "when an attr_write is set" do
|
29
|
+
class Account; attr_writer :attr_writer_method; end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "objects with a unit of work observer" do
|
35
|
+
context "mapper observer is set" do
|
36
|
+
it "when a method is added" do
|
37
|
+
Object.should_receive(:set_unit_of_work_observer).once
|
38
|
+
class Person; def new_method; end; end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "when an attr_accessor is set" do
|
42
|
+
Object.should_receive(:set_unit_of_work_observer).exactly(4).times
|
43
|
+
class Person; attr_accessor :attr_accessor_method, :second_attr_acessor_method; end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "when an attr_reader is set" do
|
47
|
+
Object.should_receive(:set_unit_of_work_observer).once
|
48
|
+
class Person; attr_writer :attr_writer_method; end
|
49
|
+
end
|
50
|
+
|
51
|
+
it "when an attr_write is set" do
|
52
|
+
Object.should_receive(:set_unit_of_work_observer).once
|
53
|
+
class Person; attr_reader :attr_reader_method; end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#set_unit_of_work_observer" do
|
59
|
+
it "sets an after filter for a pre-existent object" do
|
60
|
+
class Person; def has_after_filter; end; end
|
61
|
+
person = Person.new
|
62
|
+
PersonMapper.should_receive(:unit_of_work).with(person, :has_after_filter, []).once
|
63
|
+
person.has_after_filter
|
64
|
+
end
|
65
|
+
|
66
|
+
it "sets an after filter for a new object" do
|
67
|
+
class Double; def has_after_filter; end; end
|
68
|
+
a_double = Double.new
|
69
|
+
DoubleMapper.should_receive(:unit_of_work).with(a_double, :has_after_filter, []).once
|
70
|
+
a_double.has_after_filter
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "unit of work observer" do
|
75
|
+
class UoWObserverPersonMapper
|
76
|
+
@@last_call = []
|
77
|
+
def self.unit_of_work(object, property, args)
|
78
|
+
@@last_call << [object, property, args]
|
79
|
+
end
|
80
|
+
def self.last_call; @@last_call; end
|
81
|
+
end
|
82
|
+
|
83
|
+
class UoWObserverPerson
|
84
|
+
attr_accessor :first_name
|
85
|
+
def initialize; end
|
86
|
+
def whatever;end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should register each call sepparatelly" do
|
90
|
+
person = UoWObserverPerson.new
|
91
|
+
UoWObserverPersonMapper.last_call.should include [person, :initialize, []]
|
92
|
+
person.first_name = "John Doe"
|
93
|
+
UoWObserverPersonMapper.last_call.should include [person, :first_name=, ["John Doe"]]
|
94
|
+
person.whatever
|
95
|
+
UoWObserverPersonMapper.last_call.should include [person, :whatever, []]
|
96
|
+
UoWObserverPersonMapper.last_call.last[0].first_name.should == "John Doe"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "#has_mapper?" do
|
101
|
+
it "should return true for Person" do
|
102
|
+
Person.has_mapper?.should == true
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should return false for Account" do
|
106
|
+
Account.has_mapper?.should == false
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: datamapper2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- kurko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70178025149380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70178025149380
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: guard
|
27
|
+
requirement: &70178025147800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70178025147800
|
36
|
+
description: Implements the DataMapper pattern for you.
|
37
|
+
email:
|
38
|
+
- chavedomundo@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rspec
|
45
|
+
- Gemfile
|
46
|
+
- Guardfile
|
47
|
+
- Rakefile
|
48
|
+
- dm2.gemspec
|
49
|
+
- lib/dm2.rb
|
50
|
+
- lib/dm2/mapper.rb
|
51
|
+
- lib/dm2/unit_of_work/observer.rb
|
52
|
+
- lib/dm2/unit_of_work/observer_definition.rb
|
53
|
+
- lib/dm2/version.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- spec/unit/mapper_spec.rb
|
56
|
+
- spec/unit/unit_of_work/observer_definition_spec.rb
|
57
|
+
- spec/unit/unit_of_work/observer_spec.rb
|
58
|
+
homepage: ''
|
59
|
+
licenses: []
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project: dm2
|
78
|
+
rubygems_version: 1.8.11
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: Implements the DataMapper pattern for you.
|
82
|
+
test_files:
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
- spec/unit/mapper_spec.rb
|
85
|
+
- spec/unit/unit_of_work/observer_definition_spec.rb
|
86
|
+
- spec/unit/unit_of_work/observer_spec.rb
|