massager 0.3.0 → 0.3.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 +4 -4
- data/README.md +1 -2
- data/lib/massager/version.rb +1 -1
- data/lib/massager.rb +21 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78a1fd152f231a1aeffae99644dbc95b6cf8583d
|
4
|
+
data.tar.gz: 371c921b1552297cae85ccc5220f117e0a2f9885
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e89af400a3047eff8b7b9aa209ed07082cbf1956d8bc12b1d9ef4ed7d39919adc4afcbb4588d4c6c1d41bf37a359322703f68dc99e82e1e03806971ca24cc67f
|
7
|
+
data.tar.gz: e7a1062b52df8b43fdeda66b4c212963e9b1a98bc1f19a461a3663da1f498efb368bf4730bb41ba3f86e4fddf52e8f5e467022808a09829e0d823f17084e0945
|
data/README.md
CHANGED
@@ -58,8 +58,7 @@ It will raise an error if the type is not correct:
|
|
58
58
|
```ruby
|
59
59
|
testable = ExampleClass.call({"bar" => "value"})
|
60
60
|
testable.foo #=> "value"
|
61
|
-
testable = ExampleClass.call({"bar" => 123})
|
62
|
-
testable.foo #=> raises Dry::Types::ConstraintError
|
61
|
+
testable = ExampleClass.call({"bar" => 123}) #=> raises Dry::Types::ConstraintError
|
63
62
|
```
|
64
63
|
If you want to define your own types, check the Dry Types library. Type needs to respond to `call` method, so
|
65
64
|
you can define your own
|
data/lib/massager/version.rb
CHANGED
data/lib/massager.rb
CHANGED
@@ -28,19 +28,23 @@ module Massager
|
|
28
28
|
def call(attrs)
|
29
29
|
check_schema(attrs)
|
30
30
|
instance = new
|
31
|
-
_container.
|
32
|
-
attribute = resolve(k)
|
31
|
+
_container.keys.select {|a| a.include?("attributes.")}.each do |k|
|
32
|
+
attribute = _container.resolve(k)
|
33
33
|
instance.public_send("#{attribute.name}=", attrs) if attribute.match_schema?(attrs)
|
34
34
|
end
|
35
35
|
instance
|
36
36
|
end
|
37
37
|
|
38
|
+
def _container
|
39
|
+
@container ||= Dry::Container.new
|
40
|
+
end
|
41
|
+
|
38
42
|
private
|
39
43
|
|
40
44
|
def check_schema(attrs)
|
41
45
|
attr_keys = attrs.keys.to_set
|
42
46
|
if _container.key?("schema")
|
43
|
-
schema = resolve("schema")
|
47
|
+
schema = _container.resolve("schema")
|
44
48
|
attr_keys = attr_keys.find_all {|a| schema.include?(a)}
|
45
49
|
raise ArgumentError, "Missing keys: #{(schema - attr_keys).to_a}" unless schema.subset?(attr_keys.to_set)
|
46
50
|
end
|
@@ -48,24 +52,24 @@ module Massager
|
|
48
52
|
|
49
53
|
def add_keys_to_schema(opts, target_keys)
|
50
54
|
Dry::Monads::Maybe(opts[:strict]).fmap {
|
51
|
-
unless key?(:schema)
|
52
|
-
register(:schema, Set.new)
|
55
|
+
unless _container.key?(:schema)
|
56
|
+
_container.register(:schema, Set.new)
|
53
57
|
end
|
54
58
|
target_keys.each do |k|
|
55
|
-
resolve(:schema) << k
|
59
|
+
_container.resolve(:schema) << k
|
56
60
|
end
|
57
61
|
}
|
58
62
|
end
|
59
63
|
|
60
64
|
def register_attribute(attribute)
|
61
|
-
namespace(:attributes) do
|
65
|
+
_container.namespace(:attributes) do
|
62
66
|
register(attribute.name, attribute)
|
63
67
|
end
|
64
68
|
end
|
65
69
|
|
66
70
|
def define_setter(name)
|
67
71
|
define_method "#{name}=", Proc.new {|values|
|
68
|
-
attribute = self.class.resolve("attributes.#{name}")
|
72
|
+
attribute = self.class._container.resolve("attributes.#{name}")
|
69
73
|
instance_variable_set(:"@#{name}", attribute.call(values))
|
70
74
|
}
|
71
75
|
end
|
@@ -75,10 +79,18 @@ module Massager
|
|
75
79
|
instance_variable_get(:"@#{name}")
|
76
80
|
}
|
77
81
|
end
|
82
|
+
|
83
|
+
def inherited(subclass)
|
84
|
+
subclass_container = Dry::Container.new
|
85
|
+
_container.each_key do |k|
|
86
|
+
subclass_container.register(k, _container.resolve(k).clone)
|
87
|
+
end
|
88
|
+
subclass.instance_variable_set(:"@container", subclass_container)
|
89
|
+
end
|
78
90
|
end
|
79
91
|
|
92
|
+
|
80
93
|
def self.included(base)
|
81
94
|
base.extend(ClassMethods)
|
82
|
-
base.extend(Dry::Container::Mixin)
|
83
95
|
end
|
84
96
|
end
|