ensurance 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2730601cced0af6c9f54a2aabf877098b16b234d
4
- data.tar.gz: 8ad471692d7c97dccdfa3a71dbdb1325840c61fd
3
+ metadata.gz: 8eb671568b29b89cf20287832d8387769ed8848a
4
+ data.tar.gz: dfc080f66aee9df634277134db7ff61914afdf96
5
5
  SHA512:
6
- metadata.gz: 1bedafd66aa7f785ff06f002f63c5acbe02db78f61fb8cb5b495a5f568237de2a31bc52b20fd7561cb75290118aed60d10e27ee1ca95835b748c9987fc6e6d43
7
- data.tar.gz: 339991a51002d16c3d20e301733a4a685454873465ea48abae780553709be61b88229ade531e64da2335b1c814daba8fa8c1ada1e5105ecef61dd48b19a3642e
6
+ metadata.gz: 56351b09931c81edce4f1a30e0496e19feb6e330d538e6977b39375c4f7cb7f2ce8d9918ab8d26d12db9cc765b789ba3522c65b14c5e92b3c1647dc812e7b289
7
+ data.tar.gz: 3c65be266c128ed5ba7a440a20614233dee766e99ed9ad1ea0a7cf99c1c3f08dba9a9cb03382c35cb4a5c61c82a1234a588442c704ea07f185b57f45aba42990
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ensurance (0.1.0)
4
+ ensurance (0.1.1)
5
5
  activesupport (>= 3, < 6)
6
6
 
7
7
  GEM
Binary file
data/lib/ensurance.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require "ensurance/version"
2
2
  require 'active_support'
3
3
 
4
- require 'ensurance/date'
5
- require 'ensurance/time'
6
- require 'ensurance/hash'
4
+ require 'ensurance/date_ensure'
5
+ require 'ensurance/time_ensure'
6
+ require 'ensurance/hash_ensure'
7
7
 
8
8
  module Ensurance
9
9
  extend ActiveSupport::Concern
@@ -0,0 +1,53 @@
1
+ # NOTE: https://blog.daveallie.com/clean-monkey-patching/
2
+
3
+ require 'active_support/core_ext/date'
4
+
5
+ module Ensurance
6
+ module DateEnsure
7
+ def self.prepended(base)
8
+ base.singleton_class.prepend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ DATE_ENSURE_FORMATS = %w|%m/%d/%Y %Y/%m/%d|.freeze
13
+ def ensure(thing)
14
+ case thing.class.name
15
+ when "NilClass"
16
+ nil
17
+ when "Integer","Float"
18
+ Time.ensure(thing).to_date
19
+ when "Date"
20
+ thing
21
+ when "String"
22
+ if thing.to_i.to_s == thing
23
+ ::Time.at(thing.to_i).to_date
24
+ elsif thing.to_f.to_s == thing
25
+ ::Time.at(thing.to_f).to_date
26
+ elsif thing.index("/")
27
+ # Assume US Date format
28
+ result = nil
29
+ DATE_ENSURE_FORMATS.each do |f|
30
+ begin
31
+ result = Date.strptime(thing, f)
32
+ break
33
+ rescue ArgumentError
34
+ end
35
+ end
36
+ raise ArgumentError.new("Bad Date: #{thing}".yellow) unless result
37
+ result
38
+ else
39
+ ::Date.parse(thing)
40
+ end
41
+ else
42
+ if thing.respond_to?(:to_date)
43
+ thing.to_date
44
+ else
45
+ raise ArgumentError.new("Unknown Type for Date to ensure: #{thing.class.name}")
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ ::Date.prepend(Ensurance::DateEnsure)
@@ -0,0 +1,37 @@
1
+ # NOTE: https://blog.daveallie.com/clean-monkey-patching/
2
+
3
+ require 'active_support/core_ext/hash'
4
+ require 'json'
5
+
6
+ module Ensurance
7
+ module HashEnsure
8
+ def self.prepended(base)
9
+ base.singleton_class.prepend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def ensure(thing)
14
+ case thing.class.name
15
+ when "Hash","HashWithIndifferentAccess"
16
+ thing
17
+ when "String"
18
+ JSON.parse(thing)
19
+ when "NilClass"
20
+ nil
21
+ else
22
+ if thing.respond_to?(:to_h)
23
+ begin
24
+ thing.to_h
25
+ rescue TypeError
26
+ raise ArgumentError.new("Unhandled Type for Hash to ensure: #{thing.class}")
27
+ end
28
+ else
29
+ raise ArgumentError.new("Unhandled Type for Hash to ensure: #{thing.class}")
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ ::Hash.prepend( Ensurance::HashEnsure )
@@ -0,0 +1,44 @@
1
+ require 'active_support/core_ext/time'
2
+
3
+ # NOTE: https://blog.daveallie.com/clean-monkey-patching/
4
+
5
+ module Ensurance
6
+ module TimeEnsure
7
+ def self.prepended(base)
8
+ base.singleton_class.prepend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ def ensure(thing)
13
+ case thing.class.name
14
+ when "NilClass"
15
+ thing
16
+ when "Time"
17
+ thing
18
+ when "Date"
19
+ thing.beginning_of_day
20
+ when "Integer", "Float"
21
+ ::Time.at(thing)
22
+ when "String"
23
+ if thing.to_i.to_s == thing
24
+ ::Time.at(thing.to_i)
25
+ elsif thing.to_f.to_s == thing
26
+ ::Time.at(thing.to_f)
27
+ else
28
+ ::Time.parse(thing)
29
+ end
30
+ else
31
+ if thing.respond_to?(:to_time)
32
+ thing.to_time
33
+ else
34
+ raise ArgumentError.new("Unhandled Type for Time to ensure: #{thing.class}")
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+ ::Time.prepend(Ensurance::TimeEnsure)
44
+
@@ -1,3 +1,3 @@
1
1
  module Ensurance
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ensurance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Sharpe
@@ -153,9 +153,9 @@ files:
153
153
  - ensurance.gemspec
