type-enforcer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/type-enforcer/base.rb +60 -16
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69b0b10137e641702abe4e4400e00be63e8b0e10
4
- data.tar.gz: b0f90b58d9785ca514e704811f13bb89b46a2bf9
3
+ metadata.gz: a27db785362b083e806346c71fe4bda84d22d073
4
+ data.tar.gz: 6fc452f5a0e7d87dccb29f2f5e7ed2047028e894
5
5
  SHA512:
6
- metadata.gz: 2bd4bcff6ecf474a14881c9c231430eb49c3089b1f12b1cd2586edf45c2340c7fe17bf4cc5e69d9132a7edbc4a1cde291f1818d9ee3d2c315b69be9583c4a1d4
7
- data.tar.gz: d9d13c0c6b9154c84b2a4f755daa61f26877531db49cdbfc2575ca0bd6c7aa2937c6467a5e4ed60a9ca2861ebb11a97d5aae7e1a68f466885500b6daa8eb6e79
6
+ metadata.gz: bd204e0790cec27cb94be37028a1b3c05fe846602756207fa63d9a599ce835abf4bd90ae44f0eb6456abe561de165180d6389d5f0e2c4bd391408548186cdc06
7
+ data.tar.gz: 09857f101fc4ad5881975dcf82b98f80dfc0065e6fdb00c9e30bc9f5b0f788caf04c1c1b1e80dde01e1feb744ef906a888afd84b26aba3aae43ee4d247e8da93
@@ -1,23 +1,39 @@
1
1
  module TypeEnforcer
2
2
  class Error < StandardError; end
3
- class TypeError < StandardError; end
3
+ class NotFulfilledError < Error; end
4
+ class NotPresentError < Error; end
4
5
 
5
6
  refine Object do
6
- def enforce(type)
7
- case type
8
- when Symbol
9
- TypeEnforcer.try(type, self) || try(type) ? self : nil
7
+ def enforce(*args, &block)
8
+ options = TypeEnforcer.build_options(args, error: nil)
9
+
10
+ if block_given?
11
+ if try(*args, &block)
12
+ self
13
+ else
14
+ TypeEnforcer.raise_or_return(options[:error])
15
+ end
10
16
  else
11
- self.acts_as?(type) ? self.try(:convert_to, type) || self : nil
17
+ case type = args.first
18
+ when Symbol
19
+ if TypeEnforcer.try(type, self, *args[1..-1]) || try(*args)
20
+ self
21
+ else
22
+ TypeEnforcer.raise_or_return(options[:error])
23
+ end
24
+ else
25
+ if self.acts_as?(type)
26
+ self.try(:convert_to, *args) || self
27
+ else
28
+ TypeEnforcer.raise_or_return(options[:error])
29
+ end
30
+ end
12
31
  end
13
32
  end
14
33
  alias_method :e, :enforce
15
34
 
16
- def enforce!(type)
17
- enforced = enforce(type)
18
-
19
- raise TypeError if enforced.nil?
20
- enforced
35
+ def enforce!(*args, &block)
36
+ enforce(*args, {error: NotFulfilledError}, &block)
21
37
  end
22
38
  alias_method :e!, :enforce!
23
39
 
@@ -29,18 +45,46 @@ module TypeEnforcer
29
45
  !nil?
30
46
  end
31
47
 
32
- def present!
33
- raise TypeError if nil?
34
- self
48
+ def present!(error: NotPresentError)
49
+ unless present?
50
+ TypeEnforcer.raise_or_return(error)
51
+ else
52
+ self
53
+ end
35
54
  end
36
55
  alias_method :p!, :present!
37
56
 
38
- def try(method, *args)
39
- send(method, *args) if respond_to?(method)
57
+ def try(*args)
58
+ options = TypeEnforcer.build_options(args, rescues: StandardError)
59
+ begin
60
+ if block_given?
61
+ yield(self, *args)
62
+ else
63
+ send(*args)
64
+ end
65
+ rescue options[:rescues]
66
+ nil
67
+ end
40
68
  end
41
69
 
42
70
  def blank?
43
71
  nil? || try(:empty?)
44
72
  end
45
73
  end
74
+
75
+ def self.build_options(options, defaults)
76
+ if options.is_a?(Array)
77
+ options = options.last.is_a?(Hash) ? options.pop : {}
78
+ end
79
+
80
+ defaults.merge(options)
81
+ end
82
+
83
+ def self.raise_or_return(error)
84
+ if error.is_a?(Class) && error <= Exception
85
+ raise error
86
+ else
87
+ error
88
+ end
89
+ end
46
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type-enforcer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Lacy