mugatu 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +3 -0
- data/Rakefile +5 -0
- data/lib/mugatu.rb +4 -0
- data/lib/mugatu/attribute.rb +25 -0
- data/lib/mugatu/attribute_type.rb +25 -0
- data/lib/mugatu/entity.rb +40 -0
- data/lib/mugatu/types/array.rb +5 -0
- data/lib/mugatu/types/boolean.rb +5 -0
- data/lib/mugatu/types/boolean/false.rb +5 -0
- data/lib/mugatu/types/boolean/true.rb +5 -0
- data/lib/mugatu/types/hash.rb +5 -0
- data/lib/mugatu/types/nil.rb +5 -0
- data/lib/mugatu/types/numeric.rb +5 -0
- data/lib/mugatu/types/numeric/float.rb +5 -0
- data/lib/mugatu/types/numeric/integer.rb +5 -0
- data/lib/mugatu/types/string.rb +17 -0
- data/lib/mugatu/version.rb +3 -0
- data/mugatu.gemspec +12 -0
- data/spec/mugatu_spec.rb +71 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/types/array_spec.rb +18 -0
- data/spec/types/boolean/false_spec.rb +7 -0
- data/spec/types/boolean/true_spec.rb +7 -0
- data/spec/types/boolean_spec.rb +18 -0
- data/spec/types/hash_spec.rb +18 -0
- data/spec/types/nil_spec.rb +7 -0
- data/spec/types/numeric/float_spec.rb +28 -0
- data/spec/types/numeric/integer_spec.rb +28 -0
- data/spec/types/string_spec.rb +40 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b41d7bdc2af41ccf1b7b86e6e55009bd5a1ef422
|
4
|
+
data.tar.gz: ca50175753086c35746a671e9e53adffadbc563e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 40230ee76ee06abf95ddd0f29582ccb7c709202403cd252b81c088ff8371e119506d7e5e0a298fde237304847ad4fd8e2819f6d3189f5222d1d57c316a6dad7f
|
7
|
+
data.tar.gz: d59de7ee301107e172adfd32a6c2dcd5392b4c70454ddace9fb4995a0bc76b6ebc7771cd970228d6b23791c01a11c81b30898bd7513422c32d32eba4f392c500
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Steven Weiss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/mugatu.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mugatu
|
2
|
+
class Attribute
|
3
|
+
attr_reader :key, :type, :value
|
4
|
+
|
5
|
+
def initialize(key, type, options = {})
|
6
|
+
@key = key
|
7
|
+
@type = type
|
8
|
+
self.value = options[:default_value] # nil when none is set.
|
9
|
+
end
|
10
|
+
|
11
|
+
def value=(new_value)
|
12
|
+
@value = cast(new_value)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def cast(value)
|
18
|
+
if value.is_a?(type) || value.nil?
|
19
|
+
value
|
20
|
+
else
|
21
|
+
type.cast(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mugatu
|
2
|
+
module AttributeType
|
3
|
+
def set_cast_with(sym)
|
4
|
+
@cast_with = sym
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_cast_with
|
8
|
+
@cast_with
|
9
|
+
end
|
10
|
+
|
11
|
+
def cast(value)
|
12
|
+
if value.respond_to?(get_cast_with)
|
13
|
+
value.send(get_cast_with)
|
14
|
+
else
|
15
|
+
raise_cast_error(value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def raise_cast_error(value)
|
20
|
+
raise TypeError, "cannot cast #{value} into #{name} (TypeError)"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Dir[File.dirname(__FILE__) + '/types/**/*.rb'].each {|file| require file }
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Mugatu
|
2
|
+
class Entity
|
3
|
+
def self.attribute(key, type, options = {})
|
4
|
+
attribute = Attribute.new(key, type, options)
|
5
|
+
|
6
|
+
define_attribute_reader(attribute)
|
7
|
+
define_attribute_writer(attribute)
|
8
|
+
|
9
|
+
attributes << attribute
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.attributes
|
13
|
+
@attributes ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(attributes = {})
|
17
|
+
self.attributes.each do |a|
|
18
|
+
send("#{a.key}=", attributes[a.key]) if attributes.key?(a.key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def attributes
|
23
|
+
self.class.attributes
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def self.define_attribute_reader(attribute)
|
29
|
+
define_method attribute.key do
|
30
|
+
attribute.value
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.define_attribute_writer(attribute)
|
35
|
+
define_method "#{attribute.key}=" do |value|
|
36
|
+
attribute.value = value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class String
|
2
|
+
extend Mugatu::AttributeType
|
3
|
+
|
4
|
+
TRUTHY = ['t', 'true', 'on', 'y', 'yes', '1'].freeze
|
5
|
+
FALSY = ['f', 'false', 'off', 'n', 'no', '0'].freeze
|
6
|
+
|
7
|
+
set_cast_with :to_s
|
8
|
+
|
9
|
+
def to_b
|
10
|
+
value = downcase
|
11
|
+
|
12
|
+
return true if TRUTHY.include?(value)
|
13
|
+
return false if FALSY.include?(value)
|
14
|
+
|
15
|
+
self.class.raise_cast_error(self)
|
16
|
+
end
|
17
|
+
end
|
data/mugatu.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require_relative './lib/mugatu/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'mugatu'
|
5
|
+
s.summary = 'Mugatu'
|
6
|
+
s.version = Mugatu::VERSION
|
7
|
+
s.authors = ['Steve Weiss']
|
8
|
+
s.email = ['weissst@mail.gvsu.edu']
|
9
|
+
s.homepage = 'https://github.com/sirscriptalot/mugatu'
|
10
|
+
s.license = 'MIT'
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
end
|
data/spec/mugatu_spec.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require_relative './spec_helper'
|
2
|
+
|
3
|
+
class Entity < Mugatu::Entity
|
4
|
+
attribute :foo, String, default_value: 'bar'
|
5
|
+
end
|
6
|
+
|
7
|
+
describe 'Mugatu::Entity' do
|
8
|
+
before do
|
9
|
+
@entity = Entity.new
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#self.attributes' do
|
13
|
+
context 'no attributes set' do
|
14
|
+
it 'initializes as an empty array' do
|
15
|
+
assert Mugatu::Entity.attributes.empty?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'attributes set' do
|
20
|
+
it 'returns the @attributes array' do
|
21
|
+
refute Entity.attributes.empty?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#self.attribute' do
|
27
|
+
it 'defines a reader method for attribute.key' do
|
28
|
+
assert @entity.respond_to?(:foo)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'defines a writer method for attribute.key' do
|
32
|
+
assert @entity.respond_to?(:foo=)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#initialize' do
|
37
|
+
it 'sets default values' do
|
38
|
+
assert @entity.foo = 'bar'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#attributes' do
|
43
|
+
it 'returns the class\' @attributes' do
|
44
|
+
assert @entity.attributes == @entity.class.attributes
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'Mugatu::Attribute' do
|
50
|
+
before do
|
51
|
+
@key = :foo
|
52
|
+
@default_value = 'bar'
|
53
|
+
@attribute = Mugatu::Attribute.new(@key, String, default_value: @default_value)
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#initialize' do
|
57
|
+
describe 'options[:default_value]' do
|
58
|
+
it 'sets initial @value' do
|
59
|
+
assert @attribute.value == @default_value
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#value=' do
|
65
|
+
it 'allows nil values' do
|
66
|
+
@attribute.value = nil
|
67
|
+
|
68
|
+
assert @attribute.value.nil?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
describe 'Array' do
|
2
|
+
describe '#self.cast' do
|
3
|
+
it 'casts value to Array' do
|
4
|
+
assert Array.cast([]) == []
|
5
|
+
assert Array.cast({foo: :bar}) == [[:foo, :bar]]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'to_a is not implemented' do
|
10
|
+
it 'raises a TypeError' do
|
11
|
+
assert_raises(TypeError) { assert Array.cast('array') }
|
12
|
+
assert_raises(TypeError) { assert Array.cast(1) }
|
13
|
+
assert_raises(TypeError) { assert Array.cast(1.0) }
|
14
|
+
assert_raises(TypeError) { assert Array.cast(true) }
|
15
|
+
assert_raises(TypeError) { assert Array.cast(false) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
describe 'Boolean' do
|
2
|
+
describe '#self.cast' do
|
3
|
+
it 'casts value to a Boolean' do
|
4
|
+
assert Hash.cast([[:foo, :bar]]) == {foo: :bar}
|
5
|
+
assert Hash.cast({foo: :bar}) == {foo: :bar}
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'to_h is not implemented' do
|
10
|
+
it 'raises a TypeError' do
|
11
|
+
assert_raises(TypeError) { assert Hash.cast('hash') }
|
12
|
+
assert_raises(TypeError) { assert Hash.cast(1) }
|
13
|
+
assert_raises(TypeError) { assert Hash.cast(1.0) }
|
14
|
+
assert_raises(TypeError) { assert Hash.cast(true) }
|
15
|
+
assert_raises(TypeError) { assert Hash.cast(false) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
describe 'Hash' do
|
2
|
+
describe '#self.cast' do
|
3
|
+
it 'casts value to Array' do
|
4
|
+
assert Hash.cast([[:foo, :bar]]) == {foo: :bar}
|
5
|
+
assert Hash.cast({foo: :bar}) == {foo: :bar}
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'to_h is not implemented' do
|
10
|
+
it 'raises a TypeError' do
|
11
|
+
assert_raises(TypeError) { assert Hash.cast('hash') }
|
12
|
+
assert_raises(TypeError) { assert Hash.cast(1) }
|
13
|
+
assert_raises(TypeError) { assert Hash.cast(1.0) }
|
14
|
+
assert_raises(TypeError) { assert Hash.cast(true) }
|
15
|
+
assert_raises(TypeError) { assert Hash.cast(false) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
describe 'Float' do
|
2
|
+
describe '#self.cast' do
|
3
|
+
it 'casts value to Float' do
|
4
|
+
assert Float.cast('one') == 0.0
|
5
|
+
assert Float.cast('1') == 1.0
|
6
|
+
assert Float.cast(1) == 1.0
|
7
|
+
assert Float.cast(1.0) == 1.0
|
8
|
+
assert Float.cast(nil) == 0.0
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'to_f is not implemented' do
|
12
|
+
it 'raises a TypeError' do
|
13
|
+
assert_raises(TypeError) { assert Float.cast([]) }
|
14
|
+
assert_raises(TypeError) { assert Float.cast({}) }
|
15
|
+
assert_raises(TypeError) { assert Float.cast(true) }
|
16
|
+
assert_raises(TypeError) { assert Float.cast(false) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#to_b' do
|
22
|
+
it 'casts value to Boolean' do
|
23
|
+
refute 0.0.to_b
|
24
|
+
assert 0.1.to_b
|
25
|
+
assert 1.1.to_b
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
describe 'Integer' do
|
2
|
+
describe '#self.cast' do
|
3
|
+
it 'casts value to Integer' do
|
4
|
+
assert Integer.cast('one') == 0
|
5
|
+
assert Integer.cast('1') == 1
|
6
|
+
assert Integer.cast(1) == 1
|
7
|
+
assert Integer.cast(1.0) == 1
|
8
|
+
assert Integer.cast(nil) == 0
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'to_i is not implemented' do
|
12
|
+
it 'raises a TypeError' do
|
13
|
+
assert_raises(TypeError) { assert Integer.cast([]) }
|
14
|
+
assert_raises(TypeError) { assert Integer.cast({}) }
|
15
|
+
assert_raises(TypeError) { assert Integer.cast(true) }
|
16
|
+
assert_raises(TypeError) { assert Integer.cast(false) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#to_b' do
|
22
|
+
it 'casts value to Boolean' do
|
23
|
+
refute 0.to_b
|
24
|
+
assert 1.to_b
|
25
|
+
assert 2.to_b
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
describe 'String' do
|
2
|
+
describe '#self.cast' do
|
3
|
+
it 'casts value to String' do
|
4
|
+
assert String.cast('one') == 'one'
|
5
|
+
assert String.cast(1) == '1'
|
6
|
+
assert String.cast(1.0) == '1.0'
|
7
|
+
assert String.cast([]) == '[]'
|
8
|
+
assert String.cast({}) == '{}'
|
9
|
+
assert String.cast(true) == 'true'
|
10
|
+
assert String.cast(false) == 'false'
|
11
|
+
assert String.cast(nil) == ''
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#to_b' do
|
16
|
+
context 'value is truthy' do
|
17
|
+
it 'returns true' do
|
18
|
+
['t', 'T', 'true', 'TRUE', 'on', 'ON', 'y', 'Y', 'yes', 'YES', '1'].each do |value|
|
19
|
+
assert value.to_b
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'value is falsy' do
|
25
|
+
it 'returns false' do
|
26
|
+
['f', 'F', 'false', 'FALSE', 'off', 'OFF', 'n', 'N', 'no', 'NO', '0'].each do |value|
|
27
|
+
refute value.to_b
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'value is not a truthy or falsy' do
|
33
|
+
it 'raises TypeError' do
|
34
|
+
assert_raises TypeError do
|
35
|
+
'foo'.to_b
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mugatu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Weiss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- weissst@mail.gvsu.edu
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/mugatu.rb
|
26
|
+
- lib/mugatu/attribute.rb
|
27
|
+
- lib/mugatu/attribute_type.rb
|
28
|
+
- lib/mugatu/entity.rb
|
29
|
+
- lib/mugatu/types/array.rb
|
30
|
+
- lib/mugatu/types/boolean.rb
|
31
|
+
- lib/mugatu/types/boolean/false.rb
|
32
|
+
- lib/mugatu/types/boolean/true.rb
|
33
|
+
- lib/mugatu/types/hash.rb
|
34
|
+
- lib/mugatu/types/nil.rb
|
35
|
+
- lib/mugatu/types/numeric.rb
|
36
|
+
- lib/mugatu/types/numeric/float.rb
|
37
|
+
- lib/mugatu/types/numeric/integer.rb
|
38
|
+
- lib/mugatu/types/string.rb
|
39
|
+
- lib/mugatu/version.rb
|
40
|
+
- mugatu.gemspec
|
41
|
+
- spec/mugatu_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
- spec/types/array_spec.rb
|
44
|
+
- spec/types/boolean/false_spec.rb
|
45
|
+
- spec/types/boolean/true_spec.rb
|
46
|
+
- spec/types/boolean_spec.rb
|
47
|
+
- spec/types/hash_spec.rb
|
48
|
+
- spec/types/nil_spec.rb
|
49
|
+
- spec/types/numeric/float_spec.rb
|
50
|
+
- spec/types/numeric/integer_spec.rb
|
51
|
+
- spec/types/string_spec.rb
|
52
|
+
homepage: https://github.com/sirscriptalot/mugatu
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.5.1
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Mugatu
|
76
|
+
test_files: []
|