massager 0.2.2 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d2d0b338f8b5b13f2f4e6eb7a3e2f918e17f6804
4
- data.tar.gz: 4c593d00ad891621d637a9f3d61bd1ec9e01fa41
3
+ metadata.gz: c76b7d2aefdb4e9d17182759b7c7abcf25793f8b
4
+ data.tar.gz: eb96295d2ede881cf36fd89540fc16f805269c4b
5
5
  SHA512:
6
- metadata.gz: a8d823265174713d074e43f90620bc97aab685d28869fe7961ae434b60bb1707910bb6b09df9d91bf9c7c016e9b9378959a74d9971233a0cc4ba840985f2e4d1
7
- data.tar.gz: c2da37ebbe135a94e7994eda38073f8763c2a5b030a8bcbe4e4943611e63f7ebaffc60ab89cf7d6fdae4589c755c09324b306e52cbe86faf571d5e1c0124e8aa
6
+ metadata.gz: aafea83a72a4438db9be150cb8c4c230d1c12b33901214a2cec88f864afaa6c5b77adcd3fde16d75e78303042ca1663eb3ac8d08ace0f84d3775a435841ba3ac
7
+ data.tar.gz: 9fb64863a5619f31690f6d440e7602f4e1049254b95a2fcbeffb787c7d1fb42d780a8dc50e2f81be3865569481fd8f38348754195788cf86e85abbc78416a47b
data/README.md CHANGED
@@ -28,7 +28,7 @@ end
28
28
  ```
29
29
  In this scenario, the "bar" key's value will become the result `foo` method
30
30
  ```ruby
31
- testable = ExampleClass.build({"bar" => "value"})
31
+ testable = ExampleClass.call({"bar" => "value"})
32
32
  testable.foo #=> "value"
33
33
  ```
34
34
  ## Strict schema
@@ -41,9 +41,9 @@ end
41
41
  ```
42
42
  It will raise an error if "bar" is not passed:
43
43
  ```ruby
44
- testable = ExampleClass.build({"bar" => "value"})
44
+ testable = ExampleClass.call({"bar" => "value"})
45
45
  testable.foo #=> "value"
46
- testable = ExampleClass.build({"baz" => "value"}) #=> raises ArgumentError
46
+ testable = ExampleClass.call({"baz" => "value"}) #=> raises ArgumentError
47
47
  ```
48
48
 
49
49
  ## Type checking
@@ -56,9 +56,9 @@ end
56
56
  ```
57
57
  It will raise an error if the type is not correct:
58
58
  ```ruby
59
- testable = ExampleClass.build({"bar" => "value"})
59
+ testable = ExampleClass.call({"bar" => "value"})
60
60
  testable.foo #=> "value"
61
- testable = ExampleClass.build({"bar" => 123})
61
+ testable = ExampleClass.call({"bar" => 123})
62
62
  testable.foo #=> raises Dry::Types::ConstraintError
63
63
  ```
64
64
  If you want to define your own types, check the Dry Types library. Type needs to respond to `call` method, so
@@ -77,7 +77,7 @@ end
77
77
  ```
78
78
  And it will have following result
79
79
  ```ruby
80
- testable = ExampleClass.build({"bar" => "value"})
80
+ testable = ExampleClass.call({"bar" => "value"})
81
81
  testable.foo #=> "VALUE"
82
82
  ```
83
83
 
@@ -94,53 +94,10 @@ end
94
94
  Note that if you pass multiple keys, the modifier block is mandatory
95
95
 
96
96
  ```ruby
97
- testable = ExampleClass.build({"bar" => "bar", "baz" => "baz"})
97
+ testable = ExampleClass.call({"bar" => "bar", "baz" => "baz"})
98
98
  testable.foo #=> "bar baz"
99
99
  ```
100
100
 
101
- ## Enum attributes
102
- If you want to have enum as a result, you will need to use `enum_attribute`
103
- ```ruby
104
- class ExampleClass
105
- include Massager
106
- enum_attribute :foo, "bar", "baz"
107
- end
108
- ```
109
- ```ruby
110
- testable = ExampleClass.build({"bar" => "bar", "baz" => "baz"})
111
- testable.foo #=> ["bar", "baz"]
112
- ```
113
-
114
- ## Enum attribute with modifier
115
- You can apply modifications to the collection
116
- ```ruby
117
- class ExampleClass
118
- include Massager
119
- enum_attribute :foo, "bar", "baz" do |values|
120
- values.reverse
121
- end
122
- end
123
- ```
124
- ```ruby
125
- testable = ExampleClass.build({"bar" => "bar", "baz" => "baz"})
126
- testable.foo #=> ["baz", "bar"]
127
- ```
128
-
129
- ## Enum attribute with type check
130
- You can provide type checks as well
131
- ```ruby
132
- class ExampleClass
133
- include Massager
134
- enum_attribute :foo, "bar", "baz", type: Types::Strict::Array.member(Types::Strict::String)
135
- end
136
- ```
137
- ```ruby
138
- testable = ExampleClass.build({"bar" => "bar", "baz" => "baz"})
139
- testable.foo #=> ["bar", "baz"]
140
-
141
- testable = ExampleClass.build({"bar" => 123, "baz" => "baz"}) # Will raise Dry::Types::ConstraintError
142
- ```
143
-
144
101
  ## License
145
102
 
146
103
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,25 @@
1
+ module Massager
2
+ class AttributeWithMultipleKeys
3
+ def initialize(name:, keys:, opts: {}, block: nil)
4
+ raise ArgumentError, "If you pass multiple keys, you have to use modifier block" if block.nil?
5
+ @name, @keys, @opts, @block = name, keys, opts, block
6
+ end
7
+
8
+ def call(values)
9
+ values = values.values_at(*keys)
10
+ values = block.call(*values)
11
+ Dry::Monads::Maybe(opts[:type]).fmap {|type| values = type.call(*values)}
12
+ values
13
+ end
14
+
15
+ def return_result(values)
16
+ values
17
+ end
18
+
19
+ def match_schema?(attrs)
20
+ (attrs.keys & keys).any?
21
+ end
22
+
23
+ attr_reader :keys, :block, :opts, :name
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module Massager
2
+ class AttributeWithSingleKey
3
+ def initialize(name:, key:, opts: {}, block: nil)
4
+ @name, @key, @opts, @block = name, key, opts, block
5
+ end
6
+
7
+ def call(values)
8
+ value = values.fetch(key)
9
+ Dry::Monads::Maybe(block).fmap {|block| value = block.call(value)}
10
+ Dry::Monads::Maybe(opts[:type]).fmap {|type| value = type.call(value)}
11
+ value
12
+ end
13
+
14
+ def match_schema?(attrs)
15
+ attrs.keys.include?(key)
16
+ end
17
+
18
+ attr_reader :key, :block, :opts, :name
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Massager
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/massager.rb CHANGED
@@ -4,33 +4,31 @@ require "dry-monads"
4
4
  require "dry-container"
5
5
  require "set"
6
6
 
7
- require "massager/attributes/attribute"
8
- require "massager/attributes/enum_attribute"
7
+ require "massager/attributes/attribute_with_single_key"
8
+ require "massager/attributes/attribute_with_multiple_keys"
9
9
 
10
10
  module Massager
11
11
  module ClassMethods
12
12
  def attribute(name, *target_keys, **opts, &block)
13
- register_attribute(
14
- Attribute.new(name: name, target_keys: target_keys, opts: opts, block: block)
15
- )
16
- add_keys_to_schema(opts, target_keys)
17
- define_setter(name)
18
- define_getter(name)
19
- end
20
-
21
- def enum_attribute(name, *target_keys, **opts, &block)
22
- register_attribute(
23
- EnumAttribute.new(name: name, target_keys: target_keys, opts: opts, block: block)
24
- )
13
+ case
14
+ when target_keys.count > 1
15
+ register_attribute(
16
+ AttributeWithMultipleKeys.new(name: name, keys: target_keys, opts: opts, block: block)
17
+ )
18
+ when target_keys.count == 1
19
+ register_attribute(
20
+ AttributeWithSingleKey.new(name: name, key: target_keys.first, opts: opts, block: block)
21
+ )
22
+ end
25
23
  add_keys_to_schema(opts, target_keys)
26
24
  define_setter(name)
27
25
  define_getter(name)
28
26
  end
29
27
 
30
- def build(attrs)
28
+ def call(attrs)
31
29
  check_schema(attrs)
32
30
  instance = new
33
- each_key do |k|
31
+ _container.each_key.select {|a| a.include?("attributes.")}.each do |k|
34
32
  attribute = resolve(k)
35
33
  instance.public_send("#{attribute.name}=", attrs) if attribute.match_schema?(attrs)
36
34
  end
@@ -41,9 +39,10 @@ module Massager
41
39
 
42
40
  def check_schema(attrs)
43
41
  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)
42
+ if _container.key?("schema")
43
+ schema = resolve("schema")
44
+ attr_keys = attr_keys.find_all {|a| schema.include?(a)}
45
+ raise ArgumentError, "Missing keys: #{(schema - attr_keys).to_a}" unless schema.subset?(attr_keys.to_set)
47
46
  end
48
47
  end
49
48
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: massager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janis Miezitis
@@ -99,8 +99,8 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - lib/massager.rb
102
- - lib/massager/attributes/attribute.rb
103
- - lib/massager/attributes/enum_attribute.rb
102
+ - lib/massager/attributes/attribute_with_multiple_keys.rb
103
+ - lib/massager/attributes/attribute_with_single_key.rb
104
104
  - lib/massager/version.rb
105
105
  - massager.gemspec
106
106
  homepage: http://github.com/janjiss/massager
@@ -1,29 +0,0 @@
1
- module Massager
2
- class Attribute
3
- def initialize(name:, target_keys:, opts: {}, block: nil)
4
- raise ArgumentError, "If you pass multiple keys, you have to use modifier block" if block.nil? && target_keys.count > 1
5
- @name, @target_keys, @opts, @block = name, target_keys, opts, block
6
- end
7
-
8
- def call(values)
9
- begin
10
- values = values.values_at(*target_keys)
11
- Dry::Monads::Maybe(block).fmap {|block| values = block.call(*values)}
12
- Dry::Monads::Maybe(opts[:type]).fmap {|type| values = type.call(*values)}
13
- return_result(*values)
14
- rescue ArgumentError
15
- raise ArgumentError, "The result of modifier block should return single element"
16
- end
17
- end
18
-
19
- def return_result(values)
20
- values
21
- end
22
-
23
- def match_schema?(attrs)
24
- (attrs.keys & target_keys).any?
25
- end
26
-
27
- attr_reader :target_keys, :block, :opts, :name
28
- end
29
- end
@@ -1,21 +0,0 @@
1
- module Massager
2
- class EnumAttribute
3
- def initialize(name:, target_keys:, opts: {}, block:)
4
- @name, @target_keys, @opts, @block = name, target_keys, opts, block
5
- end
6
-
7
- def call(values)
8
- values = values.values_at(*target_keys)
9
- Dry::Monads::Maybe(block).fmap {|block| values = block.call(values)}
10
- Dry::Monads::Maybe(opts[:type]).fmap {|type| values = type.call(values)}
11
- raise ArgumentError, "The result of modifier block is not an enum" unless values.respond_to? :each
12
- values
13
- end
14
-
15
- def match_schema?(attrs)
16
- (attrs.keys & target_keys).any?
17
- end
18
-
19
- attr_reader :target_keys, :block, :opts, :name
20
- end
21
- end