yeti 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/yeti/editor.rb CHANGED
@@ -6,32 +6,32 @@ module Yeti
6
6
  attr_reader :context
7
7
  delegate :id, :to_param, :persisted?, to: :edited
8
8
 
9
- def initialize(context, given_id=nil)
10
- @context = context
11
- @given_id = given_id
9
+ def self.from_id(context, id)
10
+ new context, (find_by_id id if id)
12
11
  end
13
12
 
14
- def errors
15
- @errors ||= ::Yeti::Errors.new(
16
- self,
17
- untranslated: self.class.untranslated?
18
- )
13
+ def self.find_by_id(id)
14
+ raise NotImplementedError, "#{inspect}.find_by_id"
19
15
  end
20
16
 
21
- def edited
22
- @edited ||= if given_id
23
- find_by_id given_id
24
- else
25
- new_object
26
- end
17
+ def self.new_object
18
+ raise NotImplementedError, "#{inspect}.new_object"
19
+ end
20
+
21
+ def initialize(context, edited=nil)
22
+ @context = context
23
+ @edited = edited
27
24
  end
28
25
 
29
- def find_by_id(id)
30
- raise NotImplementedError, "#{self.class}#find_by_id"
26
+ def edited
27
+ @edited ||= self.class.new_object
31
28
  end
32
29
 
33
- def new_object
34
- raise NotImplementedError, "#{self.class}#new_object"
30
+ def errors
31
+ @errors ||= ::Yeti::Errors.new(
32
+ self,
33
+ untranslated: self.class.untranslated?
34
+ )
35
35
  end
36
36
 
37
37
  def update_attributes(attrs)
@@ -79,8 +79,6 @@ module Yeti
79
79
 
80
80
  private
81
81
 
82
- attr_reader :given_id
83
-
84
82
  def self.attribute(name, opts={})
85
83
  opts[:attribute_name] = name
86
84
  opts[:from] = :edited unless opts.has_key? :from
data/lib/yeti/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Yeti
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,21 @@
1
+ module Yeti
2
+ class Viewer
3
+
4
+ attr_reader :context, :decorated
5
+ delegate :id, :to_param, :persisted?, to: :decorated
6
+
7
+ def self.from_id(context, id)
8
+ new context, (find_by_id id if id)
9
+ end
10
+
11
+ def self.find_by_id(id)
12
+ raise NotImplementedError, "#{inspect}.find_by_id"
13
+ end
14
+
15
+ def initialize(context, decorated)
16
+ @context = context
17
+ @decorated = decorated
18
+ end
19
+
20
+ end
21
+ end
data/lib/yeti.rb CHANGED
@@ -4,6 +4,7 @@ require "active_model"
4
4
  require "string_cleaner"
5
5
  require "yeti/context"
6
6
  require "yeti/errors"
7
+ require "yeti/viewer"
7
8
  require "yeti/editor"
8
9
  require "yeti/search"
9
10
  require "yeti/version"
@@ -1,61 +1,95 @@
1
1
  require "spec_helper"
2
2
 
3
- describe Yeti::Editor do
3
+ describe ::Yeti::Editor do
4
4
  let(:context){ mock :context }
5
- subject{ Yeti::Editor.new context }
6
- it "keeps given context" do
7
- subject.context.should be context
5
+ subject{ described_class.new context }
6
+ it ".new_object is virtual" do
7
+ lambda do
8
+ described_class.new_object
9
+ end.should raise_error NotImplementedError, "Yeti::Editor.new_object"
8
10
  end
9
- it "#find_by_id is virtual" do
10
- lambda{ subject.find_by_id 1 }.should raise_error NotImplementedError
11
+ it ".find_by_id is virtual" do
12
+ lambda do
13
+ described_class.find_by_id 1
14
+ end.should raise_error NotImplementedError, "Yeti::Editor.find_by_id"
11
15
  end
