edtf-rails 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p448
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ gem "activerecord", "~> 3.2.15"
4
+ gem "edtf", "~> 1.0"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 1.8.7"
13
+ gem "debugger"
14
+ gem "sqlite3"
15
+ gem "awesome_print"
16
+ # gem "rcov", ">= 0"
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,93 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.15)
5
+ activesupport (= 3.2.15)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.15)
8
+ activemodel (= 3.2.15)
9
+ activesupport (= 3.2.15)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.15)
13
+ i18n (~> 0.6, >= 0.6.4)
14
+ multi_json (~> 1.0)
15
+ addressable (2.3.5)
16
+ arel (3.0.2)
17
+ awesome_print (1.2.0)
18
+ builder (3.0.4)
19
+ columnize (0.3.6)
20
+ debugger (1.6.2)
21
+ columnize (>= 0.3.1)
22
+ debugger-linecache (~> 1.2.0)
23
+ debugger-ruby_core_source (~> 1.2.3)
24
+ debugger-linecache (1.2.0)
25
+ debugger-ruby_core_source (1.2.3)
26
+ diff-lcs (1.2.4)
27
+ edtf (1.0.0)
28
+ activesupport (~> 3.0)
29
+ faraday (0.8.8)
30
+ multipart-post (~> 1.2.0)
31
+ git (1.2.6)
32
+ github_api (0.10.1)
33
+ addressable
34
+ faraday (~> 0.8.1)
35
+ hashie (>= 1.2)
36
+ multi_json (~> 1.4)
37
+ nokogiri (~> 1.5.2)
38
+ oauth2
39
+ hashie (2.0.5)
40
+ highline (1.6.20)
41
+ httpauth (0.2.0)
42
+ i18n (0.6.5)
43
+ jeweler (1.8.8)
44
+ builder
45
+ bundler (~> 1.0)
46
+ git (>= 1.2.5)
47
+ github_api (= 0.10.1)
48
+ highline (>= 1.6.15)
49
+ nokogiri (= 1.5.10)
50
+ rake
51
+ rdoc
52
+ json (1.8.1)
53
+ jwt (0.1.8)
54
+ multi_json (>= 1.5)
55
+ multi_json (1.8.2)
56
+ multi_xml (0.5.5)
57
+ multipart-post (1.2.0)
58
+ nokogiri (1.5.10)
59
+ oauth2 (0.9.2)
60
+ faraday (~> 0.8)
61
+ httpauth (~> 0.2)
62
+ jwt (~> 0.1.4)
63
+ multi_json (~> 1.0)
64
+ multi_xml (~> 0.5)
65
+ rack (~> 1.2)
66
+ rack (1.5.2)
67
+ rake (10.1.0)
68
+ rdoc (3.12.2)
69
+ json (~> 1.4)
70
+ rspec (2.14.1)
71
+ rspec-core (~> 2.14.0)
72
+ rspec-expectations (~> 2.14.0)
73
+ rspec-mocks (~> 2.14.0)
74
+ rspec-core (2.14.6)
75
+ rspec-expectations (2.14.3)
76
+ diff-lcs (>= 1.1.3, < 2.0)
77
+ rspec-mocks (2.14.4)
78
+ sqlite3 (1.3.8)
79
+ tzinfo (0.3.38)
80
+
81
+ PLATFORMS
82
+ ruby
83
+
84
+ DEPENDENCIES
85
+ activerecord (~> 3.2.15)
86
+ awesome_print
87
+ bundler (~> 1.0)
88
+ debugger
89
+ edtf (~> 1.0)
90
+ jeweler (~> 1.8.7)
91
+ rdoc (~> 3.12)
92
+ rspec (~> 2.8)
93
+ sqlite3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 masciugo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = edtf-rails
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to edtf-rails
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 masciugo. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "edtf-rails"
18
+ gem.homepage = "http://github.com/masciugo/edtf-rails"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{An ActiveRecord extension to deal with Extended DateTime Format attributes}
21
+ gem.description = %Q{An ActiveRecord extension to deal with Extended DateTime Format attributes}
22
+ gem.email = "masciugo@gmail.com"
23
+ gem.authors = ["masciugo"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+
31
+ RSpec::Core::RakeTask.new(:spec, :tag) do |t, task_args|
32
+ t.pattern = FileList['spec/**/*_spec.rb']
33
+ t.rspec_opts = "--format documentation --color --debug"
34
+ t.rspec_opts += " --tag #{task_args[:tag]}" unless task_args[:tag].nil?
35
+ end
36
+ # RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ # spec.pattern = 'spec/**/*_spec.rb'
38
+ # spec.rcov = true
39
+ # end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "edtf-rails #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.1
@@ -0,0 +1,81 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "edtf-rails"
8
+ s.version = "0.2.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["masciugo"]
12
+ s.date = "2013-11-12"
13
+ s.description = "An ActiveRecord extension to deal with Extended DateTime Format attributes"
14
+ s.email = "masciugo@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".ruby-version",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "edtf-rails.gemspec",
30
+ "lib/edtf-rails.rb",
31
+ "lib/edtf-rails/edtf-rails.rb",
32
+ "lib/edtf-rails/getters_and_setters.rb",
33
+ "local_history",
34
+ "spec/edtf-rails_spec.rb",
35
+ "spec/spec_helper.rb",
36
+ "spec/support/database.yml",
37
+ "spec/support/edtf_rails_test_model.rb"
38
+ ]
39
+ s.homepage = "http://github.com/masciugo/edtf-rails"
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = "1.8.23"
43
+ s.summary = "An ActiveRecord extension to deal with Extended DateTime Format attributes"
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.2.15"])
50
+ s.add_runtime_dependency(%q<edtf>, ["~> 1.0"])
51
+ s.add_development_dependency(%q<rspec>, ["~> 2.8"])
52
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.7"])
55
+ s.add_development_dependency(%q<debugger>, [">= 0"])
56
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
57
+ s.add_development_dependency(%q<awesome_print>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<activerecord>, ["~> 3.2.15"])
60
+ s.add_dependency(%q<edtf>, ["~> 1.0"])
61
+ s.add_dependency(%q<rspec>, ["~> 2.8"])
62
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
65
+ s.add_dependency(%q<debugger>, [">= 0"])
66
+ s.add_dependency(%q<sqlite3>, [">= 0"])
67
+ s.add_dependency(%q<awesome_print>, [">= 0"])
68
+ end
69
+ else
70
+ s.add_dependency(%q<activerecord>, ["~> 3.2.15"])
71
+ s.add_dependency(%q<edtf>, ["~> 1.0"])
72
+ s.add_dependency(%q<rspec>, ["~> 2.8"])
73
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
74
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
75
+ s.add_dependency(%q<jeweler>, ["~> 1.8.7"])
76
+ s.add_dependency(%q<debugger>, [">= 0"])
77
+ s.add_dependency(%q<sqlite3>, [">= 0"])
78
+ s.add_dependency(%q<awesome_print>, [">= 0"])
79
+ end
80
+ end
81
+
@@ -0,0 +1,136 @@
1
+ module EdtfRails
2
+
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
+
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
+ def edtf options = {}
23
+
24
+ admitted_keys = [:attributes]
25
+ check_options(options, admitted_keys)
26
+
27
+ class_attribute :edtf_attributes
28
+ self.edtf_attributes = [options[:attributes]].flatten
29
+
30
+ validates_format_of edtf_attributes, :with => EDTF_STRING_FORMAT, :allow_nil => true
31
+
32
+ edtf_attributes.each do |d|
33
+
34
+ attr_accessible d.to_sym
35
+
36
+ # getters
37
+ define_method(d) do
38
+ # read_attribute(d) && (EDTF.parse(read_attribute(d)) || read_attribute(d)) #if the format is not EDTF compatible the string will be returned (validation call this getter and need a value)
39
+ EDTF.parse(read_attribute(d)) || read_attribute(d)
40
+ end
41
+
42
+ # virtual attributes dob_y, dob_m....
43
+ [:y, :m, :d].each do |xx|
44
+ attr_accessor "#{d}_#{xx}"
45
+ attr_accessible "#{d}_#{xx}".to_sym
46
+ end
47
+
48
+ # setters
49
+ define_method("#{d}=") do |edtf_date|
50
+ edtf_date = edtf_date.edtf if edtf_date.is_a? Date
51
+ write_attribute(d,edtf_date)
52
+ end
53
+
54
+
55
+ end
56
+
57
+ # virtual attributes settings after any initialization
58
+ after_initialize do
59
+ edtf_attributes.each do |d|
60
+ if read_attribute(d)
61
+ [:y, :m, :d].each_with_index do |x,i|
62
+ send("#{d}_#{x}=",read_attribute(d).split('-')[i])
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ # virtual attributes utilization to set date before validation (they are used only if at least one of them is defined)
69
+ before_validation do
70
+ edtf_attributes.each do |d|
71
+ 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
73
+ date_array.each{|x| x.blank? ? break : (res += "-#{x}") }
74
+ self[d]= res
75
+ end
76
+ end
77
+ end
78
+
79
+
80
+ # Include instance methods and class methods
81
+ include EdtfRails::GettersAndSetters
82
+
83
+ end
84
+
85
+ # def has_parents options = {}
86
+
87
+ # admitted_keys = [:sex_column, :sex_values, :father_column, :mother_column, :current_spouse_column, :current_spouse]
88
+ # check_options(options, admitted_keys) do |key, value|
89
+ # if key == :sex_values
90
+ # raise WrongOptionException, ":sex_values option must be an array with two char: first for male sex symbol an last for female" unless value.is_a?(Array) and value.size == 2 and value.first.to_s.size == 1 and value.last.to_s.size == 1
91
+ # end
92
+ # end
93
+
94
+ # class_attribute :genealogy_enabled, :current_spouse_enabled, :genealogy_class
95
+ # self.genealogy_enabled = true
96
+ # self.current_spouse_enabled = options[:current_spouse].try(:==,true) || false
97
+ # self.genealogy_class = self #keep track of the original extend class to prevent wrong scopes in query method in case of STI
98
+
99
+ # tracked_relatives = [:father, :mother]
100
+ # tracked_relatives << :current_spouse if current_spouse_enabled
101
+
102
+ # ## sex
103
+ # # class attributes
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
+
136
+ end
@@ -0,0 +1,14 @@
1
+ module EdtfRails
2
+ module GettersAndSetters
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ # def try_edtf(date)
7
+ # if date
8
+ # EDTF.parse(date.to_s) || date
9
+ # end
10
+ # end
11
+
12
+ end
13
+ end
14
+ end
data/lib/edtf-rails.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "edtf"
2
+ # require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/constants')
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')
5
+ require File.join(File.expand_path(File.dirname(__FILE__)), 'edtf-rails/edtf-rails')
6
+
7
+ ActiveRecord::Base.send :extend, EdtfRails
data/local_history ADDED
@@ -0,0 +1,31 @@
1
+ 1 2013-10-28 09:22:57 hexdump testCRLF.txt
2
+ 497 2013-10-28 09:22:57 chmod a+w
3
+ 498 2013-10-28 09:22:57 ls -la
4
+ 499 2013-10-28 09:22:57 pwd
5
+ 836 2013-10-28 09:22:57 '
6
+ 839 2013-10-28 09:22:57 exit
7
+ 840 2013-10-28 09:22:57 '
8
+ 842 2013-10-28 09:22:57 exit
9
+ 843 2013-10-28 09:22:57 '
10
+ 1545 2013-10-28 09:23:19 rvm gemset use edtf-rails
11
+ 1546 2013-10-28 09:23:31 rvm gemset list
12
+ 1547 2013-10-28 09:23:48 rvm gemset rename edtf_rails edtf-rails
13
+ 1548 2013-10-28 09:24:21 rvm gemset list
14
+ 1549 2013-10-28 09:24:36 rvm gemset use edtf-rails
15
+ 1550 2013-10-28 09:24:44 cd dev/edtf-rails/
16
+ 1551 2013-10-28 09:24:48 subl .
17
+ 1552 2013-10-28 10:14:05 rake spec[wip] --trace
18
+ 1553 2013-10-28 10:15:45 rake spec --trace
19
+ 1554 2013-10-28 10:31:52 bundle
20
+ 1555 2013-10-28 10:32:28 rake spec --trace
21
+ 1556 2013-10-28 11:00:30 rake spec[wip] --trace
22
+ 1557 2013-10-28 11:58:42 rake spec --trace
23
+ 1558 2013-10-28 12:05:14 rake spec[wip] --trace
24
+ 1559 2013-10-28 12:06:10 rake spec --trace
25
+ 1560 2013-10-28 12:26:47 rake spec[wip] --trace
26
+ 1561 2013-10-28 12:30:52 rake spec --trace
27
+ 1562 2013-10-28 12:33:15 rake spec[wip] --trace
28
+ 1563 2013-10-28 12:43:33 rake spec --trace
29
+ 1564 2013-10-28 12:45:42 rake spec[wip] --trace
30
+ 1566 2013-10-30 10:26:58 history | grep 2013-10-28 >>local_history
31
+ 1567 2013-10-30 10:27:02 history | grep 2013-10-28 >local_history
@@ -0,0 +1,236 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "*** Model and table settings ***" do
4
+
5
+ before(:each) do
6
+ EdtfRailsTest.define_test_model_class(edtf_opts)
7
+ end
8
+
9
+ describe 'model with edtf options: :attributes => :dob' do
10
+
11
+ let(:edtf_opts) { {:attributes => :dob} }
12
+
13
+ it "has :dob in its edtf attributes list" do
14
+ EdtfRailsTest::Model.edtf_attributes.should include(:dob)
15
+ end
16
+
17
+ it "should have a db column named dob" do
18
+ EdtfRailsTest::Model.column_names.should include('dob')
19
+ end
20
+
21
+ end
22
+
23
+ describe 'model with edtf options: :attributes => [:dob,:dod]' do
24
+
25
+ let(:edtf_opts) { {:attributes => [:dob,:dod]} }
26
+
27
+ [:dob,:dod].each do |attr|
28
+
29
+ it "has #{attr} in its edtf attributes list" do
30
+ EdtfRailsTest::Model.edtf_attributes.should include(attr)
31
+ end
32
+
33
+ it "should have a db column named #{attr}" do
34
+ EdtfRailsTest::Model.column_names.should include(attr.to_s)
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ describe EdtfRails do
44
+ describe 'EdtfRails::EDTF_STRING_FORMAT' do
45
+ # expect(EdtfRails::EDTF_STRING_FORMAT).to be(/\d{4}(-\d{2}(-\d{2})?)?/)
46
+ [
47
+ '2000-12-12',
48
+ '2000-12-01',
49
+ '2000-12',
50
+ '2000-01',
51
+ '2000',
52
+ ].each do |date|
53
+ specify {expect(EdtfRails::EDTF_STRING_FORMAT).to match(date)}
54
+ end
55
+ [
56
+ '2000-12-1',
57
+ '2000-2-01',
58
+ '2000-2',
59
+ '97-01-02',
60
+ '1997-',
61
+ ].each do |date|
62
+ specify {expect(EdtfRails::EDTF_STRING_FORMAT).not_to match(date)}
63
+ end
64
+ [
65
+ '2000-13-10',
66
+ '2000-12-99',
67
+ '2000-30-60'
68
+ ].each do |date|
69
+ specify {expect(EdtfRails::EDTF_STRING_FORMAT).not_to match(date)}
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ EdtfRailsTest.define_test_model_class({:attributes => [:dob,:dod]})
77
+ describe EdtfRailsTest::Model do
78
+
79
+ before(:each) do
80
+ EdtfRailsTest.define_test_model_class({:attributes => described_class.edtf_attributes}) #define model again so that the underline table get recreated with the correct columns
81
+ @obj = described_class.new(params)
82
+ end
83
+
84
+ subject { @obj }
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
+ describe "#new" do
97
+ context "when initialized with no params" do
98
+ let(:params) { nil }
99
+ specify { should be_valid }
100
+ its(:dob) {should be_nil }
101
+ its(:dod) {should be_nil }
102
+ describe "#dob=new_date" do
103
+ before(:each) do
104
+ @obj.dob = new_date
105
+ @obj.save!
106
+ end
107
+
108
+ context "when new_date is Date.new(2001,9,10) " do
109
+ let(:new_date) { Date.new(2001,9,10) }
110
+ it_should_behave_like "dob getter return a Date object but a string is stored in the db"
111
+ its(:dob) {should eql Date.new(2001,9,10) }
112
+ end
113
+ context "when new_date is the string '2001-09-10'" do
114
+ let(:new_date) { '2001-09-10' }
115
+ it_should_behave_like "dob getter return a Date object but a string is stored in the db"
116
+ its(:dob) {should eql Date.new(2001,9,10) }
117
+ end
118
+ context "when new_date is the string '2001-09'" do
119
+ let(:new_date) { '2001-09' }
120
+ it_should_behave_like "dob getter return a Date object but a string is stored in the db"
121
+ describe "dob precision" do
122
+ specify { expect(@obj.dob.precision).to be :month }
123
+ end
124
+ end
125
+
126
+ end
127
+ end
128
+ context "when initialized with" do
129
+ context "year precise dob 1995" do
130
+ let(:params) { {:dob => "1995" } }
131
+ specify { should be_valid }
132
+ end
133
+ context "month precise dob 1995-11" do
134
+ let(:params) { {:dob => "1995-11" } }
135
+ specify { should be_valid }
136
+ end
137
+ context "unadmitted dob 2001-03-35" do
138
+ let(:params) { {:dob => "2001-03-35" } }
139
+ specify { should_not be_valid }
140
+ end
141
+ context "dob 2001-03-25"do
142
+ let(:params) { {:dob => "2001-03-25" } }
143
+ specify { should be_valid }
144
+ context "and unadmitted dod 2001-99-25" do
145
+ let(:params) { {:dob => "2001-03-25",:dod => "2001-99-25" } }
146
+ specify { should_not be_valid }
147
+ end
148
+ context "and month precise dod 2013-11" do
149
+ let(:params) { {:dob => "2001-03-25",:dod => "2013-11" } }
150
+ its(:dob) {should be_a Date }
151
+ its(:dod) {should be_a Date }
152
+ describe "dob precision" do
153
+ specify { expect(@obj.dob.precision).to be :day }
154
+ end
155
+ describe "dod precision" do
156
+ specify { expect(@obj.dod.precision).to be :month }
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ describe "#dob_? atomic getters" do
164
+ context "when initialized with dob 1995-12-24" do
165
+ let(:params) { {:dob => "1995-12-24" } }
166
+ its(:dob_y) { should == '1995'}
167
+ its(:dob_m) { should == '12'}
168
+ its(:dob_d) { should == '24'}
169
+ context "and dod 1935-02-14" do
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
175
+ end
176
+ context "when initialized with dob 1995-12" do
177
+ let(:params) { {:dob => "1995-12" } }
178
+ its(:dob_y) { should == '1995'}
179
+ its(:dob_m) { should == '12'}
180
+ its(:dob_d) { should be_nil}
181
+ end
182
+ context "when initialized with dob 1995" do
183
+ let(:params) { {:dob => "1995" } }
184
+ its(:dob_y) { should == '1995'}
185
+ its(:dob_m) { should be_nil}
186
+ its(:dob_d) { should be_nil}
187
+ end
188
+ end
189
+
190
+
191
+ describe "#dob_?= atomic setters effect", :wip => true do
192
+ context "when initialized with no params" do
193
+ let(:params) { nil }
194
+ describe "#dob_y = 1976" do
195
+ before(:each) do
196
+ @obj.dob_y = '1976'
197
+ end
198
+ its(:dob) { should be_nil}
199
+ its(:dob_y) { should == '1976'}
200
+ context "after saving" do
201
+ before(:each) do
202
+ @obj.save
203
+ end
204
+ it_should_behave_like "dob getter return a Date object but a string is stored in the db"
205
+ describe "dob precision" do
206
+ specify { expect(@obj.dob.precision).to be :year }
207
+ end
208
+ its(:dob_y) { should == '1976'}
209
+ its(:dob_m) { should be_nil}
210
+ its(:dob_d) { should be_nil}
211
+ end
212
+ describe "#dob_m = '05'" do
213
+ before(:each) do
214
+ @obj.dob_m = '05'
215
+ end
216
+ its(:dob) { should be_nil}
217
+ its(:dob_y) { should == '1976'}
218
+ its(:dob_m) { should == '05'}
219
+ context "after saving" do
220
+ before(:each) do
221
+ @obj.save
222
+ end
223
+ it_should_behave_like "dob getter return a Date object but a string is stored in the db"
224
+ describe "dob precision" do
225
+ specify { expect(@obj.dob.precision).to be :month }
226
+ end
227
+ its(:dob_y) { should == '1976'}
228
+ its(:dob_m) { should == '05'}
229
+ its(:dob_d) { should be_nil}
230
+ end
231
+ end
232
+
233
+ end
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require "active_record"
5
+ require "logger"
6
+ require 'edtf-rails'
7
+ require 'awesome_print'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+ config.expect_with :rspec do |c|
15
+ # # Disable the `expect` sytax...
16
+ c.syntax = :should
17
+
18
+ # ...or disable the `should` syntax...
19
+ c.syntax = :expect
20
+
21
+ # # ...or explicitly enable both
22
+ c.syntax = [:should, :expect]
23
+ end
24
+ end
25
+
26
+ EdtfRailsTest.connect_to_database
@@ -0,0 +1,13 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: spec/test.sqlite3.db
4
+
5
+ mysql:
6
+ adapter: mysql2
7
+ encoding: utf8
8
+ reconnect: false
9
+ database: edtf_rails_test
10
+ pool: 5
11
+ username: root
12
+ password:
13
+ socket: /tmp/mysql.sock
@@ -0,0 +1,39 @@
1
+ module EdtfRailsTest
2
+
3
+ def self.connect_to_database
4
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
5
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
6
+ ActiveRecord::Base.establish_connection(config['sqlite3'])
7
+ end
8
+
9
+ def self.define_test_model_class edtf_attributes_opts = {}
10
+ model = Class.new(ActiveRecord::Base) do
11
+ self.table_name = 'test_records'
12
+
13
+ edtf edtf_attributes_opts
14
+
15
+ # attr_accessible :name, :dob, :dod
16
+
17
+ def inspect
18
+ "#{name }"
19
+ end
20
+
21
+ end
22
+
23
+ remove_const(:Model) if defined?(self::Model)
24
+ self.const_set 'Model', model
25
+
26
+ cn = ActiveRecord::Base.connection
27
+ cn.drop_table 'test_records' if cn.table_exists?('test_records')
28
+
29
+ cn.create_table 'test_records' do |table|
30
+ table.string :name
31
+ model.edtf_attributes.each do |a|
32
+ table.string a
33
+ end
34
+ end
35
+
36
+ self::Model.reset_column_information
37
+ end
38
+ end
39
+
metadata ADDED
@@ -0,0 +1,212 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: edtf-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - masciugo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.15
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: 3.2.15
30
+ - !ruby/object:Gem::Dependency
31
+ name: edtf
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.8'
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: '2.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '1.0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.7
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.7
110
+ - !ruby/object:Gem::Dependency
111
+ name: debugger
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: sqlite3
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: awesome_print
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: An ActiveRecord extension to deal with Extended DateTime Format attributes
159
+ email: masciugo@gmail.com
160
+ executables: []
161
+ extensions: []
162
+ extra_rdoc_files:
163
+ - LICENSE.txt
164
+ - README.rdoc
165
+ files:
166
+ - .document
167
+ - .rspec
168
+ - .ruby-version
169
+ - Gemfile
170
+ - Gemfile.lock
171
+ - LICENSE.txt
172
+ - README.rdoc
173
+ - Rakefile
174
+ - VERSION
175
+ - edtf-rails.gemspec
176
+ - lib/edtf-rails.rb
177
+ - lib/edtf-rails/edtf-rails.rb
178
+ - lib/edtf-rails/getters_and_setters.rb
179
+ - local_history
180
+ - spec/edtf-rails_spec.rb
181
+ - spec/spec_helper.rb
182
+ - spec/support/database.yml
183
+ - spec/support/edtf_rails_test_model.rb
184
+ homepage: http://github.com/masciugo/edtf-rails
185
+ licenses:
186
+ - MIT
187
+ post_install_message:
188
+ rdoc_options: []
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ! '>='
195
+ - !ruby/object:Gem::Version
196
+ version: '0'
197
+ segments:
198
+ - 0
199
+ hash: 3685247014501985280
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ requirements: []
207
+ rubyforge_project:
208
+ rubygems_version: 1.8.23
209
+ signing_key:
210
+ specification_version: 3
211
+ summary: An ActiveRecord extension to deal with Extended DateTime Format attributes
212
+ test_files: []