mugatu 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
11
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -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.
@@ -0,0 +1,3 @@
1
+ # Mugatu
2
+
3
+ Mugatu is small library for creating domain models.
@@ -0,0 +1,5 @@
1
+ task :spec do
2
+ Dir.glob('./spec/**/*_spec.rb').each { |file| require file }
3
+ end
4
+
5
+ task :default => :spec
@@ -0,0 +1,4 @@
1
+ require_relative './mugatu/version'
2
+ require_relative './mugatu/entity'
3
+ require_relative './mugatu/attribute'
4
+ require_relative './mugatu/attribute_type'
@@ -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,5 @@
1
+ class Array
2
+ extend Mugatu::AttributeType
3
+
4
+ set_cast_with :to_a
5
+ end
@@ -0,0 +1,5 @@
1
+ class Boolean
2
+ extend Mugatu::AttributeType
3
+
4
+ set_cast_with :to_b
5
+ end
@@ -0,0 +1,5 @@
1
+ class FalseClass
2
+ def to_b
3
+ self
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class TrueClass
2
+ def to_b
3
+ self
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ extend Mugatu::AttributeType
3
+
4
+ set_cast_with :to_h
5
+ end
@@ -0,0 +1,5 @@
1
+ class NilClass
2
+ def to_b
3
+ false
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Numeric
2
+ def to_b
3
+ !zero?
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Float
2
+ extend Mugatu::AttributeType
3
+
4
+ set_cast_with :to_f
5
+ end
@@ -0,0 +1,5 @@
1
+ class Integer
2
+ extend Mugatu::AttributeType
3
+
4
+ set_cast_with :to_i
5
+ 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
@@ -0,0 +1,3 @@
1
+ module Mugatu
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -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
@@ -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
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/mugatu'
3
+
4
+ def context(*args, &block)
5
+ describe(*args, &block)
6
+ end
@@ -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,7 @@
1
+ describe 'FalseClass' do
2
+ describe '#to_b' do
3
+ it 'returns false' do
4
+ refute false.to_b
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ describe 'TrueClass' do
2
+ describe '#to_b' do
3
+ it 'returns true' do
4
+ assert true.to_b
5
+ end
6
+ end
7
+ 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,7 @@
1
+ describe 'NilClass' do
2
+ describe '#to_b' do
3
+ it 'returns false' do
4
+ refute nil.to_b
5
+ end
6
+ end
7
+ 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: []