quack 0.0.1 → 0.0.2

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,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- Y2JjNmJkYzU1OWZlOGRhOTBmMzBkMDgyNTUxZTlmZjFlMmQzNzU2NA==
5
- data.tar.gz: !binary |-
6
- ZmIyMmRhNWFjZTAwNDM0ZmU4ZmM2MmE2NDE4ZjY4MDFkYThiNGZhYg==
2
+ SHA1:
3
+ metadata.gz: 41b54691b6c9da7f2e7e3845fcdb59d198112e51
4
+ data.tar.gz: d7ad2d6352067154f62d95bff8551f58415fc9cd
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- Y2QzNzNhZmY0Nzk0Y2NjOGU3NjhkYzRmOTQ0ZGVhYzg0MzVmM2E5MDY1OGQ2
10
- ZTEyZDljMzJjYjNmZGIyYjZlNTU2NDdmMTBjNzY5OTcwNjE4NjQxOThlOGEy
11
- OTEzNWRmZTE4NGY2Y2MwMGY0OTdiYTZkMjAxYWJlZTkxZjQ4ZGM=
12
- data.tar.gz: !binary |-
13
- ODdlZTIyNWE1ZjE4ZjM1ZTk4YTRkOTFkNjgyZDQzYzk2ZDQ2MjFmODI2Njkw
14
- MjNiZTNkNTEzMjIxZmYwM2MzNzI5MjIyZWIzMmVhYWViZDU1MGUzMWQ5NmNj
15
- NzMwYTFiYjJmZDY5ZjZiMmU4MzNmNDE1MzQyMDU5ZDhiM2IyNzY=
6
+ metadata.gz: 175ceab4331f79108bcfea7340871df4fa11414c5c9b7d0a74d969faa1d71528956ab1f768f9cbcbf1c1f99342ea8e31fbcd7a1dc3605533355fe0785ac336af
7
+ data.tar.gz: 665560693fdb34104082723afc9fb9b50564d772cd48142d9d3b8a320a69c7967cca9acb0162d1270f5533e20841fd6953c9752c3542b6d70ae921e6f50e9363
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .DS_Store
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Quack
2
2
 
3
- TODO: Write a gem description
3
+ Quack is a simple Ruby scalar type coercion library.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,94 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Quack is able to guess the following type categories and cast values to appropriate built-in type:
22
+
23
+ - `Integer`
24
+ - `Float`
25
+ - `Time`
26
+ - `Boolean`
27
+ - `Null`
28
+ - `String`
29
+
30
+ ### Integer
31
+
32
+ ```ruby
33
+ value = Quack("123")
34
+ value.class
35
+ #=> Quack::Types::Integer
36
+ value.to_coerced
37
+ #=> 123
38
+ value.to_coerced.class
39
+ #=> Fixnum
40
+ ```
41
+
42
+ ### Float
43
+
44
+ ```ruby
45
+ value = Quack("29.4")
46
+ value.class
47
+ #=> Quack::Types::Float
48
+ value.to_coerced
49
+ #=> 29.4
50
+ value.to_coerced.class
51
+ #=> Float
52
+ ```
53
+
54
+ ### Time
55
+
56
+ ```ruby
57
+ value = Quack("2014-03-22T03:00:00Z")
58
+ value.class
59
+ #=> Quack::Types::Time
60
+ value.to_coerced
61
+ #=> 2014-03-22T03:00:00+00:00
62
+ value.to_coerced.class
63
+ #=> Time
64
+ ```
65
+
66
+ ### Boolean
67
+
68
+ ```ruby
69
+ value = Quack("true")
70
+ value.class
71
+ #=> Quack::Types::Boolean
72
+ value.to_coerced
73
+ #=> true
74
+ value.to_coerced.class
75
+ #=> TrueClass
76
+
77
+ value = Quack("false")
78
+ value.class
79
+ #=> Quack::Types::Boolean
80
+ value.to_coerced
81
+ #=> false
82
+ value.to_coerced.class
83
+ #=> FalseClass
84
+ ```
85
+
86
+ ### Null
87
+
88
+ ```ruby
89
+ value = Quack(nil)
90
+ value.class
91
+ #=> Quack::Types::Null
92
+ value.to_coerced
93
+ #=> nil
94
+ value.to_coerced.class
95
+ #=> NilClass
96
+ ```
97
+
98
+ ### String
99
+
100
+ ```ruby
101
+ value = Quack("foo")
102
+ value.class
103
+ #=> Quack::Types::String
104
+ value.to_coerced
105
+ #=> foo
106
+ value.to_coerced.class
107
+ #=> String
108
+ ```
22
109
 
