yeti 0.3.9 → 0.3.11
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/yeti/editor.rb +33 -5
- data/lib/yeti/version.rb +1 -1
- data/spec/yeti/editor_spec.rb +47 -0
- metadata +4 -4
data/lib/yeti/editor.rb
CHANGED
@@ -87,6 +87,16 @@ module Yeti
|
|
87
87
|
id.hash
|
88
88
|
end
|
89
89
|
|
90
|
+
def attributes_for_persist
|
91
|
+
attributes = {}
|
92
|
+
self.class.attributes.each do |key|
|
93
|
+
value = send key
|
94
|
+
opts = self.class.attribute_options[key]
|
95
|
+
attributes[key] = format_input_for_persist value, opts
|
96
|
+
end
|
97
|
+
attributes
|
98
|
+
end
|
99
|
+
|
90
100
|
protected
|
91
101
|
|
92
102
|
# ~~~ methods to be implemented in subclasses ~~~
|
@@ -105,11 +115,15 @@ module Yeti
|
|
105
115
|
opts[:from] = :edited unless opts.has_key? :from
|
106
116
|
attribute_options[name.to_sym] = opts
|
107
117
|
define_attribute_methods attributes
|
108
|
-
from =
|
109
|
-
when ""
|
110
|
-
|
111
|
-
when
|
112
|
-
|
118
|
+
from = case opts[:from].to_s
|
119
|
+
when ""
|
120
|
+
"nil"
|
121
|
+
when /^\.(.*)/
|
122
|
+
$1
|
123
|
+
when /\./
|
124
|
+
opts[:from]
|
125
|
+
else
|
126
|
+
"#{opts[:from]}.#{name}"
|
113
127
|
end
|
114
128
|
class_eval """
|
115
129
|
def #{name}
|
@@ -152,6 +166,20 @@ module Yeti
|
|
152
166
|
value
|
153
167
|
end
|
154
168
|
|
169
|
+
def format_input_for_persist(value, attribute_opts)
|
170
|
+
case attribute_opts[:as]
|
171
|
+
when :date
|
172
|
+
case value
|
173
|
+
when Date
|
174
|
+
value
|
175
|
+
else
|
176
|
+
Date.parse value
|
177
|
+
end
|
178
|
+
else
|
179
|
+
value
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
155
183
|
def format_output(value, attribute_opts)
|
156
184
|
value
|
157
185
|
end
|
data/lib/yeti/version.rb
CHANGED
data/spec/yeti/editor_spec.rb
CHANGED
@@ -242,12 +242,17 @@ describe ::Yeti::Editor do
|
|
242
242
|
attribute :timestamp, from: ".timestamp_str"
|
243
243
|
attribute :related_id, from: "related.id"
|
244
244
|
attribute :invalid
|
245
|
+
|
245
246
|
def related
|
246
247
|
Struct.new(:id, :description).new 2, "Business man"
|
247
248
|
end
|
249
|
+
|
250
|
+
private
|
251
|
+
|
248
252
|
def timestamp_str
|
249
253
|
"2001-01-01"
|
250
254
|
end
|
255
|
+
|
251
256
|
end.new context, existing_object
|
252
257
|
end
|
253
258
|
it "attribute default value comes from edited" do
|
@@ -325,4 +330,46 @@ describe ::Yeti::Editor do
|
|
325
330
|
subject.hash.should == other.hash
|
326
331
|
end
|
327
332
|
end
|
333
|
+
describe "#attributes_for_persist" do
|
334
|
+
subject{ described_class.new context }
|
335
|
+
before{ described_class.stub(:new_object).with(context).and_return record }
|
336
|
+
context "parses date format" do
|
337
|
+
let :described_class do
|
338
|
+
Class.new ::Yeti::Editor do
|
339
|
+
attribute :valid_from, as: :date
|
340
|
+
end
|
341
|
+
end
|
342
|
+
let(:record){ mock :new_record, valid_from: Date.parse("2002-09-01") }
|
343
|
+
it "when a new value is assigned" do
|
344
|
+
subject.valid_from = "2002-12-31"
|
345
|
+
subject.attributes.should == {valid_from: "2002-12-31"}
|
346
|
+
subject.attributes_for_persist.should == {
|
347
|
+
valid_from: Date.parse("2002-12-31")
|
348
|
+
}
|
349
|
+
end
|
350
|
+
it "without new value" do
|
351
|
+
subject.attributes.should == {valid_from: Date.parse("2002-09-01")}
|
352
|
+
subject.attributes_for_persist.should == {
|
353
|
+
valid_from: Date.parse("2002-09-01")
|
354
|
+
}
|
355
|
+
end
|
356
|
+
end
|
357
|
+
context "formatting can be changed with #format_input_for_persist" do
|
358
|
+
let :described_class do
|
359
|
+
Class.new ::Yeti::Editor do
|
360
|
+
attribute :name
|
361
|
+
end
|
362
|
+
end
|
363
|
+
let(:record){ mock :new_record, name: nil }
|
364
|
+
it "uses format_input_for_persist on each value" do
|
365
|
+
subject.name = "Tony"
|
366
|
+
subject.should_receive(:format_input_for_persist).with(
|
367
|
+
"Tony",
|
368
|
+
attribute_name: :name,
|
369
|
+
from: :edited
|
370
|
+
).and_return "Anthony"
|
371
|
+
subject.attributes_for_persist.should == {name: "Anthony"}
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
328
375
|
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.3.
|
4
|
+
version: 0.3.11
|
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: 2013-
|
12
|
+
date: 2013-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
116
|
version: '0'
|
117
117
|
segments:
|
118
118
|
- 0
|
119
|
-
hash:
|
119
|
+
hash: -2480111560270293046
|
120
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
121
|
none: false
|
122
122
|
requirements:
|
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
125
|
version: '0'
|
126
126
|
segments:
|
127
127
|
- 0
|
128
|
-
hash:
|
128
|
+
hash: -2480111560270293046
|
129
129
|
requirements: []
|
130
130
|
rubyforge_project:
|
131
131
|
rubygems_version: 1.8.23
|