constructable 0.2.2 → 0.2.3
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.
- data/VERSION +1 -1
- data/constructable.gemspec +1 -2
- data/lib/constructable/attribute.rb +2 -2
- data/lib/constructable/constructor.rb +17 -0
- data/lib/constructable/core_ext.rb +40 -1
- data/lib/constructable.rb +1 -2
- data/test/constructable/test_attribute.rb +5 -10
- data/test/constructable/test_constructor.rb +8 -0
- metadata +2 -3
- data/lib/constructable/constructable.rb +0 -40
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/constructable.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{constructable}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Manuel Korfmann"]
|
@@ -33,7 +33,6 @@ See the documentation for Constructable::Constructable#constructable or the READ
|
|
33
33
|
"constructable.gemspec",
|
34
34
|
"lib/constructable.rb",
|
35
35
|
"lib/constructable/attribute.rb",
|
36
|
-
"lib/constructable/constructable.rb",
|
37
36
|
"lib/constructable/constructor.rb",
|
38
37
|
"lib/constructable/core_ext.rb",
|
39
38
|
"lib/constructable/exceptions.rb",
|
@@ -51,7 +51,7 @@ module Constructable
|
|
51
51
|
private :check_for_requirement
|
52
52
|
|
53
53
|
def process(constructor_hash)
|
54
|
-
|
54
|
+
unless constructor_hash[self.name].nil?
|
55
55
|
REQUIREMENTS.each do |requirement|
|
56
56
|
check_for_requirement(requirement, constructor_hash)
|
57
57
|
end
|
@@ -60,7 +60,7 @@ module Constructable
|
|
60
60
|
else
|
61
61
|
check_for_requirement(REQUIRED_REQUIREMENT, constructor_hash)
|
62
62
|
self.default
|
63
|
-
end
|
63
|
+
end
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module Constructable
|
2
2
|
class Constructor
|
3
|
+
attr_accessor :attributes
|
4
|
+
|
3
5
|
def initialize(klass)
|
4
6
|
@attributes = []
|
5
7
|
@klass = klass
|
@@ -11,6 +13,8 @@ module Constructable
|
|
11
13
|
obj.send :initialize, *args, &block
|
12
14
|
obj
|
13
15
|
end
|
16
|
+
|
17
|
+
self.define_attributes_method
|
14
18
|
end
|
15
19
|
|
16
20
|
|
@@ -42,5 +46,18 @@ module Constructable
|
|
42
46
|
Attribute.new(*attribute)
|
43
47
|
end
|
44
48
|
end
|
49
|
+
|
50
|
+
def define_attributes_method
|
51
|
+
constructor = self
|
52
|
+
@klass.class_eval do
|
53
|
+
define_method :attributes do
|
54
|
+
Hash[
|
55
|
+
constructor.attributes
|
56
|
+
.map { |a| [a.name,instance_variable_get(a.ivar_symbol)] }
|
57
|
+
.select(&:last)
|
58
|
+
]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
45
62
|
end
|
46
63
|
end
|
@@ -1,3 +1,42 @@
|
|
1
1
|
module Constructable
|
2
|
-
::
|
2
|
+
class ::Class
|
3
|
+
include Constructable
|
4
|
+
|
5
|
+
# @example
|
6
|
+
#
|
7
|
+
# class Foo
|
8
|
+
# constructable [:bar, :readable => true], [:baz, :required => true, :readable => true]
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# foo = Foo.new(bar: 5)
|
12
|
+
# # raises AttributeError, ':baz is a required attribute'
|
13
|
+
#
|
14
|
+
# foo = Foo.new(baz: 7, bar: 5)
|
15
|
+
#
|
16
|
+
# foo.bar
|
17
|
+
# # => 5
|
18
|
+
# foo.baz
|
19
|
+
# # => 7
|
20
|
+
#
|
21
|
+
# class ProgrammingLanguage
|
22
|
+
# constructable [:paradigms,
|
23
|
+
# readable: true,
|
24
|
+
# required: true,
|
25
|
+
# validate: ->(value) { value.is_a?(Array) }]
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# c = ProgrammingLanguage.new(paradigms: :functional)
|
29
|
+
# # raises AttributeError, ':paradigms did not pass validation'
|
30
|
+
#
|
31
|
+
# ruby = ProgrammingLanguage.new(paradigms: [:object_oriented, :functional])
|
32
|
+
# ruby.paradigms
|
33
|
+
# # => [:object_oriented, :functional]
|
34
|
+
#
|
35
|
+
# @param [Array<[Array<Symbol, Hash>]>] args an array of symbols or arrays: the name of the attribute and it's configuration
|
36
|
+
def constructable(*args)
|
37
|
+
@constructor ||= Constructor.new(self)
|
38
|
+
@constructor.define_attributes(args)
|
39
|
+
return nil
|
40
|
+
end
|
41
|
+
end
|
3
42
|
end
|
data/lib/constructable.rb
CHANGED
@@ -2,16 +2,6 @@ require 'helper'
|
|
2
2
|
include Constructable
|
3
3
|
describe 'Attribute' do
|
4
4
|
|
5
|
-
describe 'value' do
|
6
|
-
it 'returns the last processed value' do
|
7
|
-
attribute = Attribute.new(:foo)
|
8
|
-
attribute.process({foo: :bar})
|
9
|
-
assert_equal :bar, attribute.value
|
10
|
-
attribute.process({foo: 1})
|
11
|
-
assert_equal 1, attribute.value
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
5
|
describe 'name' do
|
16
6
|
it 'returns the name' do
|
17
7
|
attribute = Attribute.new(:attribute)
|
@@ -27,6 +17,11 @@ describe 'Attribute' do
|
|
27
17
|
end
|
28
18
|
|
29
19
|
describe 'process' do
|
20
|
+
it 'intepretes nil but not false as undefined' do
|
21
|
+
attribute = Attribute.new(:foo, default: true)
|
22
|
+
assert_equal false, attribute.process({foo: false})
|
23
|
+
end
|
24
|
+
|
30
25
|
it 'should raise nothing if no attributes are specified' do
|
31
26
|
attribute = Attribute.new(:foo)
|
32
27
|
assert_equal 'bar', attribute.process({foo: 'bar'})
|
@@ -14,6 +14,14 @@ describe 'Constructor' do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
describe 'Class#attributes' do
|
18
|
+
it 'returns the attribute matching the symbol' do
|
19
|
+
@klass.constructable :foo, :bar
|
20
|
+
instance = @klass.new(foo: 6)
|
21
|
+
assert_equal({ foo: 6 }, instance.attributes)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
17
25
|
describe 'permission' do
|
18
26
|
it 'should allow writable attributes' do
|
19
27
|
@klass.constructable [:writable_attribute, writable: true]
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: constructable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Manuel Korfmann
|
@@ -82,7 +82,6 @@ files:
|
|
82
82
|
- constructable.gemspec
|
83
83
|
- lib/constructable.rb
|
84
84
|
- lib/constructable/attribute.rb
|
85
|
-
- lib/constructable/constructable.rb
|
86
85
|
- lib/constructable/constructor.rb
|
87
86
|
- lib/constructable/core_ext.rb
|
88
87
|
- lib/constructable/exceptions.rb
|
@@ -104,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
103
|
requirements:
|
105
104
|
- - ">="
|
106
105
|
- !ruby/object:Gem::Version
|
107
|
-
hash:
|
106
|
+
hash: -3265931130624330065
|
108
107
|
segments:
|
109
108
|
- 0
|
110
109
|
version: "0"
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Constructable
|
2
|
-
module Constructable
|
3
|
-
# @example
|
4
|
-
#
|
5
|
-
# class Foo
|
6
|
-
# constructable [:bar, :readable => true], [:baz, :required => true, :readable => true]
|
7
|
-
# end
|
8
|
-
#
|
9
|
-
# foo = Foo.new(bar: 5)
|
10
|
-
# # raises AttributeError, ':baz is a required attribute'
|
11
|
-
#
|
12
|
-
# foo = Foo.new(baz: 7, bar: 5)
|
13
|
-
#
|
14
|
-
# foo.bar
|
15
|
-
# # => 5
|
16
|
-
# foo.baz
|
17
|
-
# # => 7
|
18
|
-
#
|
19
|
-
# class ProgrammingLanguage
|
20
|
-
# constructable [:paradigms,
|
21
|
-
# readable: true,
|
22
|
-
# required: true,
|
23
|
-
# validate: ->(value) { value.is_a?(Array) }]
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# c = ProgrammingLanguage.new(paradigms: :functional)
|
27
|
-
# # raises AttributeError, ':paradigms did not pass validation'
|
28
|
-
#
|
29
|
-
# ruby = ProgrammingLanguage.new(paradigms: [:object_oriented, :functional])
|
30
|
-
# ruby.paradigms
|
31
|
-
# # => [:object_oriented, :functional]
|
32
|
-
#
|
33
|
-
# @param [Array<[Array<Symbol, Hash>]>] args an array of symbols or arrays: the name of the attribute and it's configuration
|
34
|
-
def constructable(*args)
|
35
|
-
@__constructor ||= Constructor.new(self)
|
36
|
-
@__constructor.define_attributes(args)
|
37
|
-
return nil
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|