154
154
  - file:memdb1?mode=memory&cache=shared
155
155
  - lib/ensurance.rb
156
- - lib/ensurance/date.rb
157
- - lib/ensurance/hash.rb
158
- - lib/ensurance/time.rb
156
+ - lib/ensurance/date_ensure.rb
157
+ - lib/ensurance/hash_ensure.rb
158
+ - lib/ensurance/time_ensure.rb
159
159
  - lib/ensurance/version.rb
160
160
  homepage: https://github.com/bsharpe/ensurance
161
161
  licenses:
@@ -1,45 +0,0 @@
1
- require 'active_support/core_ext/date'
2
-
3
- module Ensurance
4
- module Date
5
- FORMATS = %w|%m/%d/%Y %Y/%m/%d|.freeze
6
- def self.ensure(thing)
7
- case thing.class.name
8
- when "NilClass"
9
- nil
10
- when "Integer","Float"
11
- Time.ensure(thing).to_date
12
- when "Date"
13
- thing
14
- when "String"
15
- if thing.to_i.to_s == thing
16
- ::Time.at(thing.to_i).to_date
17
- elsif thing.to_f.to_s == thing
18
- ::Time.at(thing.to_f).to_date
19
- elsif thing.index("/")
20
- # Assume US Date format
21
- result = nil
22
- FORMATS.each do |f|
23
- begin
24
- result = Date.strptime(thing, f)
25
- break
26
- rescue ArgumentError
27
- end
28
- end
29
- raise ArgumentError.new("Bad Date: #{thing}".yellow) unless result
30
- result
31
- else
32
- ::Date.parse(thing)
33
- end
34
- else
35
- if thing.respond_to?(:to_date)
36
- thing.to_date
37
- else
38
- raise ArgumentError.new("Unknown Type for Date to ensure: #{thing.class.name}")
39
- end
40
- end
41
- end
42
- end
43
- end
44
-
45
- ::Date.include Ensurance::Date
@@ -1,29 +0,0 @@
1
- require 'active_support/core_ext/hash'
2
- require 'json'
3
-
4
- module Ensurance
5
- module Hash
6
- def self.ensure(thing)
7
- case thing.class.name
8
- when "Hash","HashWithIndifferentAccess"
9
- thing
10
- when "String"
11
- JSON.parse(thing)
12
- when "NilClass"
13
- nil
14
- else
15
- if thing.respond_to?(:to_h)
16
- begin
17
- thing.to_h
18
- rescue TypeError
19
- raise ArgumentError.new("Unhandled Type for Hash to ensure: #{thing.class}")
20
- end
21
- else
22
- raise ArgumentError.new("Unhandled Type for Hash to ensure: #{thing.class}")
23
- end
24
- end
25
- end
26
- end
27
- end
28
-
29
- ::Hash.include Ensurance::Hash
@@ -1,34 +0,0 @@
1
- require 'active_support/core_ext/time'
2
-
3
- module Ensurance
4
- module Time
5
- def self.ensure(thing)
6
- case thing.class.name
7
- when "NilClass"
8
- thing
9
- when "Time"
10
- thing
11
- when "Date"
12
- thing.beginning_of_day
13
- when "Integer", "Float"
14
- ::Time.at(thing)
15
- when "String"
16
- if thing.to_i.to_s == thing
17
- ::Time.at(thing.to_i)
18
- elsif thing.to_f.to_s == thing
19
- ::Time.at(thing.to_f)
20
- else
21
- ::Time.parse(thing)
22
- end
23
- else
24
- if thing.respond_to?(:to_time)
25
- thing.to_time
26
- else
27
- raise ArgumentError.new("Unhandled Type for Time to ensure: #{thing.class}")
28
- end
29
- end
30
- end
31
- end
32
- end
33
-
34
- ::Time.include Ensurance::Time