rails_view_adapters 0.2.3 → 0.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86dc8bb0c06dda9a184daf9a9ea141496e7e733a
4
- data.tar.gz: 7e69b00f4c963579b4bdf9fe12e565d20d128c6e
3
+ metadata.gz: 13e26a6b680a1fa4c5568fb1e5e445460534ea36
4
+ data.tar.gz: 04142c6f2d5bc28b30cf4a6fe784be85f010d74f
5
5
  SHA512:
6
- metadata.gz: 4865b365bd92003bb46ab50dc360da8836f1ae0c0ce80f8187fa63770a336521dfa70084c2055a571a245ae0fb65e50661870163c9dd83b1c11f75d4459a383c
7
- data.tar.gz: b7bd8140fbe8a723716fc551a4506c9ecc3e6b3bb87ccf4af8fac331f0a4153a7fff65c3d2cad05a8d07ac21d10fe4a5576fa7821e80df4aa5ea69d426ea6e8f
6
+ metadata.gz: 208e50ae487714b67bef56377a2416989618afdbf051452313d61145f1bc2243d28086ae394d5da926ac458734a3ebb26d0340f67de12b5c51d49b308c8c98d4
7
+ data.tar.gz: a713a9fa0328e669827c4c169350aaaae61c21644eb78c8c786fbcf81d2127bda7de7e6b05778c7dd2297cc8c1651850d0960428c62ed103faeffe6cfeb834e5
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rails_view_adapters.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem "simplecov", require: false
8
+ gem "coveralls", require: false
9
+ end
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Rails View Adapters
2
2
 
3
+ [![Build Status](https://travis-ci.org/mlibrary/rails_view_adapters.svg?branch=master)](https://travis-ci.org/mlibrary/rails_view_adapters)
4
+ [![Coverage Status](https://coveralls.io/repos/github/mlibrary/rails_view_adapters/badge.svg?branch=master)](https://coveralls.io/github/mlibrary/rails_view_adapters?branch=master)
5
+
3
6
  This gem provides a
4
7
  [DSL](http://www.rubydoc.info/github/mlibrary/rails_view_adapters/master/RailsViewAdapters/DefinitionProxy)
5
8
  for defining adapters that map your model's
@@ -1,4 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require "active_support/time_with_zone"
4
+
2
5
  module RailsViewAdapters
3
6
 
4
7
  # Defines the DSL methods that are used to modify the underlying
@@ -45,14 +48,19 @@ module RailsViewAdapters
45
48
  map.add_model_field(model_field)
46
49
  end
47
50
 
48
- # Register a one-to-one mapping of a date field.
51
+ # Register a one-to-one mapping of a date field. When converting
52
+ # from the public representation, if the non-string values are
53
+ # returned as-is. Strings that cannot be parsed with the given
54
+ # date_format string are returned as nil.
55
+ #
56
+ # If no timezone is provided, utc is assumed.
49
57
  # @param [Symbol] model_field
50
58
  # @param [Symbol] public_field
51
59
  # @param [String] date_format The Date format to use.
52
60
  def map_date(model_field, public_field, date_format)
53
61
  raise ArgumentError if date_format.nil?
54
62
  map_from_public public_field do |value|
55
- { model_field => time_from_public(value) }
63
+ { model_field => time_from_public(value, date_format) }
56
64
  end
57
65
  map_to_public model_field do |value|
58
66
  { public_field => value.utc.strftime(date_format) }
@@ -136,14 +144,33 @@ module RailsViewAdapters
136
144
 
137
145
  private
138
146
 
139
- def time_from_public(time)
147
+ def time_from_public(time, date_format)
140
148
  if time.is_a? String
141
- Time.zone.parse(time)
149
+ parts_to_time(DateTime._strptime(time, date_format))
142
150
  else
143
151
  time
144
152
  end
145
153
  end
146
154
 
155
+ def parts_to_time(parts)
156
+ return nil if parts.empty?
157
+ begin
158
+ time = Time.new(
159
+ parts.fetch(:year),
160
+ parts.fetch(:mon),
161
+ parts.fetch(:mday),
162
+ parts.fetch(:hour),
163
+ parts.fetch(:min),
164
+ parts.fetch(:sec) + parts.fetch(:sec_fraction, 0),
165
+ parts.fetch(:offset, 0)
166
+ )
167
+ rescue KeyError
168
+ return nil
169
+ end
170
+
171
+ ActiveSupport::TimeWithZone.new(time.utc, Time.zone)
172
+ end
173
+
147
174
  def to_bool(value)
148
175
  return nil if value.nil? || value =~ /^(null|nil)$/i
149
176
  return true if value == true || value =~ /^(true|t|yes|y|1)$/i
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module RailsViewAdapters
3
- VERSION = "0.2.3"
3
+ VERSION = "0.2.4"
4
4
  end
@@ -81,8 +81,8 @@ module RailsViewAdapters
81
81
  let(:model_field) { :mod }
82
82
  let(:public_field) { :pub }
83
83
  let(:date_format) { "%Y-%m-%dT%H:%M:%SZ" }
84
- let(:time) { Time.new(2016, 10, 31, 12, 25, 33).utc }
85
- let(:time_string) { time.strftime(date_format) }
84
+ let(:time) { Time.utc(2012, 6, 10, 13, 07, 33) }
85
+ let(:time_string) { "2012-06-10T13:07:33Z" }
86
86
  it "creates the correct model_fields" do
87
87
  proxy.map_date(model_field, public_field, date_format)
88
88
  expect(proxy.map.model_fields).to eql([model_field])
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,14 @@
1
-
2
1
  # frozen_string_literal: true
2
+
3
+ require "simplecov"
4
+ require "coveralls"
5
+
6
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
7
+ SimpleCov.start do
8
+ add_filter "/.bundle/"
9
+ add_filter "/spec/"
10
+ end
11
+
3
12
  RSpec.configure do |config|
4
13
  config.expect_with :rspec do |expectations|
5
14
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_view_adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Hockey
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-02 00:00:00.000000000 Z
11
+ date: 2016-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord