edtf-rails 0.2.1 → 0.2.2
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/VERSION +1 -1
- data/edtf-rails.gemspec +3 -2
- data/lib/edtf-rails/edtf-rails.rb +33 -81
- data/lib/edtf-rails/utils.rb +10 -0
- data/lib/edtf-rails.rb +2 -1
- data/spec/edtf-rails_spec.rb +47 -43
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/edtf-rails.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "edtf-rails"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["masciugo"]
|
12
|
-
s.date = "2013-11-
|
12
|
+
s.date = "2013-11-15"
|
13
13
|
s.description = "An ActiveRecord extension to deal with Extended DateTime Format attributes"
|
14
14
|
s.email = "masciugo@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/edtf-rails.rb",
|
31
31
|
"lib/edtf-rails/edtf-rails.rb",
|
32
32
|
"lib/edtf-rails/getters_and_setters.rb",
|
33
|
+
"lib/edtf-rails/utils.rb",
|
33
34
|
"local_history",
|
34
35
|
"spec/edtf-rails_spec.rb",
|
35
36
|
"spec/spec_helper.rb",
|
@@ -2,23 +2,6 @@ module EdtfRails
|
|
2
2
|
|
3
3
|
EDTF_STRING_FORMAT = /^(\d{4})$|^(\d{4}-(0[1-9]|1[012]))$|^(\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))$/
|
4
4
|
|
5
|
-
def check_options(options, admitted_keys)
|
6
|
-
|
7
|
-
raise WrongArgumentException, "first argument must be in a hash." unless options.is_a? Hash
|
8
|
-
raise WrongArgumentException, "seconf argument must be in an array." unless admitted_keys.is_a? Array
|
9
|
-
|
10
|
-
options.each do |key, value|
|
11
|
-
unless admitted_keys.include? key
|
12
|
-
raise WrongOptionException.new("Unknown option: #{key.inspect} => #{value.inspect}.")
|
13
|
-
end
|
14
|
-
if block_given?
|
15
|
-
yield key, value
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
5
|
def edtf options = {}
|
23
6
|
|
24
7
|
admitted_keys = [:attributes]
|
@@ -48,89 +31,58 @@ module EdtfRails
|
|
48
31
|
# setters
|
49
32
|
define_method("#{d}=") do |edtf_date|
|
50
33
|
edtf_date = edtf_date.edtf if edtf_date.is_a? Date
|
51
|
-
|
52
|
-
end
|
34
|
+
transaction do
|
53
35
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
after_initialize do
|
59
|
-
edtf_attributes.each do |d|
|
60
|
-
if read_attribute(d)
|
36
|
+
write_attribute(d,edtf_date)
|
37
|
+
|
38
|
+
# virtual attributes settings after any assignment (including initialization)
|
39
|
+
date_array = read_attribute(d).to_s.split('-')
|
61
40
|
[:y, :m, :d].each_with_index do |x,i|
|
62
|
-
send("#{d}_#{x}=",
|
41
|
+
send("#{d}_#{x}=",date_array[i])
|
63
42
|
end
|
43
|
+
|
64
44
|
end
|
65
45
|
end
|
46
|
+
|
66
47
|
end
|
67
48
|
|
68
49
|
# virtual attributes utilization to set date before validation (they are used only if at least one of them is defined)
|
69
50
|
before_validation do
|
70
51
|
edtf_attributes.each do |d|
|
71
52
|
date_array = [:y, :m, :d].map{|x| send("#{d}_#{x}")}
|
72
|
-
if (res = date_array.shift).present? #they are used only if at least one of them is defined
|
53
|
+
self[d]= if (res = date_array.shift).present? #they are used only if at least one of them is defined
|
73
54
|
date_array.each{|x| x.blank? ? break : (res += "-#{x}") }
|
74
|
-
|
55
|
+
res
|
56
|
+
else
|
57
|
+
nil
|
75
58
|
end
|
76
59
|
end
|
77
60
|
end
|
78
61
|
|
79
62
|
|
80
63
|
# Include instance methods and class methods
|
81
|
-
include EdtfRails::GettersAndSetters
|
64
|
+
# include EdtfRails::GettersAndSetters
|
65
|
+
# include EdtfRails::Utils
|
82
66
|
|
83
67
|
end
|
84
68
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
# class_attribute :sex_column, :sex_values, :sex_male_value, :sex_female_value
|
105
|
-
# self.sex_column = options[:sex_column] || :sex
|
106
|
-
# self.sex_values = (options[:sex_values] and options[:sex_values].to_a.map(&:to_s)) || ['M','F']
|
107
|
-
# self.sex_male_value = self.sex_values.first
|
108
|
-
# self.sex_female_value = self.sex_values.last
|
109
|
-
# # instance attribute
|
110
|
-
# alias_attribute :sex, sex_column if self.sex_column != :sex
|
111
|
-
# # validation
|
112
|
-
# validates_presence_of sex_column
|
113
|
-
# validates_format_of sex_column, :with => /[#{sex_values.join}]/
|
114
|
-
|
115
|
-
# ## relatives associations
|
116
|
-
# tracked_relatives.each do |key|
|
117
|
-
# # class attribute where is stored the correspondig foreign_key column name
|
118
|
-
# class_attribute_name = "#{key}_column"
|
119
|
-
# foreign_key = "#{key}_id"
|
120
|
-
# class_attribute class_attribute_name
|
121
|
-
# self.send("#{class_attribute_name}=", options[class_attribute_name.to_sym] || foreign_key)
|
122
|
-
|
123
|
-
# # self join association
|
124
|
-
# attr_accessible foreign_key
|
125
|
-
# belongs_to key, class_name: self, foreign_key: foreign_key
|
126
|
-
|
127
|
-
# end
|
128
|
-
|
129
|
-
# # Include instance methods and class methods
|
130
|
-
# include Genealogy::QueryMethods
|
131
|
-
# include Genealogy::AlterMethods
|
132
|
-
# include Genealogy::SpouseMethods if current_spouse_enabled
|
133
|
-
|
134
|
-
# end
|
135
|
-
|
69
|
+
private
|
70
|
+
|
71
|
+
def check_options(options, admitted_keys)
|
72
|
+
|
73
|
+
raise WrongArgumentException, "first argument must be in a hash." unless options.is_a? Hash
|
74
|
+
raise WrongArgumentException, "seconf argument must be in an array." unless admitted_keys.is_a? Array
|
75
|
+
|
76
|
+
options.each do |key, value|
|
77
|
+
unless admitted_keys.include? key
|
78
|
+
raise WrongOptionException.new("Unknown option: #{key.inspect} => #{value.inspect}.")
|
79
|
+
end
|
80
|
+
if block_given?
|
81
|
+
yield key, value
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
136
88
|
end
|
data/lib/edtf-rails.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require "edtf"
|
2
2
|
# require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/constants')
|
3
3
|
# require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/exceptions')
|
4
|
-
require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/getters_and_setters')
|
4
|
+
# require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/getters_and_setters')
|
5
|
+
# require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/utils')
|
5
6
|
require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/edtf-rails')
|
6
7
|
|
7
8
|
ActiveRecord::Base.send :extend, EdtfRails
|
data/spec/edtf-rails_spec.rb
CHANGED
@@ -83,16 +83,6 @@ describe EdtfRailsTest::Model do
|
|
83
83
|
|
84
84
|
subject { @obj }
|
85
85
|
|
86
|
-
shared_examples "dob getter return a Date object but a string is stored in the db" do
|
87
|
-
describe "#dob" do
|
88
|
-
subject { @obj.dob }
|
89
|
-
specify { should be_a Date }
|
90
|
-
end
|
91
|
-
it "has a string as value in the db" do
|
92
|
-
expect(@obj.read_attribute(:dob)).to be_a String
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
86
|
describe "#new" do
|
97
87
|
context "when initialized with no params" do
|
98
88
|
let(:params) { nil }
|
@@ -100,24 +90,35 @@ describe EdtfRailsTest::Model do
|
|
100
90
|
its(:dob) {should be_nil }
|
101
91
|
its(:dod) {should be_nil }
|
102
92
|
describe "#dob=new_date" do
|
93
|
+
|
103
94
|
before(:each) do
|
104
95
|
@obj.dob = new_date
|
105
96
|
@obj.save!
|
106
97
|
end
|
107
|
-
|
108
|
-
|
98
|
+
|
99
|
+
shared_examples "return a Date object but store a String in the db" do
|
100
|
+
describe "#dob" do
|
101
|
+
subject { @obj.dob }
|
102
|
+
specify { should be_a Date }
|
103
|
+
end
|
104
|
+
it "has a string as value in the db" do
|
105
|
+
expect(@obj.read_attribute(:dob)).to be_a String
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "when new_date is Date.new(2001,9,10)" do
|
109
110
|
let(:new_date) { Date.new(2001,9,10) }
|
110
|
-
it_should_behave_like "
|
111
|
+
it_should_behave_like "return a Date object but store a String in the db"
|
111
112
|
its(:dob) {should eql Date.new(2001,9,10) }
|
112
113
|
end
|
113
114
|
context "when new_date is the string '2001-09-10'" do
|
114
115
|
let(:new_date) { '2001-09-10' }
|
115
|
-
it_should_behave_like "
|
116
|
+
it_should_behave_like "return a Date object but store a String in the db"
|
116
117
|
its(:dob) {should eql Date.new(2001,9,10) }
|
117
118
|
end
|
118
119
|
context "when new_date is the string '2001-09'" do
|
119
120
|
let(:new_date) { '2001-09' }
|
120
|
-
it_should_behave_like "
|
121
|
+
it_should_behave_like "return a Date object but store a String in the db"
|
121
122
|
describe "dob precision" do
|
122
123
|
specify { expect(@obj.dob.precision).to be :month }
|
123
124
|
end
|
@@ -161,34 +162,42 @@ describe EdtfRailsTest::Model do
|
|
161
162
|
end
|
162
163
|
|
163
164
|
describe "#dob_? atomic getters" do
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
its(:
|
168
|
-
its(:
|
169
|
-
|
170
|
-
let(:params) { {:dob => "1995-12-24", :dod => "1935-02-14" } }
|
171
|
-
its(:dod_y) { should == '1935'}
|
172
|
-
its(:dod_m) { should == '02'}
|
173
|
-
its(:dod_d) { should == '14'}
|
174
|
-
end
|
165
|
+
|
166
|
+
shared_examples "a composed date" do |attribute|
|
167
|
+
let(:date_array) { subject.read_attribute(attribute).to_s.split('-') }
|
168
|
+
its(:dob_y) { should == date_array[0]}
|
169
|
+
its(:dob_m) { should == date_array[1]}
|
170
|
+
its(:dob_d) { should == date_array[2]}
|
175
171
|
end
|
172
|
+
|
176
173
|
context "when initialized with dob 1995-12" do
|
177
174
|
let(:params) { {:dob => "1995-12" } }
|
178
|
-
|
179
|
-
its(:dob_m) { should == '12'}
|
180
|
-
its(:dob_d) { should be_nil}
|
175
|
+
it_should_behave_like "a composed date", :dob
|
181
176
|
end
|
182
177
|
context "when initialized with dob 1995" do
|
183
178
|
let(:params) { {:dob => "1995" } }
|
184
|
-
|
185
|
-
|
186
|
-
|
179
|
+
it_should_behave_like "a composed date", :dob
|
180
|
+
end
|
181
|
+
context "when initialized with dob 1995-12-24" do
|
182
|
+
let(:params) { {:dob => "1995-12-24" } }
|
183
|
+
it_should_behave_like "a composed date", :dob
|
184
|
+
context "and when assign a new date 2001-09-11" do
|
185
|
+
before(:each) do
|
186
|
+
@obj.dob = "2001-09-11"
|
187
|
+
end
|
188
|
+
it_should_behave_like "a composed date", :dob
|
189
|
+
end
|
190
|
+
context "and when assign a new nil date" do
|
191
|
+
before(:each) do
|
192
|
+
@obj.dob = nil
|
193
|
+
end
|
194
|
+
it_should_behave_like "a composed date", :dob
|
195
|
+
end
|
187
196
|
end
|
188
197
|
end
|
189
198
|
|
190
199
|
|
191
|
-
describe "#dob_?= atomic setters effect"
|
200
|
+
describe "#dob_?= atomic setters effect" do
|
192
201
|
context "when initialized with no params" do
|
193
202
|
let(:params) { nil }
|
194
203
|
describe "#dob_y = 1976" do
|
@@ -201,32 +210,27 @@ describe EdtfRailsTest::Model do
|
|
201
210
|
before(:each) do
|
202
211
|
@obj.save
|
203
212
|
end
|
204
|
-
it_should_behave_like "
|
213
|
+
it_should_behave_like "return a Date object but store a String in the db"
|
205
214
|
describe "dob precision" do
|
206
215
|
specify { expect(@obj.dob.precision).to be :year }
|
207
216
|
end
|
208
|
-
|
209
|
-
its(:dob_m) { should be_nil}
|
210
|
-
its(:dob_d) { should be_nil}
|
217
|
+
it_should_behave_like "a composed date", :dob
|
211
218
|
end
|
212
219
|
describe "#dob_m = '05'" do
|
213
220
|
before(:each) do
|
214
221
|
@obj.dob_m = '05'
|
215
222
|
end
|
216
223
|
its(:dob) { should be_nil}
|
217
|
-
|
218
|
-
its(:dob_m) { should == '05'}
|
224
|
+
# it_should_behave_like "a composed date", :dob # NO because until it is saved
|
219
225
|
context "after saving" do
|
220
226
|
before(:each) do
|
221
227
|
@obj.save
|
222
228
|
end
|
223
|
-
it_should_behave_like "
|
229
|
+
it_should_behave_like "return a Date object but store a String in the db"
|
224
230
|
describe "dob precision" do
|
225
231
|
specify { expect(@obj.dob.precision).to be :month }
|
226
232
|
end
|
227
|
-
|
228
|
-
its(:dob_m) { should == '05'}
|
229
|
-
its(:dob_d) { should be_nil}
|
233
|
+
it_should_behave_like "a composed date", :dob
|
230
234
|
end
|
231
235
|
end
|
232
236
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: edtf-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
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-11-
|
12
|
+
date: 2013-11-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -176,6 +176,7 @@ files:
|
|
176
176
|
- lib/edtf-rails.rb
|
177
177
|
- lib/edtf-rails/edtf-rails.rb
|
178
178
|
- lib/edtf-rails/getters_and_setters.rb
|
179
|
+
- lib/edtf-rails/utils.rb
|
179
180
|
- local_history
|
180
181
|
- spec/edtf-rails_spec.rb
|
181
182
|
- spec/spec_helper.rb
|
@@ -196,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
196
197
|
version: '0'
|
197
198
|
segments:
|
198
199
|
- 0
|
199
|
-
hash:
|
200
|
+
hash: -2884630438112940405
|
200
201
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
201
202
|
none: false
|
202
203
|
requirements:
|