constructable 0.2.1 → 0.2.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.
- data/VERSION +1 -1
- data/constructable.gemspec +1 -1
- data/lib/constructable/attribute.rb +2 -8
- data/lib/constructable/constructor.rb +3 -19
- data/test/constructable/test_constructor.rb +0 -26
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/constructable.gemspec
CHANGED
@@ -7,7 +7,7 @@ module Constructable
|
|
7
7
|
REQUIRED_REQUIREMENT = {
|
8
8
|
name: :required,
|
9
9
|
message: proc {":#{self.name} is a required attribute"},
|
10
|
-
check: ->(hash) { hash
|
10
|
+
check: ->(hash) { hash[self.name] }
|
11
11
|
}
|
12
12
|
|
13
13
|
REQUIREMENTS = [
|
@@ -30,10 +30,6 @@ module Constructable
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
def defined?
|
34
|
-
@defined
|
35
|
-
end
|
36
|
-
|
37
33
|
def accessible=(boolean)
|
38
34
|
if boolean
|
39
35
|
self.readable = true
|
@@ -55,16 +51,14 @@ module Constructable
|
|
55
51
|
private :check_for_requirement
|
56
52
|
|
57
53
|
def process(constructor_hash)
|
58
|
-
if constructor_hash
|
54
|
+
if constructor_hash[self.name]
|
59
55
|
REQUIREMENTS.each do |requirement|
|
60
56
|
check_for_requirement(requirement, constructor_hash)
|
61
57
|
end
|
62
58
|
value = constructor_hash[self.name]
|
63
|
-
@defined = true
|
64
59
|
self.converter ? converter.(value) : value
|
65
60
|
else
|
66
61
|
check_for_requirement(REQUIRED_REQUIREMENT, constructor_hash)
|
67
|
-
@defined = false
|
68
62
|
self.default
|
69
63
|
end.tap { |value| @value = value }
|
70
64
|
end
|
@@ -21,19 +21,11 @@ module Constructable
|
|
21
21
|
attributes = @attributes
|
22
22
|
attributes.each do |attribute|
|
23
23
|
@klass.class_eval do
|
24
|
-
|
25
24
|
attr_reader attribute.name if attribute.readable
|
26
25
|
|
27
26
|
define_method(:"#{attribute.name}=") do |value|
|
28
27
|
instance_variable_set attribute.ivar_symbol, attribute.process({ attribute.name => value})
|
29
28
|
end if attribute.writable
|
30
|
-
|
31
|
-
define_method(attribute.group) do
|
32
|
-
attributes.group_by(&:group)[attribute.group].inject({}) do |hash, attribute|
|
33
|
-
hash[attribute.name] = attribute.value if attribute.defined?
|
34
|
-
hash
|
35
|
-
end
|
36
|
-
end if attribute.group && !method_defined?(attribute.group)
|
37
29
|
end
|
38
30
|
end
|
39
31
|
end
|
@@ -46,17 +38,9 @@ module Constructable
|
|
46
38
|
end
|
47
39
|
|
48
40
|
def generate_attributes(attributes)
|
49
|
-
attributes.map do |
|
50
|
-
|
51
|
-
|
52
|
-
attributes.map do |attribute|
|
53
|
-
Attribute.new(*attribute).tap { |a| a.group = dynamic_args.keys.first }
|
54
|
-
end
|
55
|
-
end
|
56
|
-
else
|
57
|
-
Attribute.new(*dynamic_args)
|
58
|
-
end
|
59
|
-
end.flatten
|
41
|
+
attributes.map do |attribute|
|
42
|
+
Attribute.new(*attribute)
|
43
|
+
end
|
60
44
|
end
|
61
45
|
end
|
62
46
|
end
|
@@ -14,32 +14,6 @@ describe 'Constructor' do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
describe 'Attribute#group' do
|
18
|
-
it 'returns attributes included in the group along with their values' do
|
19
|
-
@klass.constructable [:foo, group: :foobar], [:bar, group: :foobar]
|
20
|
-
instance = @klass.new(foo: 1, bar: 2)
|
21
|
-
assert_equal({foo: 1, bar: 2}, instance.foobar)
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'ONLY returns attributes included in that group' do
|
25
|
-
@klass.constructable [:foo, group: :foobar], [:bar]
|
26
|
-
instance = @klass.new(foo: 1, bar: 2)
|
27
|
-
assert_equal({foo: 1}, instance.foobar)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'has a nice syntax' do
|
31
|
-
@klass.constructable foobar: [:foo, :bar]
|
32
|
-
instance = @klass.new(foo: 1, bar: 2)
|
33
|
-
assert_equal({foo: 1, bar: 2}, instance.foobar)
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'ONLY returns attributes acutally provided' do
|
37
|
-
@klass.constructable foobar: [:foo, :bar]
|
38
|
-
instance = @klass.new(foo: 1)
|
39
|
-
assert_equal false, instance.foobar.has_key?(:bar)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
17
|
describe 'permission' do
|
44
18
|
it 'should allow writable attributes' do
|
45
19
|
@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.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Manuel Korfmann
|
@@ -104,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
104
|
requirements:
|
105
105
|
- - ">="
|
106
106
|
- !ruby/object:Gem::Version
|
107
|
-
hash:
|
107
|
+
hash: 1216029924814922555
|
108
108
|
segments:
|
109
109
|
- 0
|
110
110
|
version: "0"
|