massager 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ea5940423c363a2f3d765f9504e4477fa7dc899
4
- data.tar.gz: 31eab0624ed8ceeb8db534553337b128cc57ba2a
3
+ metadata.gz: bb5f8e586ab12d52bd6754f918c955a8bcfc7b35
4
+ data.tar.gz: de71786ff585c916936b4c6d6f3a9309ca054518
5
5
  SHA512:
6
- metadata.gz: dcd086a0ddcb18e9e7c5befa11c85376641e2d6e181f0f6c775c9005a2438989e2b1d59ce3a0deb241e413382cf2114da64372edc479cd9fca88928db8655bff
7
- data.tar.gz: 16546d2ed9e8b1f56aa77bebc010bc29c940ff924b269607ecc14adabd5217e5915e1bd963717919cab5ae1cbc600cb84fd4594bcde0d5d40c2ee0735aef1902
6
+ metadata.gz: ca80f2aa124b47bd794ab56fd94aba597b1534349561e7588ad3cd1c88f191d1dd66ebba9d5c4334ce1fd1397b1eece186f45edc36d9c911faf4cfc11b2ef1d8
7
+ data.tar.gz: 5d16cc90a4e2301f5b297c27458958134624eceda586bcc420a58943bf11e71d28073cee298bc22df3c33c724e1534b32b4348f1022754a42da5a41af5c642b9
@@ -4,10 +4,27 @@ require "dry-monads"
4
4
  require "dry-container"
5
5
  require "set"
6
6
 
7
+ require "massager/errors/constraint_error"
7
8
  require "massager/attributes/attribute_with_single_key"
8
9
  require "massager/attributes/attribute_with_multiple_keys"
10
+ require "massager/hashify"
9
11
 
10
12
  module Massager
13
+ module InstanceMethods
14
+ def [](name)
15
+ public_send(name)
16
+ end
17
+
18
+ def to_hash
19
+ self.class._container.keys.select {|a| a.include?("attributes.")}.each_with_object({}) do |key, result|
20
+ attribute_name = key.gsub("attributes.", "").to_sym
21
+ result[attribute_name] = Hashify[self[attribute_name]]
22
+ end
23
+ end
24
+
25
+ alias_method :to_h, :to_hash
26
+ end
27
+
11
28
  module ClassMethods
12
29
  def attribute(name, *target_keys, **opts, &block)
13
30
  case
@@ -92,5 +109,6 @@ module Massager
92
109
 
93
110
  def self.included(base)
94
111
  base.extend(ClassMethods)
112
+ base.include(InstanceMethods)
95
113
  end
96
114
  end
@@ -8,7 +8,13 @@ module Massager
8
8
  def call(values)
9
9
  values = values.values_at(*keys)
10
10
  values = block.call(*values)
11
- Dry::Monads::Maybe(opts[:type]).fmap {|type| values = type.call(*values)}
11
+ Dry::Monads::Maybe(opts[:type]).fmap {|type|
12
+ begin
13
+ value = type.call(values)
14
+ rescue Dry::Types::ConstraintError
15
+ raise Massager::ConstraintError, "Attribute #{name} did not pass the constraint for value of #{values.inspect}"
16
+ end
17
+ }
12
18
  values
13
19
  end
14
20
 
@@ -7,7 +7,13 @@ module Massager
7
7
  def call(values)
8
8
  value = values.fetch(key)
9
9
  Dry::Monads::Maybe(block).fmap {|block| value = block.call(value)}
10
- Dry::Monads::Maybe(opts[:type]).fmap {|type| value = type.call(value)}
10
+ Dry::Monads::Maybe(opts[:type]).fmap {|type|
11
+ begin
12
+ value = type.call(value)
13
+ rescue Dry::Types::ConstraintError
14
+ raise Massager::ConstraintError, "Attribute #{name} did not pass the constraint for value of #{value}"
15
+ end
16
+ }
11
17
  value
12
18
  end
13
19
 
@@ -0,0 +1,4 @@
1
+ module Massager
2
+ class ConstraintError < RuntimeError
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Massager
2
+ module Hashify
3
+ def self.[](value)
4
+ if value.respond_to?(:to_hash)
5
+ value.to_hash
6
+ elsif value.respond_to?(:map)
7
+ value.map { |item| self[item] }
8
+ else
9
+ value
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Massager
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  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.3.2
4
+ version: 0.3.3
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-19 00:00:00.000000000 Z
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-types
@@ -101,6 +101,8 @@ files:
101
101
  - lib/massager.rb
102
102
  - lib/massager/attributes/attribute_with_multiple_keys.rb
103
103
  - lib/massager/attributes/attribute_with_single_key.rb
104
+ - lib/massager/errors/constraint_error.rb
105
+ - lib/massager/hashify.rb
104
106
  - lib/massager/version.rb
105
107
  - massager.gemspec
106
108
  homepage: http://github.com/janjiss/massager