attribution 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/attribution.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "attribution"
5
- gem.version = "0.3.1"
5
+ gem.version = "0.3.2"
6
6
  gem.authors = ["Paul Barry"]
7
7
  gem.email = ["mail@paulbarry.com"]
8
8
  gem.description = %q{Add attributes to Ruby objects}
@@ -0,0 +1,24 @@
1
+ module Attribution
2
+ module Util
3
+ # Returns an array of the values that match each key,
4
+ # up until the point at which it finds a blank value.
5
+ #
6
+ # @params hash Hash The hash containing values
7
+ # @params keys [Object] The keys to look up in the Hash
8
+ # @return [Object] The values
9
+ # @example
10
+ # extract_values({ a: 1, b: 2, c: nil, d: 4}, :a, :b, :c :d) # => [1, 2]
11
+ def self.extract_values(hash, *keys)
12
+ values = []
13
+ keys.each do |key|
14
+ value = hash[key]
15
+ if value.present?
16
+ values << value
17
+ else
18
+ break
19
+ end
20
+ end
21
+ values
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module Attribution
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/lib/attribution.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "attribution/util"
1
2
  require "attribution/version"
2
3
  require "active_support/core_ext"
3
4
 
@@ -100,7 +101,9 @@ module Attribution
100
101
  when Date then arg
101
102
  when Time, DateTime then arg.to_date
102
103
  when String then Date.parse(arg)
103
- when Hash then Date.new(*arg.slice(:year, :month, :day).values.map(&:to_i))
104
+ when Hash
105
+ args = Util.extract_values(arg, :year, :month, :day)
106
+ args.present? ? Date.new(*args.map(&:to_i)) : nil
104
107
  when nil then nil
105
108
  else raise ArgumentError.new("can't convert #{arg.class} to Date")
106
109
  end
@@ -115,7 +118,9 @@ module Attribution
115
118
  when Date, DateTime then arg.to_time
116
119
  when Time then arg
117
120
  when String then Time.parse(arg)
118
- when Hash then Time.new(*arg.slice(:year, :month, :day, :hour, :min, :sec, :utc_offset).values.map(&:to_i))
121
+ when Hash
122
+ args = Util.extract_values(arg, :year, :month, :day, :hour, :min, :sec, :utc_offset)
123
+ args.present? ? Time.new(*args.map(&:to_i)) : nil
119
124
  when nil then nil
120
125
  else raise ArgumentError.new("can't convert #{arg.class} to Time")
121
126
  end
@@ -120,11 +120,46 @@ class AttributionTest < Test::Unit::TestCase
120
120
  assert_equal Date.parse('2013-03-17'), book.published_on
121
121
  end
122
122
 
123
+ def test_date_hash_just_year
124
+ book = Book.new(:published_on => { :year => '2013', :month => '', :day => '' })
125
+ assert_equal Date.new(2013), book.published_on
126
+ end
127
+
128
+ def test_date_hash_just_year_month
129
+ book = Book.new(:published_on => { :year => '2013', :month => '5', :day => '' })
130
+ assert_equal Date.new(2013, 5), book.published_on
131
+ end
132
+
133
+ def test_date_hash_empty
134
+ book = Book.new(:published_on => { :year => '', :month => '', :day => '' })
135
+ assert_equal nil, book.published_on
136
+ end
137
+
123
138
  def test_time_hash
124
139
  book = Book.new(:created_at => { :year => '2013', :month => '03', :day => '17', :hour => '07', :min => '30', :sec => '11', :utc_offset => '3600' })
125
140
  assert_equal Time.parse('2013-03-17 07:30:11 +01:00'), book.created_at
126
141
  end
127
142
 
143
+ def test_time_hash_empty
144
+ book = Book.new(:created_at => { :year => '', :month => '', :day => '', :hour => '', :min => '', :sec => '', :utc_offset => '' })
145
+ assert_equal nil, book.created_at
146
+ end
147
+
148
+ def test_time_hash_just_year
149
+ book = Book.new(:created_at => { :year => '2013' })
150
+ assert_equal Time.parse('2013-01-01 00:00:00'), book.created_at
151
+ end
152
+
153
+ def test_time_hash_just_year_month
154
+ book = Book.new(:created_at => { :year => '2013', :month => '03' })
155
+ assert_equal Time.parse('2013-03-01 00:00:00'), book.created_at
156
+ end
157
+
158
+ def test_time_hash_just_year_month_day
159
+ book = Book.new(:created_at => { :year => '2013', :month => '03', :day => '17' })
160
+ assert_equal Time.parse('2013-03-17 00:00:00'), book.created_at
161
+ end
162
+
128
163
  def test_nil
129
164
  book = Book.new(
130
165
  :id => nil,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribution
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -57,6 +57,7 @@ files:
57
57
  - Rakefile
58
58
  - attribution.gemspec
59
59
  - lib/attribution.rb
60
+ - lib/attribution/util.rb
60
61
  - lib/attribution/version.rb
61
62
  - test/attribution_test.rb
62
63
  homepage: http://github.com/pjb3/attribution