23
110
  ## Contributing
24
111
 
@@ -0,0 +1,21 @@
1
+ module Quack
2
+ class Guesser
3
+ attr_reader :value
4
+
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def guess
10
+ klass.new(value)
11
+ end
12
+
13
+ private
14
+
15
+ def klass
16
+ Quack::Types.select { |t|
17
+ t.matches?(value)
18
+ }.first
19
+ end
20
+ end
21
+ end
data/lib/quack/type.rb CHANGED
@@ -24,6 +24,10 @@ module Quack
24
24
  raise NotImplementedError
25
25
  end
26
26
 
27
+ def type_matches?(other)
28
+ self.class == other.class
29
+ end
30
+
27
31
  def to_s
28
32
  to_coerced.to_s
29
33
  end
data/lib/quack/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Quack
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/quack.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  require "quack/version"
2
2
  require "quack/types"
3
- require "quack/value"
3
+ require "quack/guesser"
4
4
 
5
5
  module Quack
6
6
  class ParseError < StandardError; end
7
7
  end
8
+
9
+ def Quack(value)
10
+ Quack::Guesser.new(value).guess
11
+ end
@@ -0,0 +1,102 @@
1
+ require "test_helper"
2
+
3
+ describe Quack::Guesser do
4
+ describe "given a string integer" do
5
+ let(:value) { "123" }
6
+ let(:subject) { Quack::Guesser.new(value) }
7
+
8
+ it "should have Integer type" do
9
+ subject.guess.class.must_equal(Quack::Types::Integer)
10
+ end
11
+ end
12
+
13
+ describe "given a Fixnum integer" do
14
+ let(:value) { 123 }
15
+ let(:subject) { Quack::Guesser.new(value) }
16
+
17
+ it "should have Integer type" do
18
+ subject.guess.class.must_equal(Quack::Types::Integer)
19
+ end
20
+ end
21
+
22
+ describe "given a string float" do
23
+ let(:value) { "123.4" }
24
+ let(:subject) { Quack::Guesser.new(value) }
25
+
26
+ it "should have Float type" do
27
+ subject.guess.class.must_equal(Quack::Types::Float)
28
+ end
29
+ end
30
+
31
+ describe "given a Float" do
32
+ let(:value) { 123.4 }
33
+ let(:subject) { Quack::Guesser.new(value) }
34
+
35
+ it "should have Float type" do
36
+ subject.guess.class.must_equal(Quack::Types::Float)
37
+ end
38
+ end
39
+
40
+ describe "given a 'true' string" do
41
+ let(:value) { "true" }
42
+ let(:subject) { Quack::Guesser.new(value) }
43
+
44
+ it "should have Boolean type" do
45
+ subject.guess.class.must_equal(Quack::Types::Boolean)
46
+ end
47
+ end
48
+
49
+ describe "given a 'false' string" do
50
+ let(:value) { "false" }
51
+ let(:subject) { Quack::Guesser.new(value) }
52
+
53
+ it "should have Boolean type" do
54
+ subject.guess.class.must_equal(Quack::Types::Boolean)
55
+ end
56
+ end
57
+
58
+ describe "given true" do
59
+ let(:value) { true }
60
+ let(:subject) { Quack::Guesser.new(value) }
61
+
62
+ it "should have Boolean type" do
63
+ subject.guess.class.must_equal(Quack::Types::Boolean)
64
+ end
65
+ end
66
+
67
+ describe "given false" do
68
+ let(:value) { false }
69
+ let(:subject) { Quack::Guesser.new(value) }
70
+
71
+ it "should have Boolean type" do
72
+ subject.guess.class.must_equal(Quack::Types::Boolean)
73
+ end
74
+ end
75
+
76
+ describe "given an ISO 8061 UTC date" do
77
+ let(:value) { "2014-03-22T03:00:00Z" }
78
+ let(:subject) { Quack::Guesser.new(value) }
79
+
80
+ it "should have Time type" do
81
+ subject.guess.class.must_equal(Quack::Types::Time)
82
+ end
83
+ end
84
+
85
+ describe "given an ISO 8061 non-UTC date" do
86
+ let(:value) { "2014-03-22T03:00:00-07:00" }
87
+ let(:subject) { Quack::Guesser.new(value) }
88
+
89
+ it "should have Time type" do
90
+ subject.guess.class.must_equal(Quack::Types::Time)
91
+ end
92
+ end
93
+
94
+ describe "given an random string" do
95
+ let(:value) { "foo123" }
96
+ let(:subject) { Quack::Guesser.new(value) }
97
+
98
+ it "should have String type" do
99
+ subject.guess.class.must_equal(Quack::Types::String)
100
+ end
101
+ end
102
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,10 @@
1
+ require "test_helper"
2
+
3
+ describe Quack do
4
+ let(:value) { "123" }
5
+ it "should proxy to the guesser" do
6
+ type = Quack(value)
7
+ type.class.must_equal(Quack::Types::Integer)
8
+ type.to_coerced.must_equal(123)
9
+ end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quack
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
  - Derrick Reimer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-17 00:00:00.000000000 Z