12
- it "#new_object is virtual" do
13
- lambda{ subject.new_object }.should raise_error NotImplementedError
14
- end
15
- it "#persist! is virtual" do
16
- lambda{ subject.persist! }.should raise_error NotImplementedError
17
- end
18
- context "with a given id" do
19
- let(:edited){ mock :edited }
20
- subject{ Yeti::Editor.new context, 1 }
21
- before{ subject.stub(:find_by_id).with(1).and_return edited }
22
- it "uses #find_by_id to find the main object being edited" do
23
- subject.edited.should be edited
24
- end
25
- it "delegates persisted? to edited" do
26
- edited.stub(:persisted?).and_return(expected = mock)
27
- subject.persisted?.should be expected
28
- end
29
- end
30
- context "with id nil" do
31
- let(:edited){ mock :edited }
32
- subject{ Yeti::Editor.new context, nil }
33
- before{ subject.stub(:new_object).and_return edited }
34
- it "uses #new_object to initialize main object being edited" do
35
- subject.edited.should be edited
16
+ describe "initialization" do
17
+ let(:new_object){ mock :new_object }
18
+ let(:existing_object){ mock :existing_object }
19
+ context "with context only" do
20
+ before{ described_class.stub(:new_object).and_return new_object }
21
+ it "keeps given context" do
22
+ subject.context.should be context
23
+ end
24
+ it "#persist! is virtual" do
25
+ lambda do
26
+ subject.persist!
27
+ end.should raise_error NotImplementedError, "Yeti::Editor#persist!"
28
+ end
29
+ it "uses .new_object to initialize edited object" do
30
+ subject.edited.should == new_object
31
+ end
32
+ it "delegates id to edited object" do
33
+ should delegates(:id).to :new_object
34
+ end
35
+ it "delegates to_param to edited object" do
36
+ should delegates(:to_param).to :new_object
37
+ end
38
+ it "delegates persisted? edited object" do
39
+ should delegates(:persisted?).to :new_object
40
+ end
36
41
  end
37
- it "delegates persisted? to edited" do
38
- edited.stub(:persisted?).and_return(expected = mock)
39
- subject.persisted?.should be expected
42
+ context "initialize with context and object to edit" do
43
+ subject{ described_class.new context, existing_object }
44
+ it "keeps given context" do
45
+ subject.context.should be context
46
+ end
47
+ it "#persist! is virtual" do
48
+ lambda do
49
+ subject.persist!
50
+ end.should raise_error NotImplementedError, "Yeti::Editor#persist!"
51
+ end
52
+ it "delegates id to edited object" do
53
+ should delegates(:id).to :existing_object
54
+ end
55
+ it "delegates to_param to edited object" do
56
+ should delegates(:to_param).to :existing_object
57
+ end
58
+ it "delegates persisted? edited object" do
59
+ should delegates(:persisted?).to :existing_object
60
+ end
40
61
  end
41
62
  end
42
- context "without id" do
43
- let(:edited){ mock :edited }
44
- before{ subject.stub(:new_object).and_return edited }
45
- it "uses #new_object to initialize main object being edited" do
46
- subject.edited.should be edited
63
+ describe ".from_id(context, given_id)" do
64
+ let(:new_object){ mock :new_object }
65
+ let(:existing_object){ mock :existing_object, id: 1 }
66
+ subject{ described_class.from_id context, given_id }
67
+ context "when given_id is nil" do
68
+ let(:given_id){ nil }
69
+ it "uses .new_object to generate object to edit" do
70
+ described_class.should_receive(:new_object).and_return new_object
71
+ subject.edited.should be new_object
72
+ end
47
73
  end
