discoverer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +13 -0
- data/Rakefile +2 -0
- data/discoverer.gemspec +22 -0
- data/docs/Discoverer/Model.md +11 -0
- data/lib/discoverer.rb +10 -0
- data/lib/discoverer/discoverer.rb +29 -0
- data/lib/discoverer/model.rb +42 -0
- data/lib/discoverer/reader.rb +20 -0
- data/lib/discoverer/version.rb +3 -0
- data/lib/discoverer/writer.rb +16 -0
- data/spec/discoverer/discoverer_spec.rb +124 -0
- data/spec/discoverer/model_spec.rb +133 -0
- data/spec/discoverer/reader_spec.rb +92 -0
- data/spec/discoverer/writer_spec.rb +78 -0
- data/spec/spec_helper.rb +2 -0
- metadata +117 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Xavier Via
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Discoverer
|
2
|
+
====
|
3
|
+
|
4
|
+
Clases
|
5
|
+
------
|
6
|
+
|
7
|
+
{Discoverer::Model}
|
8
|
+
|
9
|
+
To do
|
10
|
+
-----
|
11
|
+
|
12
|
+
- For the love of God, add some documentation!!!
|
13
|
+
- The exception when the Pattern Classes (Writer, Reader) are not properly implemented should be more verbose about what's actually the problem. Furthermore, testing method for correct Pattern Class implementations should be provided within Core.
|
data/Rakefile
ADDED
data/discoverer.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/discoverer/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Xavier Via"]
|
6
|
+
gem.email = ["xavierviacanel@gmail.com"]
|
7
|
+
gem.description = %q{Discoverer functionality with focus in writers and readers}
|
8
|
+
gem.summary = %q{Discoverer functionality with focus in writers and readers}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.add_dependency 'virtus'
|
12
|
+
|
13
|
+
gem.add_development_dependency 'rspec'
|
14
|
+
gem.add_development_dependency 'pry'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($\)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.name = "discoverer"
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
gem.version = Discoverer::VERSION
|
22
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Model
|
2
|
+
=====
|
3
|
+
|
4
|
+
Model is a class to be inherited in every Fetcher Model.
|
5
|
+
|
6
|
+
It automagically:
|
7
|
+
|
8
|
+
* Includes Virtus
|
9
|
+
* Adds the `:_id` attribute
|
10
|
+
* Provides a flexible constructor on top of Virtus
|
11
|
+
* Defines `#from` and `#to` as [Discoverer Methods](http://xaviervia.com.ar/patterns/discoverer-method) for Readers and Writers
|
data/lib/discoverer.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Discoverer
|
2
|
+
def self.has_adapter_for? adapter_constant, the_class
|
3
|
+
return false if the_class.name.nil?
|
4
|
+
namespace = the_class.name.split '::'
|
5
|
+
current_module = adapter_constant
|
6
|
+
until namespace.empty?
|
7
|
+
if RUBY_VERSION =~ /1.8/
|
8
|
+
return false unless current_module.constants.include? namespace.first
|
9
|
+
else
|
10
|
+
return false unless current_module.constants.include? namespace.first.to_sym
|
11
|
+
end
|
12
|
+
current_module = current_module.const_get namespace.shift
|
13
|
+
end
|
14
|
+
return true
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.for adapter_constant, the_class
|
18
|
+
the_class.ancestors.length.times do |time|
|
19
|
+
if has_adapter_for? adapter_constant, the_class.ancestors[time]
|
20
|
+
return eval "::#{adapter_constant}::#{the_class.ancestors[time]}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
raise NotFoundError,
|
25
|
+
"There is no #{adapter_constant.name.downcase} for #{the_class} or any of its ancestors"
|
26
|
+
end
|
27
|
+
|
28
|
+
class NotFoundError < StandardError; end
|
29
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Discoverer
|
2
|
+
# {include:file:docs/Discoverer/Model.md}
|
3
|
+
class Model
|
4
|
+
include Virtus
|
5
|
+
include Reader
|
6
|
+
include Writer
|
7
|
+
|
8
|
+
# @!attribute _id
|
9
|
+
# @return [Object] the id of the object as persisted
|
10
|
+
attribute :_id
|
11
|
+
|
12
|
+
# Accepts an attributes Hash as argument. Loads from the default datasource
|
13
|
+
# unless the Hash has an _id setted.
|
14
|
+
#
|
15
|
+
# @return [Model] a new instance
|
16
|
+
def initialize *args
|
17
|
+
super *args
|
18
|
+
#binding.pry
|
19
|
+
from.default if @_id.nil? and not attributes!.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [Hash] The attributes which are not set to nil
|
23
|
+
def attributes!
|
24
|
+
the_attributes = {}
|
25
|
+
attributes.each do |key, value|
|
26
|
+
the_attributes[key] = value unless value.nil?
|
27
|
+
end
|
28
|
+
return the_attributes
|
29
|
+
end
|
30
|
+
|
31
|
+
# Static methods
|
32
|
+
# @return [Symbol] the name of the class, pluralized, downcased and made into a Symbol
|
33
|
+
def self.table_name
|
34
|
+
"#{self.downcase}s".to_sym
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [String] the name of the clasee, downcased and made into a String
|
38
|
+
def self.downcase
|
39
|
+
"#{self}".split("::").last.downcase
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Discoverer
|
2
|
+
|
3
|
+
# Discoverer Method implementation for Readers
|
4
|
+
# Implements method #from
|
5
|
+
module Reader
|
6
|
+
|
7
|
+
# Discoverer method
|
8
|
+
# @return [Reader] the reader object for this object, initialized
|
9
|
+
def from
|
10
|
+
begin
|
11
|
+
@_reader ||= Discoverer.for( ::Reader, self.class ).new self
|
12
|
+
@_reader
|
13
|
+
rescue Discoverer::NotFoundError => e
|
14
|
+
raise MissingReaderError, "The reader for #{self.class} (Reader::#{self.class}) wasn't found, please create it"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MissingReaderError < StandardError; end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Discoverer
|
2
|
+
module Writer
|
3
|
+
|
4
|
+
def to
|
5
|
+
begin
|
6
|
+
@_writer ||= Discoverer.for(::Writer, self.class).new self
|
7
|
+
@_writer
|
8
|
+
rescue Discoverer::NotFoundError => e
|
9
|
+
raise MissingWriterError, "The writer for #{self.class} (Writer::#{self.class}) wasn't found, please create it"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class MissingWriterError < StandardError; end
|
14
|
+
class EmptySourceError < StandardError; end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Discoverer do
|
4
|
+
describe '.has_adapter_for?' do
|
5
|
+
context 'an adapter exists matching the given constant and class' do
|
6
|
+
it 'should return true' do
|
7
|
+
module SomeAdaptation; class Klass; end; end
|
8
|
+
|
9
|
+
class Klass; end
|
10
|
+
|
11
|
+
Discoverer.has_adapter_for?(SomeAdaptation, Klass).should == true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'no adapter exists for the given constant' do
|
16
|
+
it 'should return false' do
|
17
|
+
module SomeAdaptation; end
|
18
|
+
module Ja; class Kle; end; end
|
19
|
+
|
20
|
+
Discoverer.has_adapter_for?(SomeAdaptation, Ja::Kle).should == false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'the constant does not exist' do
|
25
|
+
it 'should raise a relevant error' do
|
26
|
+
expect { Discoverer.has_adapter_for? DoesNotExist, Object
|
27
|
+
}.to raise_error NameError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'the class has not name' do
|
32
|
+
it 'should return false' do
|
33
|
+
module SomeAdaptation; end
|
34
|
+
|
35
|
+
not_name = stub 'not name'
|
36
|
+
not_name.should_receive(:name).and_return nil
|
37
|
+
Discoverer.has_adapter_for?(SomeAdaptation, not_name).should == false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '.for' do
|
43
|
+
context 'this class has an adapter for the constant' do
|
44
|
+
before do
|
45
|
+
module Some; class End; end; end
|
46
|
+
class End; end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should return the adapter class' do
|
50
|
+
Discoverer.for( Some, End ).should == Some::End
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'the superclass has a adapter_for' do
|
55
|
+
before do
|
56
|
+
module Some1; class Super1; end; end
|
57
|
+
|
58
|
+
class Super1; end
|
59
|
+
class End1 < Super1; end
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should return the superclass adapter class' do
|
63
|
+
Discoverer.for( Some1, End1 ).should == Some1::Super1
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'an implemented module has a adapter_for' do
|
68
|
+
before do
|
69
|
+
module Some2; class Module1; end; end
|
70
|
+
module Module1; end
|
71
|
+
|
72
|
+
class End2; include Module1; end
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should return the module adapter class' do
|
76
|
+
Discoverer.for( Some2, End2 ).should == Some2::Module1
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'the superclass has a adapter_for and its superclass has one too' do
|
81
|
+
before do
|
82
|
+
module Some3; class Super2; end; end
|
83
|
+
module Some3; class Super3; end; end
|
84
|
+
|
85
|
+
class Super2; end
|
86
|
+
class Super3 < Super2; end
|
87
|
+
class End3 < Super3; end
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should return the superclass\'s adapter' do
|
91
|
+
Discoverer.for( Some3, End3 ).should == Some3::Super3
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# I keep it here for a memento, and a good laught:
|
96
|
+
# Remember, remember the errors and bad practices that stem from going
|
97
|
+
# halfway in abstraction of implementation
|
98
|
+
context 'some ancestor does not have an adapter' do
|
99
|
+
before do
|
100
|
+
module Some5; class Super5; end; end
|
101
|
+
class Super5; end
|
102
|
+
module Module3; end
|
103
|
+
class End5 < Super5; include Module3; end
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should skip over it' do
|
107
|
+
Discoverer.for( Some5, End5 ).should == Some5::Super5
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'there is no adapter for the class in the hierarchy' do
|
112
|
+
before do
|
113
|
+
module Some4; end
|
114
|
+
class End4; end
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should return an exception' do
|
118
|
+
expect { Discoverer.for Some4, End4
|
119
|
+
}.to raise_error Discoverer::NotFoundError,
|
120
|
+
"There is no some4 for End4 or any of its ancestors"
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Discoverer::Model do
|
4
|
+
it 'should include Discoverer::Reader' do
|
5
|
+
Discoverer::Model.ancestors.should include Discoverer::Reader
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should include Discoverer::Writer' do
|
9
|
+
Discoverer::Model.ancestors.should include Discoverer::Writer
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should include Virtus' do
|
13
|
+
Discoverer::Model.ancestors.should include Virtus
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should include the attribute _id' do
|
17
|
+
flag = false
|
18
|
+
Discoverer::Model.attribute_set.each do |attribute|
|
19
|
+
flag = true if attribute.name == :_id
|
20
|
+
end
|
21
|
+
flag.should === true
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '.table_name' do
|
25
|
+
it 'should return the class name, downcased, pluralized and made into a Symbol' do
|
26
|
+
Discoverer::Model.table_name.should == :models
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '.downcase' do
|
31
|
+
it 'should return the class name downcased and made into a String' do
|
32
|
+
Discoverer::Model.downcase.should == 'model'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#attributes!' do
|
37
|
+
context 'we have another attribute' do
|
38
|
+
before do
|
39
|
+
module Discoverer
|
40
|
+
class Model
|
41
|
+
attribute :my_attr
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'no attribute is set' do
|
47
|
+
it 'should return an empty hash' do
|
48
|
+
model = Discoverer::Model.new {}
|
49
|
+
model.attributes!.should == {}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'only one attribute is set' do
|
54
|
+
context 'my_attr was set' do
|
55
|
+
it 'should return the attribute that was set' do
|
56
|
+
Discoverer::Model.any_instance.should_receive(:from).and_return stub(:default => nil)
|
57
|
+
model = Discoverer::Model.new :my_attr => "value"
|
58
|
+
model.attributes!.should == { :my_attr => "value" }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context '_id was set' do
|
63
|
+
it 'should return the attribute that was set' do
|
64
|
+
model = Discoverer::Model.new :_id => "value"
|
65
|
+
model.attributes!.should == { :_id => "value" }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'both attributes are set' do
|
71
|
+
it 'should return both attributes' do
|
72
|
+
model = Discoverer::Model.new :my_attr => 'value', :_id => "123456"
|
73
|
+
model.attributes!.should == { :_id => '123456', :my_attr => 'value' }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#initialize' do
|
80
|
+
before do
|
81
|
+
module Discoverer
|
82
|
+
class Model
|
83
|
+
attribute :my_attr
|
84
|
+
attribute :other_attr
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'we have an extra attribute' do
|
90
|
+
context 'no argument is passed' do
|
91
|
+
it 'should return a new instance with no attributes set' do
|
92
|
+
mod = Discoverer::Model.new
|
93
|
+
mod.attributes!.should be_empty
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should not call the reader #from' do
|
97
|
+
Discoverer::Model.any_instance.should_not_receive :from
|
98
|
+
Discoverer::Model.new
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'a Hash with _id key is passed' do
|
103
|
+
it 'should return a new instance with the attributes set' do
|
104
|
+
mod = Discoverer::Model.new :_id => "lala", :my_attr => "2354"
|
105
|
+
mod._id.should == "lala"
|
106
|
+
mod.my_attr.should == "2354"
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should not call the reader #from' do
|
110
|
+
Discoverer::Model.any_instance.should_not_receive :from
|
111
|
+
Discoverer::Model.new :_id => "lala", :my_attr => "2354"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'a Hash without _id key is passed' do
|
116
|
+
it 'should return a new instance with the attributes set' do
|
117
|
+
reader = stub 'reader'
|
118
|
+
Discoverer::Model.any_instance.should_receive(:from).and_return reader
|
119
|
+
reader.should_receive :default
|
120
|
+
mod = Discoverer::Model.new :my_attr => "345345"
|
121
|
+
mod.my_attr.should == "345345"
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should call the reader #from and send the method #default' do
|
125
|
+
reader = stub 'reader'
|
126
|
+
Discoverer::Model.any_instance.should_receive(:from).and_return reader
|
127
|
+
reader.should_receive :default
|
128
|
+
Discoverer::Model.new :my_attr => "234"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Discoverer::Reader do
|
5
|
+
describe "#from" do
|
6
|
+
context "para la clase Klass" do
|
7
|
+
it "should instance the class Reader::Klass" do
|
8
|
+
module Reader
|
9
|
+
class Klass
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
Reader::Klass.should_receive :new
|
14
|
+
|
15
|
+
class Klass
|
16
|
+
include Discoverer::Reader
|
17
|
+
end
|
18
|
+
|
19
|
+
obj = Klass.new
|
20
|
+
obj.from
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should pass the currect object as argument" do
|
24
|
+
module Reader
|
25
|
+
class Klass
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class ::Klass
|
30
|
+
include Discoverer::Reader
|
31
|
+
end
|
32
|
+
|
33
|
+
obj = ::Klass.new
|
34
|
+
|
35
|
+
Reader::Klass.should_receive( :new ).with obj
|
36
|
+
|
37
|
+
obj.from
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return the same reader if its called twice" do
|
41
|
+
module Reader
|
42
|
+
class Klass
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class ::Klass
|
47
|
+
include Discoverer::Reader
|
48
|
+
end
|
49
|
+
|
50
|
+
obj = ::Klass.new
|
51
|
+
|
52
|
+
reader = stub 'reader'
|
53
|
+
Reader::Klass.should_receive( :new ).with( obj ).and_return reader
|
54
|
+
|
55
|
+
obj.from.should === reader
|
56
|
+
obj.from.should === reader
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "for a subclass" do
|
61
|
+
it "should simply work, provided the pattern is right" do
|
62
|
+
module Reader
|
63
|
+
class UserR
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class Model
|
68
|
+
include Discoverer::Reader
|
69
|
+
end
|
70
|
+
|
71
|
+
class UserR < Model
|
72
|
+
end
|
73
|
+
obj = UserR.new
|
74
|
+
Reader::UserR.should_receive( :new ).with obj
|
75
|
+
obj.from
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "there is a reader" do
|
80
|
+
it 'should fail with a descriptive error' do
|
81
|
+
class Ponele
|
82
|
+
include Discoverer::Reader
|
83
|
+
end
|
84
|
+
|
85
|
+
obj = Ponele.new
|
86
|
+
expect { obj.from
|
87
|
+
}.to raise_error Discoverer::Reader::MissingReaderError,
|
88
|
+
"The reader for Ponele (Reader::Ponele) wasn't found, please create it"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Discoverer::Writer do
|
5
|
+
describe "#to" do
|
6
|
+
before do
|
7
|
+
module Writer
|
8
|
+
class TestClass
|
9
|
+
end
|
10
|
+
end
|
11
|
+
class TestClass
|
12
|
+
include Discoverer::Writer
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when the method is called" do
|
17
|
+
it "should try to instance the class reader with self(actual class that call the method)" do
|
18
|
+
|
19
|
+
Writer::TestClass.should_receive :new
|
20
|
+
|
21
|
+
obj = TestClass.new
|
22
|
+
obj.to
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should pass as argument self" do
|
26
|
+
obj = TestClass.new
|
27
|
+
Writer::TestClass.should_receive(:new).with(obj)
|
28
|
+
obj.to
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should retrive the same writer if it's called twice" do
|
32
|
+
writer = stub "writer"
|
33
|
+
aux = TestClass.new
|
34
|
+
|
35
|
+
Writer::TestClass.should_receive(:new).with(aux).and_return(writer)
|
36
|
+
|
37
|
+
aux.to.should eq writer
|
38
|
+
aux.to.should eq writer
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "for a subclass" do
|
44
|
+
it "should work at once, provided the implementation of the pattern is done" do
|
45
|
+
module Writer
|
46
|
+
class UserW
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class UserW < TestClass
|
51
|
+
end
|
52
|
+
|
53
|
+
obj = UserW.new
|
54
|
+
Writer::UserW.should_receive( :new ).with obj
|
55
|
+
obj.to
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
context "there is no writer" do
|
61
|
+
it "should fail with a friendly error" do
|
62
|
+
class Fail
|
63
|
+
include Discoverer::Writer
|
64
|
+
end
|
65
|
+
|
66
|
+
obj = Fail.new
|
67
|
+
expect {
|
68
|
+
obj.to
|
69
|
+
}.to raise_error Discoverer::Writer::MissingWriterError,
|
70
|
+
"The writer for Fail (Writer::Fail) wasn't found, please create it"
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: discoverer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Xavier Via
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: virtus
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Discoverer functionality with focus in writers and readers
|
63
|
+
email:
|
64
|
+
- xavierviacanel@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- discoverer.gemspec
|
75
|
+
- docs/Discoverer/Model.md
|
76
|
+
- lib/discoverer.rb
|
77
|
+
- lib/discoverer/discoverer.rb
|
78
|
+
- lib/discoverer/model.rb
|
79
|
+
- lib/discoverer/reader.rb
|
80
|
+
- lib/discoverer/version.rb
|
81
|
+
- lib/discoverer/writer.rb
|
82
|
+
- spec/discoverer/discoverer_spec.rb
|
83
|
+
- spec/discoverer/model_spec.rb
|
84
|
+
- spec/discoverer/reader_spec.rb
|
85
|
+
- spec/discoverer/writer_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: ''
|
88
|
+
licenses: []
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.8.24
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Discoverer functionality with focus in writers and readers
|
111
|
+
test_files:
|
112
|
+
- spec/discoverer/discoverer_spec.rb
|
113
|
+
- spec/discoverer/model_spec.rb
|
114
|
+
- spec/discoverer/reader_spec.rb
|
115
|
+
- spec/discoverer/writer_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
has_rdoc:
|