11
+ date: 2014-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,14 +28,14 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: A simple type coercion library
@@ -51,6 +51,7 @@ files:
51
51
  - README.md
52
52
  - Rakefile
53
53
  - lib/quack.rb
54
+ - lib/quack/guesser.rb
54
55
  - lib/quack/type.rb
55
56
  - lib/quack/types.rb
56
57
  - lib/quack/types/boolean.rb
@@ -59,17 +60,17 @@ files:
59
60
  - lib/quack/types/null.rb
60
61
  - lib/quack/types/string.rb
61
62
  - lib/quack/types/time.rb
62
- - lib/quack/value.rb
63
63
  - lib/quack/version.rb
64
64
  - quack.gemspec
65
+ - spec/quack/guesser_spec.rb
66
+ - spec/quack/types/boolean_spec.rb
67
+ - spec/quack/types/float_spec.rb
68
+ - spec/quack/types/integer_spec.rb
69
+ - spec/quack/types/null_spec.rb
70
+ - spec/quack/types/string_spec.rb
71
+ - spec/quack/types/time_spec.rb
72
+ - spec/quack_spec.rb
65
73
  - spec/test_helper.rb
66
- - spec/types/boolean_spec.rb
67
- - spec/types/float_spec.rb
68
- - spec/types/integer_spec.rb
69
- - spec/types/null_spec.rb
70
- - spec/types/string_spec.rb
71
- - spec/types/time_spec.rb
72
- - spec/value_spec.rb
73
74
  homepage: https://github.com/djreimer/quack
74
75
  licenses:
75
76
  - MIT
@@ -80,26 +81,27 @@ require_paths:
80
81
  - lib
81
82
  required_ruby_version: !ruby/object:Gem::Requirement
82
83
  requirements:
83
- - - ! '>='
84
+ - - '>='
84
85
  - !ruby/object:Gem::Version
85
86
  version: '0'
86
87
  required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  requirements:
88
- - - ! '>='
89
+ - - '>='
89
90
  - !ruby/object:Gem::Version
90
91
  version: '0'
91
92
  requirements: []
92
93
  rubyforge_project:
93
- rubygems_version: 2.1.10
94
+ rubygems_version: 2.0.3
94
95
  signing_key:
95
96
  specification_version: 4
96
97
  summary: Quack is a simple type coercion library for scalar Ruby objects
97
98
  test_files:
99
+ - spec/quack/guesser_spec.rb
100
+ - spec/quack/types/boolean_spec.rb
101
+ - spec/quack/types/float_spec.rb
102
+ - spec/quack/types/integer_spec.rb
103
+ - spec/quack/types/null_spec.rb
104
+ - spec/quack/types/string_spec.rb
105
+ - spec/quack/types/time_spec.rb
106
+ - spec/quack_spec.rb
98
107
  - spec/test_helper.rb
