type-enforcer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69b0b10137e641702abe4e4400e00be63e8b0e10
4
+ data.tar.gz: b0f90b58d9785ca514e704811f13bb89b46a2bf9
5
+ SHA512:
6
+ metadata.gz: 2bd4bcff6ecf474a14881c9c231430eb49c3089b1f12b1cd2586edf45c2340c7fe17bf4cc5e69d9132a7edbc4a1cde291f1818d9ee3d2c315b69be9583c4a1d4
7
+ data.tar.gz: d9d13c0c6b9154c84b2a4f755daa61f26877531db49cdbfc2575ca0bd6c7aa2937c6467a5e4ed60a9ca2861ebb11a97d5aae7e1a68f466885500b6daa8eb6e79
@@ -0,0 +1,46 @@
1
+ module TypeEnforcer
2
+ class Error < StandardError; end
3
+ class TypeError < StandardError; end
4
+
5
+ refine Object do
6
+ def enforce(type)
7
+ case type
8
+ when Symbol
9
+ TypeEnforcer.try(type, self) || try(type) ? self : nil
10
+ else
11
+ self.acts_as?(type) ? self.try(:convert_to, type) || self : nil
12
+ end
13
+ end
14
+ alias_method :e, :enforce
15
+
16
+ def enforce!(type)
17
+ enforced = enforce(type)
18
+
19
+ raise TypeError if enforced.nil?
20
+ enforced
21
+ end
22
+ alias_method :e!, :enforce!
23
+
24
+ def acts_as?(type)
25
+ type === self
26
+ end
27
+
28
+ def present?
29
+ !nil?
30
+ end
31
+
32
+ def present!
33
+ raise TypeError if nil?
34
+ self
35
+ end
36
+ alias_method :p!, :present!
37
+
38
+ def try(method, *args)
39
+ send(method, *args) if respond_to?(method)
40
+ end
41
+
42
+ def blank?
43
+ nil? || try(:empty?)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ module TypeEnforcer
2
+ refine String do
3
+ def numeric?
4
+ (/^\d+(\.\d+)?$/ =~ self).present?
5
+ end
6
+
7
+ def fixnum?
8
+ (/^\d+$/ =~ self).present?
9
+ end
10
+
11
+ def blank?
12
+ (/^\s*$/ =~ self).present?
13
+ end
14
+ end
15
+
16
+ refine Numeric do
17
+ # http://stackoverflow.com/a/1093707/1311454
18
+ def whole?
19
+ self % 1 == 0
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ require 'type-enforcer/base'
2
+ require 'type-enforcer/matchers'
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: type-enforcer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kyle Lacy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A way to ensure things are the types you want them to be
14
+ email: kylelacy@me.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/type-enforcer.rb
20
+ - lib/type-enforcer/base.rb
21
+ - lib/type-enforcer/matchers.rb
22
+ homepage: https://github.com/kylewlacy/type-enforcer
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: nil-b-gone
45
+ test_files: []
46
+ has_rdoc: