massager 0.2.0 → 0.2.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 +14 -0
- data/lib/massager/version.rb +1 -1
- data/lib/massager.rb +31 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbe0172b4b689156c4e488c3860b272c096b3c7c
|
4
|
+
data.tar.gz: 4ee1813aa3fdf68ea08f431eb1a66910d8c61347
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8704155886d7bf79ca08f2f3a73032a9a49b25fba6a7e65c65e37b12d270999acd0eddc49c6eac43f8a10ea2dda30cac26a129045f1d4d7edfb0d227e99ac07
|
7
|
+
data.tar.gz: d2b8547a4d30459cf7346f0055e87ad529e07d616f44042e6dd72a01681da788934e46e03ff7f93f741b68de0495015254180e734a888da83bf3aae0fdaeb671
|
data/README.md
CHANGED
@@ -31,6 +31,20 @@ In this scenario, the "bar" key's value will become the result `foo` method
|
|
31
31
|
testable = ExampleClass.build({"bar" => "value"})
|
32
32
|
testable.foo #=> "value"
|
33
33
|
```
|
34
|
+
## Strict schema
|
35
|
+
You can have required keys defined with `strict: true`
|
36
|
+
```ruby
|
37
|
+
class ExampleClass
|
38
|
+
include Massager
|
39
|
+
attribute :foo, "bar", strict: true
|
40
|
+
end
|
41
|
+
```
|
42
|
+
It will raise an error if "bar" is not passed:
|
43
|
+
```ruby
|
44
|
+
testable = ExampleClass.build({"bar" => "value"})
|
45
|
+
testable.foo #=> "value"
|
46
|
+
testable = ExampleClass.build({"baz" => "value"}) #=> raises ArgumentError
|
47
|
+
```
|
34
48
|
|
35
49
|
## Type checking
|
36
50
|
You can also pass type checks using dry-types library:
|
data/lib/massager/version.rb
CHANGED
data/lib/massager.rb
CHANGED
@@ -2,6 +2,7 @@ require "massager/version"
|
|
2
2
|
require "dry-types"
|
3
3
|
require "dry-monads"
|
4
4
|
require "dry-container"
|
5
|
+
require "set"
|
5
6
|
|
6
7
|
require "massager/attributes/attribute"
|
7
8
|
require "massager/attributes/enum_attribute"
|
@@ -9,41 +10,63 @@ require "massager/attributes/enum_attribute"
|
|
9
10
|
module Massager
|
10
11
|
module ClassMethods
|
11
12
|
def attribute(name, *target_keys, **opts, &block)
|
12
|
-
set_container
|
13
13
|
register_attribute(
|
14
14
|
Attribute.new(name: name, target_keys: target_keys, opts: opts, block: block)
|
15
15
|
)
|
16
|
+
add_to_schema(opts, target_keys)
|
16
17
|
define_setter(name)
|
17
18
|
define_getter(name)
|
18
19
|
end
|
19
20
|
|
20
21
|
def enum_attribute(name, *target_keys, **opts, &block)
|
21
|
-
set_container
|
22
22
|
register_attribute(
|
23
23
|
EnumAttribute.new(name: name, target_keys: target_keys, opts: opts, block: block)
|
24
24
|
)
|
25
|
+
add_to_schema(opts, target_keys)
|
25
26
|
define_setter(name)
|
26
27
|
define_getter(name)
|
27
28
|
end
|
28
29
|
|
29
30
|
def build(attrs)
|
31
|
+
check_schema(attrs)
|
30
32
|
instance = new
|
31
|
-
|
32
|
-
attribute =
|
33
|
-
instance.public_send("#{
|
33
|
+
each_key do |k|
|
34
|
+
attribute = resolve(k)
|
35
|
+
instance.public_send("#{attribute.name}=", attrs) if attribute.match_schema?(attrs)
|
34
36
|
end
|
35
37
|
instance
|
36
38
|
end
|
37
39
|
|
38
40
|
private
|
39
41
|
|
42
|
+
def check_schema(attrs)
|
43
|
+
attr_keys = attrs.keys.to_set
|
44
|
+
if key?(:schema)
|
45
|
+
schema = resolve(:schema)
|
46
|
+
raise ArgumentError, "Missing keys: #{(schema - attr_keys).to_a}" unless schema.superset?(attr_keys)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def add_keys_to_schema(opts, target_keys)
|
51
|
+
Dry::Monads::Maybe(opts[:strict]).fmap {
|
52
|
+
unless key?(:schema)
|
53
|
+
register(:schema, Set.new)
|
54
|
+
end
|
55
|
+
target_keys.each do |k|
|
56
|
+
resolve(:schema) << k
|
57
|
+
end
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
40
61
|
def register_attribute(attribute)
|
41
|
-
|
62
|
+
namespace(:attributes) do
|
63
|
+
register(attribute.name, attribute)
|
64
|
+
end
|
42
65
|
end
|
43
66
|
|
44
67
|
def define_setter(name)
|
45
68
|
define_method "#{name}=", Proc.new {|values|
|
46
|
-
attribute = self.class.
|
69
|
+
attribute = self.class.resolve("attributes.#{name}")
|
47
70
|
instance_variable_set(:"@#{name}", attribute.call(values))
|
48
71
|
}
|
49
72
|
end
|
@@ -53,15 +76,10 @@ module Massager
|
|
53
76
|
instance_variable_get(:"@#{name}")
|
54
77
|
}
|
55
78
|
end
|
56
|
-
|
57
|
-
def set_container
|
58
|
-
self.define_singleton_method(:container) do
|
59
|
-
@container ||= Dry::Container.new
|
60
|
-
end
|
61
|
-
end
|
62
79
|
end
|
63
80
|
|
64
81
|
def self.included(base)
|
65
82
|
base.extend(ClassMethods)
|
83
|
+
base.extend(Dry::Container::Mixin)
|
66
84
|
end
|
67
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: massager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janis Miezitis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-types
|