easy_params 0.8.0 → 0.9.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
  SHA256:
3
- metadata.gz: 520468dff4cbd537cef3eec5d4d0c15607e0616143812746c559a7697294bc1c
4
- data.tar.gz: 338dc40010d0a02ab8819289ad64e9355fa2ee302fbb93ae82723b15a9b8d94c
3
+ metadata.gz: 56cdc3f7e00c381786f025e6aca73baacbb8b8e7cf02aaa6d37c163db2c8463e
4
+ data.tar.gz: 531ef26595af187e646caa32a448c3c3b37b4d31f028d0202989424ea5761b90
5
5
  SHA512:
6
- metadata.gz: b0f00c2a25862648b027d96b0491ddfdc6f838cf5fef010c8fa263436afb2ea463f85a3ea8b46ca3839e4036d8a8a73fd98a577fb78f2f8a204a1515b130022f
7
- data.tar.gz: 86a0f9d29e30d44d40d3af84604d458b8b8cffc190d9e960de3a3858d550161b33970520ea3d3e03d3ad3e221532f0fc28a48abd3882eb0c760a186465328d21
6
+ metadata.gz: 59d619a8301928dc576050eee95959edb97a5dbaac9a7179e0d2d698317236707b7715208e8274db8c0379a15fdb5d5035a6b445eb7bc7543583668dcda0ffcb
7
+ data.tar.gz: 12a2e968a77f10af3c960bc57c6fc63b66c4dd2d1cbceff371114ac8f1c534e326ddf4b4365ccc4e826f979baa690b9661d54c1c9a3216bbdeac5e54f98248af
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_params (0.8.0)
4
+ easy_params (0.9.0)
5
5
  activemodel (>= 3.2)
6
6
 
7
7
  GEM
@@ -6,12 +6,18 @@ module EasyParams
6
6
  include ActiveModel::Model
7
7
  include EasyParams::Types::Struct
8
8
  include EasyParams::Validation
9
+ include EasyParams::Composition
9
10
 
10
11
  attr_writer :default
11
12
 
12
13
  def initialize(params = {})
13
14
  self.class.schema.each do |attr, type|
14
- public_send("#{attr}=", type.coerce(params.to_h[attr]))
15
+ coerced_type = type.coerce(params.to_h[attr])
16
+ if coerced_type && (type.is_a?(EasyParams::Base) || type.is_a?(EasyParams::Types::StructsCollection))
17
+ coerced_type.owner = self
18
+ end
19
+ coerced_type.each { |v| v.owner = coerced_type } if coerced_type.is_a?(EasyParams::Types::StructsCollection)
20
+ public_send("#{attr}=", coerced_type)
15
21
  end
16
22
  end
17
23
 
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EasyParams
4
+ # Provides composition helpers for EasyParams components.
5
+ module Composition
6
+ def self.included(base)
7
+ base.attr_accessor :owner
8
+ base.include(InstanceMethods)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods # rubocop:disable Style/Documentation
13
+ def part_of(owner_name)
14
+ @owner_name = owner_name
15
+ alias_method owner_name, :owner
16
+ end
17
+ end
18
+
19
+ module InstanceMethods # rubocop:disable Style/Documentation
20
+ def method_missing(name, *attrs, **kwargs, &block)
21
+ return super unless name.to_s.start_with?('owner_')
22
+
23
+ owner_method = name.to_s.sub('owner_', '').to_sym
24
+ return super unless (handler = owners_chain.lazy.detect { |o| o.public_methods.include?(owner_method) })
25
+
26
+ handler.public_send(owner_method, *attrs, **kwargs, &block)
27
+ end
28
+
29
+ def respond_to_missing?(method_name, include_private = false)
30
+ return super unless method_name.to_s.start_with?('owner_')
31
+
32
+ owners_chain.lazy.any? { |o| o.respond_to?(method_name.to_s.sub('owner_', '').to_sym, include_private) }
33
+ end
34
+
35
+ private
36
+
37
+ def owners_chain
38
+ @owners_chain ||= Enumerator.new do |y|
39
+ obj = self
40
+ y << obj = obj.owner while obj.public_methods.include?(:owner)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -41,6 +41,8 @@ module EasyParams
41
41
 
42
42
  # base interface for array of structs type
43
43
  class StructsCollection < Collection
44
+ include EasyParams::Composition
45
+
44
46
  def read_default
45
47
  @default
46
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyParams
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.0'
5
5
  end
data/lib/easy_params.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'active_model'
4
4
  require 'bigdecimal/util'
5
+ require 'easy_params/composition'
5
6
  require 'easy_params/types/generic'
6
7
  require 'easy_params/types/collection'
7
8
  require 'easy_params/types/struct'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran
@@ -48,6 +48,7 @@ files:
48
48
  - easy_params.gemspec
49
49
  - lib/easy_params.rb
50
50
  - lib/easy_params/base.rb
51
+ - lib/easy_params/composition.rb
51
52
  - lib/easy_params/types.rb
52
53
  - lib/easy_params/types/collection.rb
53
54
  - lib/easy_params/types/generic.rb