48
- it "delegates persisted? to edited" do
49
- edited.stub(:persisted?).and_return(expected = mock)
50
- subject.persisted?.should be expected
74
+ context "when given_id is not nil" do
75
+ let(:given_id){ "1" }
76
+ it "uses .find_by_id to find object to edit" do
77
+ described_class.should_receive(:find_by_id).with("1").and_return do
78
+ existing_object
79
+ end
80
+ subject.edited.should be existing_object
81
+ subject.id.should be 1
82
+ end
51
83
  end
52
84
  end
53
85
  context "when not valid" do
54
- before{ subject.stub(:valid?).and_return false }
55
86
  it "#save returns false" do
87
+ subject.should_receive(:valid?).and_return false
88
+ subject.should_not_receive :persist!
56
89
  subject.save.should be false
57
90
  end
58
91
  it "#save(validate: false) calls persist! then returns true" do
92
+ subject.should_not_receive :valid?
59
93
  subject.should_receive :persist!
60
94
  subject.save(validate: false).should be true
61
95
  end
@@ -67,19 +101,19 @@ describe Yeti::Editor do
67
101
  end
68
102
  end
69
103
  context "editor of one record" do
70
- let :editor_class do
71
- Class.new Yeti::Editor do
104
+ let :described_class do
105
+ Class.new ::Yeti::Editor do
72
106
  attribute :name
73
107
  validates_presence_of :name
74
108
  def self.name
75
109
  "ObjectEditor"
76
110
  end
111
+ def self.new_object
112
+ Struct.new(:id, :name).new nil, nil
113
+ end
77
114
  end
78
115
  end
79
116
  context "new record" do
80
- subject{ editor_class.new context, nil }
81
- let(:new_record){ mock :new_record, name: nil, id: nil }
82
- before{ subject.stub(:new_object).and_return new_record }
83
117
  its(:id){ should be_nil }
84
118
  its(:name){ should be_nil }
85
119
  it "#name= converts input to string" do
@@ -120,7 +154,7 @@ describe Yeti::Editor do
120
154
  subject.should_not be_without_error
121
155
  end
122
156
  it "can return untranslated error messages" do
123
- editor_class.class_eval do
157
+ described_class.class_eval do
124
158
  dont_translate_error_messages
125
159
  end
126
160
  subject.valid?
@@ -158,9 +192,8 @@ describe Yeti::Editor do
158
192
  end
159
193
  end
160
194
  context "existing record" do
161
- subject{ editor_class.new context, 1 }
162
- let(:existing_record){ mock :existing_record, name: "Anthony", id: 1 }
163
- before{ subject.stub(:find_by_id).with(1).and_return existing_record }
195
+ let(:existing_object){ mock :existing_object, id: 1, name: "Anthony" }
196
+ subject{ described_class.new context, existing_object }
164
197
  it("gets id from record"){ subject.id.should be 1 }
165
198
  it "gets name from record" do
166
199
  subject.name.should == "Anthony"
@@ -193,26 +226,23 @@ describe Yeti::Editor do
193
226
  end
194
227
  end
195
228
  context "editor of multiple records" do
196
- let :editor_class do
197
- Class.new Yeti::Editor do
229
+ let(:existing_object){ Struct.new(:id, :name).new 1, "Anthony" }
230
+ subject do
231
+ Class.new described_class do
198
232
  attribute :name
199
233
  attribute :description, from: :related
200
234
  attribute :password, from: nil
201
235
  attribute :timestamp, from: ".timestamp_str"
202
236
  attribute :related_id, from: "related.id"
203
237
  attribute :invalid
204
- def find_by_id(id)
205
- Struct.new(:id, :name).new(id, "Anthony")
206
- end
207
238
  def related
208
239
  Struct.new(:id, :description).new 2, "Business man"
209
240
  end
210
241
  def timestamp_str
211
242
  "2001-01-01"
212
243
  end
213
- end
244
+ end.new context, existing_object
214
245
  end
215
- subject{ editor_class.new context, 1 }
216
246
  it "attribute default value comes from edited" do
217
247
  subject.id.should == 1
218
248
  subject.name.should == "Anthony"
