quack 0.0.1
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 +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +12 -0
- data/lib/quack/type.rb +31 -0
- data/lib/quack/types/boolean.rb +17 -0
- data/lib/quack/types/float.rb +23 -0
- data/lib/quack/types/integer.rb +23 -0
- data/lib/quack/types/null.rb +17 -0
- data/lib/quack/types/string.rb +17 -0
- data/lib/quack/types/time.rb +48 -0
- data/lib/quack/types.rb +26 -0
- data/lib/quack/value.rb +21 -0
- data/lib/quack/version.rb +3 -0
- data/lib/quack.rb +7 -0
- data/quack.gemspec +23 -0
- data/spec/test_helper.rb +5 -0
- data/spec/types/boolean_spec.rb +47 -0
- data/spec/types/float_spec.rb +34 -0
- data/spec/types/integer_spec.rb +45 -0
- data/spec/types/null_spec.rb +20 -0
- data/spec/types/string_spec.rb +16 -0
- data/spec/types/time_spec.rb +54 -0
- data/spec/value_spec.rb +148 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Y2JjNmJkYzU1OWZlOGRhOTBmMzBkMDgyNTUxZTlmZjFlMmQzNzU2NA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZmIyMmRhNWFjZTAwNDM0ZmU4ZmM2MmE2NDE4ZjY4MDFkYThiNGZhYg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Y2QzNzNhZmY0Nzk0Y2NjOGU3NjhkYzRmOTQ0ZGVhYzg0MzVmM2E5MDY1OGQ2
|
10
|
+
ZTEyZDljMzJjYjNmZGIyYjZlNTU2NDdmMTBjNzY5OTcwNjE4NjQxOThlOGEy
|
11
|
+
OTEzNWRmZTE4NGY2Y2MwMGY0OTdiYTZkMjAxYWJlZTkxZjQ4ZGM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODdlZTIyNWE1ZjE4ZjM1ZTk4YTRkOTFkNjgyZDQzYzk2ZDQ2MjFmODI2Njkw
|
14
|
+
MjNiZTNkNTEzMjIxZmYwM2MzNzI5MjIyZWIzMmVhYWViZDU1MGUzMWQ5NmNj
|
15
|
+
NzMwYTFiYjJmZDY5ZjZiMmU4MzNmNDE1MzQyMDU5ZDhiM2IyNzY=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Derrick Reimer
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Quack
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'quack'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install quack
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/quack/type.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Quack
|
2
|
+
class Type
|
3
|
+
attr_reader :value
|
4
|
+
|
5
|
+
def initialize(value)
|
6
|
+
@value = value
|
7
|
+
end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def built_in_types
|
11
|
+
[]
|
12
|
+
end
|
13
|
+
|
14
|
+
def already_coerced?(value)
|
15
|
+
built_in_types.include?(value.class)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def already_coerced?
|
20
|
+
self.class.already_coerced?(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_coerced
|
24
|
+
raise NotImplementedError
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
to_coerced.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "quack/type"
|
2
|
+
|
3
|
+
module Quack
|
4
|
+
module Types
|
5
|
+
class Boolean < Quack::Type
|
6
|
+
class << self
|
7
|
+
def matches?(value)
|
8
|
+
value.to_s == "true" || value.to_s == "false"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_coerced
|
13
|
+
value.to_s == "true"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "quack/type"
|
2
|
+
|
3
|
+
module Quack
|
4
|
+
module Types
|
5
|
+
class Float < Quack::Type
|
6
|
+
PATTERN = /\A\d+\.\d*\z/
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def built_in_types
|
10
|
+
[::Float]
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(value)
|
14
|
+
already_coerced?(value) || !!(value.to_s.strip =~ PATTERN)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_coerced
|
19
|
+
already_coerced? ? value : value.to_f
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "quack/type"
|
2
|
+
|
3
|
+
module Quack
|
4
|
+
module Types
|
5
|
+
class Integer < Quack::Type
|
6
|
+
PATTERN = /\A\d+\z/
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def built_in_types
|
10
|
+
[Fixnum, Bignum]
|
11
|
+
end
|
12
|
+
|
13
|
+
def matches?(value)
|
14
|
+
already_coerced?(value) || !!(value.to_s.strip =~ PATTERN)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_coerced
|
19
|
+
already_coerced? ? value : value.to_i
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "time"
|
2
|
+
require "quack/type"
|
3
|
+
|
4
|
+
module Quack
|
5
|
+
module Types
|
6
|
+
class Time < Quack::Type
|
7
|
+
ISO_8601 = /
|
8
|
+
(?<year>\d{4})-
|
9
|
+
(?<month>\d{2})-
|
10
|
+
(?<day>\d{2})T
|
11
|
+
(?<hour>\d{2}):
|
12
|
+
(?<minute>\d{2}):
|
13
|
+
(?<second>\d{2})
|
14
|
+
(?<offset>Z|(\+|-)(\d{2}):(\d{2}))
|
15
|
+
/x
|
16
|
+
|
17
|
+
UTC = "+00:00"
|
18
|
+
|
19
|
+
class << self
|
20
|
+
def built_in_types
|
21
|
+
[::Time]
|
22
|
+
end
|
23
|
+
|
24
|
+
def matches?(value)
|
25
|
+
already_coerced?(value) || !!(value.to_s =~ ISO_8601)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse_offset(offset)
|
30
|
+
offset == "Z" ? UTC : offset
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_coerced
|
34
|
+
return value if already_coerced?
|
35
|
+
parts = value.to_s.scan(ISO_8601).flatten
|
36
|
+
offset = parse_offset(parts.pop)
|
37
|
+
parts = parts.map(&:to_i) << offset
|
38
|
+
::Time.new(*parts)
|
39
|
+
rescue => ex
|
40
|
+
raise ParseError.new(ex.message)
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_s
|
44
|
+
to_coerced.iso8601
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/quack/types.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "quack/types/null"
|
2
|
+
require "quack/types/integer"
|
3
|
+
require "quack/types/float"
|
4
|
+
require "quack/types/boolean"
|
5
|
+
require "quack/types/time"
|
6
|
+
require "quack/types/string"
|
7
|
+
|
8
|
+
module Quack
|
9
|
+
module Types
|
10
|
+
extend Enumerable
|
11
|
+
|
12
|
+
TYPES = [
|
13
|
+
Quack::Types::Null,
|
14
|
+
Quack::Types::Integer,
|
15
|
+
Quack::Types::Float,
|
16
|
+
Quack::Types::Boolean,
|
17
|
+
Quack::Types::Time,
|
18
|
+
# ... insert new types here ...
|
19
|
+
Quack::Types::String # must be last!
|
20
|
+
]
|
21
|
+
|
22
|
+
def self.each
|
23
|
+
TYPES.each { |t| yield(t) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/quack/value.rb
ADDED
@@ -0,0 +1,21 @@
|
|
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/lib/quack.rb
ADDED
data/quack.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'quack/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "quack"
|
8
|
+
spec.version = Quack::VERSION
|
9
|
+
spec.authors = ["Derrick Reimer"]
|
10
|
+
spec.email = ["derrickreimer@gmail.com"]
|
11
|
+
spec.description = %q{A simple type coercion library}
|
12
|
+
spec.summary = %q{Quack is a simple type coercion library for scalar Ruby objects}
|
13
|
+
spec.homepage = "https://github.com/djreimer/quack"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/spec/test_helper.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe Quack::Types::Boolean do
|
4
|
+
describe ".matches?" do
|
5
|
+
it "should be true for true" do
|
6
|
+
Quack::Types::Boolean.matches?(true).must_equal(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be true for false" do
|
10
|
+
Quack::Types::Boolean.matches?(false).must_equal(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be true for 'true' string" do
|
14
|
+
Quack::Types::Boolean.matches?("true").must_equal(true)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be true for 'false' string" do
|
18
|
+
Quack::Types::Boolean.matches?("false").must_equal(true)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should be false for aritrary strings" do
|
22
|
+
Quack::Types::Boolean.matches?("foo").must_equal(false)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#to_coerced" do
|
27
|
+
it "should return true if original value is true" do
|
28
|
+
type = Quack::Types::Boolean.new(true)
|
29
|
+
type.to_coerced.must_equal(true)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return true if original value is true" do
|
33
|
+
type = Quack::Types::Boolean.new(false)
|
34
|
+
type.to_coerced.must_equal(false)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should cast 'true' string to true" do
|
38
|
+
type = Quack::Types::Boolean.new("true")
|
39
|
+
type.to_coerced.must_equal(true)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should cast 'false' string to true" do
|
43
|
+
type = Quack::Types::Boolean.new("false")
|
44
|
+
type.to_coerced.must_equal(false)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe Quack::Types::Float do
|
4
|
+
describe ".matches?" do
|
5
|
+
it "should be true for float strings" do
|
6
|
+
Quack::Types::Float.matches?("123.4").must_equal(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be true for floats" do
|
10
|
+
Quack::Types::Float.matches?(123.4).must_equal(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be false for integer strings" do
|
14
|
+
Quack::Types::Float.matches?("123").must_equal(false)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be false for integers" do
|
18
|
+
Quack::Types::Float.matches?(123).must_equal(false)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#to_coerced" do
|
23
|
+
it "should return original value if its a Float" do
|
24
|
+
type = Quack::Types::Float.new(2.4)
|
25
|
+
type.to_coerced.must_equal(2.4)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should cast float strings to Float" do
|
29
|
+
type = Quack::Types::Float.new("123.4")
|
30
|
+
type.to_coerced.must_equal(123.4)
|
31
|
+
type.to_coerced.class.must_equal(Float)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe Quack::Types::Integer do
|
4
|
+
describe ".matches?" do
|
5
|
+
it "should be true for integer strings" do
|
6
|
+
Quack::Types::Integer.matches?("123").must_equal(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be true for Fixnum" do
|
10
|
+
Quack::Types::Integer.matches?(123).must_equal(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be true for Bignum" do
|
14
|
+
num = 232305722798259244150093798251441
|
15
|
+
Quack::Types::Integer.matches?(num).must_equal(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be false for float strings" do
|
19
|
+
Quack::Types::Integer.matches?("123.4").must_equal(false)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be false for floats" do
|
23
|
+
Quack::Types::Integer.matches?(123.4).must_equal(false)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#to_coerced" do
|
28
|
+
it "should return original value if its a Fixnum" do
|
29
|
+
type = Quack::Types::Integer.new(2)
|
30
|
+
type.to_coerced.must_equal(2)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return original value if its a Bignum" do
|
34
|
+
num = 232305722798259244150093798251441
|
35
|
+
type = Quack::Types::Integer.new(num)
|
36
|
+
type.to_coerced.must_equal(num)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should cast integer strings to Fixnum" do
|
40
|
+
type = Quack::Types::Integer.new("123")
|
41
|
+
type.to_coerced.must_equal(123)
|
42
|
+
type.to_coerced.class.must_equal(Fixnum)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe Quack::Types::Null do
|
4
|
+
describe ".matches?" do
|
5
|
+
it "should be true for nil" do
|
6
|
+
Quack::Types::Null.matches?(nil).must_equal(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be false for non-nil" do
|
10
|
+
Quack::Types::Null.matches?("foo").must_equal(false)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#to_coerced" do
|
15
|
+
it "should return nil" do
|
16
|
+
type = Quack::Types::Null.new(nil)
|
17
|
+
type.to_coerced.must_equal(nil)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe Quack::Types::String do
|
4
|
+
describe ".matches?" do
|
5
|
+
it "should be true" do
|
6
|
+
Quack::Types::String.matches?("foo").must_equal(true)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#to_coerced" do
|
11
|
+
it "should cast integers to strings" do
|
12
|
+
type = Quack::Types::String.new(123)
|
13
|
+
type.to_coerced.must_equal("123")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
describe Quack::Types::Time do
|
4
|
+
describe ".matches?" do
|
5
|
+
it "should be true for Time objects" do
|
6
|
+
Quack::Types::Time.matches?(Time.new(2014, 3, 22)).must_equal(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should be true for ISO 8601 UTC times" do
|
10
|
+
Quack::Types::Time.matches?("2014-03-22T03:00:00Z").must_equal(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be true for ISO 8601 non-UTC times" do
|
14
|
+
Quack::Types::Time.matches?("2014-03-22T03:00:00-08:00").must_equal(true)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be false for non ISO 8601 dates" do
|
18
|
+
Quack::Types::Time.matches?("3/22/2014").must_equal(false)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#to_coerced" do
|
23
|
+
it "should cast ISO 8601 UTC times" do
|
24
|
+
type = Quack::Types::Time.new("2014-03-22T03:00:00Z")
|
25
|
+
expected = Time.new(2014, 3, 22, 3, 0, 0, "+00:00")
|
26
|
+
type.to_coerced.must_equal(expected)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should cast ISO 8601 non-UTC times" do
|
30
|
+
type = Quack::Types::Time.new("2014-03-22T03:00:00-07:00")
|
31
|
+
expected = Time.new(2014, 3, 22, 3, 0, 0, "-07:00")
|
32
|
+
type.to_coerced.must_equal(expected)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should raise a ParseError for invalid dates" do
|
36
|
+
type = Quack::Types::Time.new("2014-03-22")
|
37
|
+
proc { type.to_coerced }.must_raise(Quack::ParseError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#to_s" do
|
42
|
+
it "should cast UTC times to ISO 8601 strings" do
|
43
|
+
time = Time.utc(2014, 3, 22, 3, 10, 12)
|
44
|
+
type = Quack::Types::Time.new(time)
|
45
|
+
type.to_s.must_equal("2014-03-22T03:10:12Z")
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should cast non-UTC times to ISO 8601 strings" do
|
49
|
+
time = Time.new(2014, 3, 22, 3, 10, 12, "-07:00")
|
50
|
+
type = Quack::Types::Time.new(time)
|
51
|
+
type.to_s.must_equal("2014-03-22T03:10:12-07:00")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/value_spec.rb
ADDED
@@ -0,0 +1,148 @@
|
|
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
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quack
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derrick Reimer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A simple type coercion library
|
42
|
+
email:
|
43
|
+
- derrickreimer@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/quack.rb
|
54
|
+
- lib/quack/type.rb
|
55
|
+
- lib/quack/types.rb
|
56
|
+
- lib/quack/types/boolean.rb
|
57
|
+
- lib/quack/types/float.rb
|
58
|
+
- lib/quack/types/integer.rb
|
59
|
+
- lib/quack/types/null.rb
|
60
|
+
- lib/quack/types/string.rb
|
61
|
+
- lib/quack/types/time.rb
|
62
|
+
- lib/quack/value.rb
|
63
|
+
- lib/quack/version.rb
|
64
|
+
- quack.gemspec
|
65
|
+
- 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
|
+
homepage: https://github.com/djreimer/quack
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.1.10
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Quack is a simple type coercion library for scalar Ruby objects
|
97
|
+
test_files:
|
98
|
+
- 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
|