99
- - spec/types/boolean_spec.rb
100
- - spec/types/float_spec.rb
101
- - spec/types/integer_spec.rb
102
- - spec/types/null_spec.rb
103
- - spec/types/string_spec.rb
104
- - spec/types/time_spec.rb
105
- - spec/value_spec.rb
data/lib/quack/value.rb DELETED
@@ -1,21 +0,0 @@
1
- require "forwardable"
2
-
3
- module Quack
4
- class Value
5
- extend Forwardable
6
- attr_reader :value, :type_class, :type_instance
7
-
8
- delegate :to_coerced => :type_instance
9
- delegate :to_s => :type_instance
10
-
11
- def initialize(value)
12
- @value = value
13
- @type_class = Quack::Types.select { |t| t.matches?(value) }.first
14
- @type_instance = type_class.new(value)
15
- end
16
-
17
- def type_matches?(other)
18
- type_class == other.type_class
19
- end
20
- end
21
- end
data/spec/value_spec.rb DELETED
@@ -1,148 +0,0 @@
1
- require "test_helper"
2
-
3
- describe Quack::Value do
4
- describe "given a string integer" do
5
- let(:value) { "123" }
6
- let(:subject) { Quack::Value.new(value) }
7
-
8
- it "should convert to a Fixnum" do
9
- subject.to_coerced.must_equal(123)
10
- end
11
-
12
- it "should have Integer type" do
13
- subject.type_class.must_equal(Quack::Types::Integer)
14
- end
15
- end
16
-
17
- describe "given a Fixnum integer" do
18
- let(:value) { 123 }
19
- let(:subject) { Quack::Value.new(value) }
20
-
21
- it "should return the original value" do
22
- subject.to_coerced.must_equal(123)
23
- end
24
-
25
- it "should have Integer type" do
26
- subject.type_class.must_equal(Quack::Types::Integer)
27
- end
28
- end
29
-
30
- describe "given a string float" do
31
- let(:value) { "123.4" }
32
- let(:subject) { Quack::Value.new(value) }
33
-
34
- it "should convert to a Float" do
35
- subject.to_coerced.must_equal(123.4)
36
- end
37
-
38
- it "should have Float type" do
39
- subject.type_class.must_equal(Quack::Types::Float)
40
- end
41
- end
42
-
43
- describe "given a Float" do
44
- let(:value) { 123.4 }
45
- let(:subject) { Quack::Value.new(value) }
46
-
47
- it "should return the original value" do
48
- subject.to_coerced.must_equal(123.4)
49
- end
50
-
51
- it "should have Float type" do
52
- subject.type_class.must_equal(Quack::Types::Float)
53
- end
54
- end
55
-
56
- describe "given a 'true' string" do
57
- let(:value) { "true" }
58
- let(:subject) { Quack::Value.new(value) }
59
-
60
- it "should return true" do
61
- subject.to_coerced.must_equal(true)
62
- end
63
-
64
- it "should have Boolean type" do
65
- subject.type_class.must_equal(Quack::Types::Boolean)
66
- end
67
- end
68
-
69
- describe "given a 'false' string" do
70
- let(:value) { "false" }
71
- let(:subject) { Quack::Value.new(value) }
72
-
73
- it "should return false" do
74
- subject.to_coerced.must_equal(false)
75
- end
76
-
77
- it "should have Boolean type" do
78
- subject.type_class.must_equal(Quack::Types::Boolean)
79
- end
80
- end
81
-
82
- describe "given true" do
83
- let(:value) { true }
84
- let(:subject) { Quack::Value.new(value) }
85
-
86
- it "should return true" do
87
- subject.to_coerced.must_equal(true)
88
- end
89
-
90
- it "should have Boolean type" do
91
- subject.type_class.must_equal(Quack::Types::Boolean)
92
- end
93
- end
94
-
95
- describe "given false" do
96
- let(:value) { false }
97
- let(:subject) { Quack::Value.new(value) }
98
-
99
- it "should return false" do
100
- subject.to_coerced.must_equal(false)
101
- end
102
-
103
- it "should have Boolean type" do
104
- subject.type_class.must_equal(Quack::Types::Boolean)
105
- end
106
- end
107
-
108
- describe "given an ISO 8061 UTC date" do
109
- let(:value) { "2014-03-22T03:00:00Z" }
110
- let(:subject) { Quack::Value.new(value) }
111
-
112
- it "should return the correct Time object" do
113
- expected = Time.utc(2014, 3, 22, 3, 0, 0)
114
- subject.to_coerced.must_equal(expected)
115
- end
116
-
117
- it "should have Time type" do
118
- subject.type_class.must_equal(Quack::Types::Time)
119
- end
120
- end
121
-
122
- describe "given an ISO 8061 non-UTC date" do
123
- let(:value) { "2014-03-22T03:00:00-07:00" }
124
- let(:subject) { Quack::Value.new(value) }
125
-
126
- it "should return the correct Time object" do
127
- expected = Time.new(2014, 3, 22, 3, 0, 0, "-07:00")
128
- subject.to_coerced.must_equal(expected)
129
- end
130
-
131
- it "should have Time type" do
132
- subject.type_class.must_equal(Quack::Types::Time)
133
- end
134
- end
135
-
136
- describe "given an random string" do
137
- let(:value) { "foo123" }
138
- let(:subject) { Quack::Value.new(value) }
139
-
140
- it "should return the string" do
141
- subject.to_coerced.must_equal(value)
142
- end
143
-
144
- it "should have String type" do
145
- subject.type_class.must_equal(Quack::Types::String)
146
- end
147
- end
148
- end