@@ -234,14 +264,13 @@ describe Yeti::Editor do
234
264
  end
235
265
  end
236
266
  describe "#mandatory?" do
237
- let :editor_class do
238
- Class.new Yeti::Editor do
267
+ subject do
268
+ Class.new described_class do
239
269
  validates_presence_of :name
240
270
  attribute :name
241
271
  attribute :password
242
- end
272
+ end.new context
243
273
  end
244
- subject{ editor_class.new context }
245
274
  it "is true for an attribute with validates_presence_of" do
246
275
  subject.mandatory?(:name).should be true
247
276
  end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe ::Yeti::Viewer do
4
+ let(:context){ mock :context }
5
+ subject{ described_class.new context }
6
+ it ".find_by_id is virtual" do
7
+ lambda do
8
+ described_class.find_by_id 1
9
+ end.should raise_error NotImplementedError, "Yeti::Viewer.find_by_id"
10
+ end
11
+ describe "initialization" do
12
+ let(:existing_object){ mock :existing_object }
13
+ subject{ described_class.new context, existing_object }
14
+ it "keeps given context" do
15
+ subject.context.should be context
16
+ end
17
+ it "keeps given object to decorate" do
18
+ subject.decorated.should be existing_object
19
+ end
20
+ it "delegates id to decorated object" do
21
+ should delegates(:id).to :existing_object
22
+ end
23
+ it "delegates to_param to decorated object" do
24
+ should delegates(:to_param).to :existing_object
25
+ end
26
+ it "delegates persisted? decorated object" do
27
+ should delegates(:persisted?).to :existing_object
28
+ end
29
+ end
30
+ describe ".from_id(context, given_id)" do
31
+ let(:existing_object){ mock :existing_object }
32
+ subject{ described_class.from_id context, "1" }
33
+ it "uses .find_by_id to find object to edit" do
34
+ described_class.should_receive(:find_by_id).with("1").and_return do
35
+ existing_object
36
+ end
37
+ subject.decorated.should be existing_object
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yeti
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-13 00:00:00.000000000 Z
12
+ date: 2012-12-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -93,11 +93,13 @@ files:
93
93
  - lib/yeti/errors.rb
94
94
  - lib/yeti/search.rb
95
95
  - lib/yeti/version.rb
96
+ - lib/yeti/viewer.rb
96
97
  - spec/spec_helper.rb
97
98
  - spec/support/matchers.rb
98
99
  - spec/yeti/context_spec.rb
99
100
  - spec/yeti/editor_spec.rb
100
101
  - spec/yeti/search_spec.rb
102
+ - spec/yeti/viewer_spec.rb
101
103
  - spec/yeti_spec.rb
102
104
  - yeti.gemspec
103
105
  homepage: ''
@@ -112,21 +114,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
114
  - - ! '>='
113
115
  - !ruby/object:Gem::Version
114
116
  version: '0'
115
- segments:
116
- - 0
117
- hash: 3787282194085503627
118
117
  required_rubygems_version: !ruby/object:Gem::Requirement
119
118
  none: false
120
119
  requirements:
121
120
  - - ! '>='
122
121
  - !ruby/object:Gem::Version
123
122
  version: '0'
124
- segments:
125
- - 0
126
- hash: 3787282194085503627
127
123
  requirements: []
128
124
  rubyforge_project:
129
- rubygems_version: 1.8.23
125
+ rubygems_version: 1.8.24
130
126
  signing_key:
131
127
  specification_version: 3
132
128
  summary: Editor pattern simplifies edition of multiple objects at once using ActiveModel
@@ -136,4 +132,5 @@ test_files:
136
132
  - spec/yeti/context_spec.rb
137
133
  - spec/yeti/editor_spec.rb
138
134
  - spec/yeti/search_spec.rb
135
+ - spec/yeti/viewer_spec.rb
139
136
  - spec/yeti_spec.rb