white_rabbit 0.0.1

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm --create ruby-1.9.2-p180@white_rabbit
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in split_datetime_fields.gemspec
4
+ gemspec
5
+
6
+ gem 'activesupport'
7
+ gem 'activemodel'
8
+ gem 'tzinfo'
9
+ gem 'validates_timeliness'
10
+ gem 'rspec', '~> 2.6'
11
+
12
+ # dev gems
13
+ gem 'looksee'
14
+ gem 'ruby-debug19'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 WellnessFX & Enrique Carlos Mogollan
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.
@@ -0,0 +1,76 @@
1
+ # White Rabbit
2
+
3
+ ## Description
4
+
5
+ Ruby library for This library let's you set a date and a time of a datetime attribute in from a rails model to allow you have separate UI elements for date and time.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'white_rabbit'
12
+
13
+ and then
14
+
15
+ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install white_rabbit
20
+
21
+ ## Usage
22
+
23
+ The spec contains this example:
24
+
25
+ class TestClass
26
+ include WhiteRabbit
27
+
28
+ # notice only the start accessor is defined
29
+ attr_accessor :start
30
+
31
+ split_datetime_fields_for :start
32
+
33
+ def initialize(attributes = {})
34
+ attributes.each do |name, value|
35
+ send("#{name}=", value)
36
+ end
37
+ end
38
+ end
39
+
40
+ Then you can do:
41
+
42
+ test_class = TestClass.new
43
+ test_class.start_date = 1.day.ago
44
+ test_class.start_time = 1.hour.ago
45
+
46
+ When both, date and time, are set and can be parsed via Timeliness you get a valid object:
47
+
48
+ test_class.start.class # will print "ActiveSupport::TimeWithZone"
49
+
50
+ However, the date and time parameters will be a string, since it's easier to use in your views.
51
+
52
+ test_class.start_date.class # will print "String" in the default us_format
53
+ test_class.start_time.class # will print "String"
54
+
55
+ To add more date/time formats, first define your date or time format:
56
+
57
+ Time::DATE_FORMATS[:long_date] = "%B %d, %Y"
58
+
59
+ Then define the parse format for Validates_Timeliness:
60
+
61
+ ValidatesTimeliness.parser.add_formats(:datetime, 'mmm d, yyyy h:nnampm')
62
+
63
+ More about ValidatesTimeliness: http://github.com/adzap/validates_timeliness
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
72
+
73
+
74
+ ## Running the Specs
75
+
76
+ rspec spec/
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,52 @@
1
+ require "white_rabbit/version"
2
+
3
+ module WhiteRabbit
4
+ extend ActiveSupport::Concern
5
+
6
+ Time::DATE_FORMATS[:us_date] = '%m/%d/%y'
7
+ Time::DATE_FORMATS[:us_12_hour_time] = '%-l:%M%P'
8
+
9
+ module ClassMethods
10
+ SPLIT_FIELDS = [:date, :time]
11
+
12
+ def split_datetime_fields_for(field_name, opts = {})
13
+ # format symbol passed into .to_s for each split field, i.e. time.to_s(:format)
14
+ default_opts = {:date_format => :us_date, :time_format => :us_12_hour_time}
15
+ opts = default_opts.merge(opts)
16
+
17
+ process_method_name = "process_#{field_name}"
18
+
19
+ SPLIT_FIELDS.each do |type|
20
+ # setter
21
+ split_field_name = "#{field_name}_#{type}"
22
+ define_method(split_field_name + '=') do |val|
23
+ val.strip! if val.present?
24
+ instance_variable_set "@#{split_field_name}", val
25
+ send(process_method_name)
26
+ end
27
+
28
+ # getter
29
+ define_method(split_field_name) do
30
+ split_value = instance_values[split_field_name]
31
+ if SPLIT_FIELDS.any? { |t| instance_values["#{field_name}_#{t}"].present? }
32
+ split_value
33
+ else
34
+ send(field_name).try(:to_s, opts[:"#{type}_format"])
35
+ end
36
+ end
37
+ end
38
+
39
+ # process datetime field from split fields
40
+ define_method(process_method_name) do
41
+ date = instance_values["#{field_name}_date"]
42
+ time = instance_values["#{field_name}_time"]
43
+ parsed = nil
44
+ if date && time
45
+ parsed = Timeliness.parse("#{date} #{time}", :zone => Time.zone)
46
+ send(:"#{field_name}=", parsed)
47
+ end
48
+ end
49
+ send(:private, process_method_name)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module WhiteRabbit
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ # debugging
2
+ require 'ruby-debug'
3
+ require 'looksee'
4
+
5
+ require 'active_support'
6
+ require 'active_support/core_ext/time/zones.rb'
7
+ require 'active_support/core_ext/date/calculations.rb'
8
+ require 'active_support/core_ext/date_time/calculations.rb'
9
+ require 'active_support/core_ext/numeric/time.rb'
10
+ require 'active_support/core_ext/object/instance_variables.rb'
11
+ require 'active_support/time_with_zone.rb'
12
+ require 'active_model'
13
+ require 'validates_timeliness'
14
+
15
+ require File.expand_path('../../lib/white_rabbit.rb', __FILE__)
16
+
17
+ ## If your environment doesn't have a default Time.zone the test define one
18
+ Time.zone = "Pacific Time (US & Canada)" unless Time.zone
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ # For testing, we define a "long date" format
4
+ Time::DATE_FORMATS[:long_date] = "%B %d, %Y"
5
+ # Letting ValidatesTimeliness parse date long format
6
+ ValidatesTimeliness.parser.add_formats(:date, 'mmm d, yyyy')
7
+ # Letting ValidatesTimeliness parse datetime long format
8
+ ValidatesTimeliness.parser.add_formats(:datetime, 'mmm d, yyyy h:nnampm')
9
+
10
+ describe WhiteRabbit do
11
+ class TestClass
12
+ include WhiteRabbit
13
+
14
+ attr_accessor :start
15
+
16
+ split_datetime_fields_for :start
17
+
18
+ def initialize(attributes = {})
19
+ attributes.each do |name, value|
20
+ send("#{name}=", value)
21
+ end
22
+ end
23
+ end
24
+
25
+ shared_examples_for "time parsing" do
26
+ context "start_date" do
27
+ describe "#start_date=" do
28
+ it "should not set start without a start_time" do
29
+ test_class = TestClass.new(:start_date => @start_date)
30
+ test_class.start.should be_nil
31
+ end
32
+
33
+ it "should not set start with an invalid start_date" do
34
+ test_class = TestClass.new(:start_date => '123/123/123')
35
+ test_class.start.should be_nil
36
+ end
37
+
38
+ it "should not overwrite start without a start_time" do
39
+ test_class = TestClass.new(:start => @datetime + 1.day)
40
+ test_class.start_date = @start_date
41
+ test_class.start.should eq(@datetime + 1.day)
42
+ end
43
+
44
+ it "should set start with a previously set start_time" do
45
+ test_class = TestClass.new(:start_time => @start_time)
46
+ test_class.start_date = @start_date
47
+ test_class.start.should eq(@datetime)
48
+ end
49
+ end
50
+
51
+ describe "#start_date" do
52
+ it "should return start_date, even if start has not been set" do
53
+ test_class = TestClass.new(:start_date => @start_date)
54
+ test_class.start_date.should eq(@start_date)
55
+ end
56
+
57
+ it "should return start in %m/%d/%Y format" do
58
+ test_class = TestClass.new(:start => @datetime)
59
+ test_class.start_date.should eq(@start_date)
60
+ end
61
+
62
+ it "should return nil if start and start_time has been set" do
63
+ test_class = TestClass.new(:start => @datetime)
64
+ test_class.start_time = '123'
65
+ test_class.start_date.should be_nil
66
+ end
67
+ end
68
+ end
69
+
70
+ context "start_time" do
71
+ describe "#start_time=" do
72
+ it "should not set start without a start_time" do
73
+ test_class = TestClass.new(:start_time => @start_time)
74
+ test_class.start.should be_nil
75
+ end
76
+
77
+ it "should not overwrite start without a start_time" do
78
+ test_class = TestClass.new(:start => @datetime + 1.day)
79
+ test_class.start_time = @start_time
80
+ test_class.start.should eq(@datetime + 1.day)
81
+ end
82
+
83
+ it "should set start with a previously set start_date" do
84
+ test_class = TestClass.new(:start_date => @start_date)
85
+ test_class.start_time = @start_time
86
+ test_class.start.should eq(@datetime)
87
+ end
88
+
89
+ it "should not set start with an invalid start_time" do
90
+ test_class = TestClass.new(:start_time => '123:345')
91
+ test_class.start.should be_nil
92
+ end
93
+ end
94
+
95
+ describe "#start_time" do
96
+ it "should return start_time, even if start has not been set" do
97
+ test_class = TestClass.new(:start_time => @start_time)
98
+ test_class.start_time.should eq(@start_time)
99
+ end
100
+
101
+ it "should return start in %I:%m%p format" do
102
+ test_class = TestClass.new(:start => @datetime)
103
+ test_class.start_time.should eq(@start_time)
104
+ end
105
+ end
106
+ end
107
+
108
+ it "should accept date and time in two fields in us date format" do
109
+ test_class = TestClass.new(:start_date => @start_date, :start_time => @start_time)
110
+ test_class.start.should eq(@datetime)
111
+ end
112
+
113
+ it "should accept date and time in two fields in long date format" do
114
+ test_class = TestClass.new(:start_date => @start_date_long, :start_time => @start_time)
115
+ test_class.start.should eq(@datetime)
116
+ end
117
+ end
118
+
119
+ describe "in system time" do
120
+ before do
121
+ @datetime = Time.zone.local(2011, 8, 12, 13, 30, 0)
122
+ @start_date = @datetime.to_s(:us_date)
123
+ @start_date_long = @datetime.to_s(:long_date)
124
+ @start_time = @datetime.to_s(:us_12_hour_time)
125
+ end
126
+
127
+ it_should_behave_like 'time parsing'
128
+ end
129
+
130
+ describe "in TOT time" do
131
+ before(:all) do
132
+ @original_timezone = Time.zone
133
+ Time.zone = "Nuku'alofa"
134
+ end
135
+
136
+ context "Correct ordering" do
137
+ before do
138
+ Time.zone = "Pacific Time (US & Canada)" unless Time.zone
139
+ @datetime = Time.zone.local(2011, 8, 12, 13, 30, 0)
140
+ @start_date = @datetime.to_s(:us_date)
141
+ @start_date_long = @datetime.to_s(:long_date)
142
+ @start_time = @datetime.to_s(:us_12_hour_time)
143
+ end
144
+
145
+ it_should_behave_like 'time parsing'
146
+ end
147
+
148
+ after(:all) do
149
+ Time.zone = @original_timezone
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/white_rabbit/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Carlos Mogollan", "WellnessFX"]
6
+ gem.email = ["emogollan@gmail.com"]
7
+ gem.description = %q{This library let's you set a date and a time of a datetime attribute in a rails model}
8
+ gem.summary = %q{White Rabbit creates accessors for your datetime object so you can set the date and time and using datetime pickers in your views }
9
+ gem.homepage = "http://www.github.com/wellnessfx/white_rabbit"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "white_rabbit"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = WhiteRabbit::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: white_rabbit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Carlos Mogollan
9
+ - WellnessFX
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-10-12 00:00:00.000000000Z
14
+ dependencies: []
15
+ description: This library let's you set a date and a time of a datetime attribute
16
+ in a rails model
17
+ email:
18
+ - emogollan@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - .rvmrc
25
+ - Gemfile
26
+ - LICENSE
27
+ - README.md
28
+ - Rakefile
29
+ - lib/white_rabbit.rb
30
+ - lib/white_rabbit/version.rb
31
+ - spec/spec_helper.rb
32
+ - spec/white_rabbit_spec.rb
33
+ - white_rabbit.gemspec
34
+ homepage: http://www.github.com/wellnessfx/white_rabbit
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.6
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: White Rabbit creates accessors for your datetime object so you can set the
58
+ date and time and using datetime pickers in your views
59
+ test_files:
60
+ - spec/spec_helper.rb
61
+ - spec/white_rabbit_spec.rb