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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/easy_params/base.rb +7 -1
- data/lib/easy_params/composition.rb +45 -0
- data/lib/easy_params/types/collection.rb +2 -0
- data/lib/easy_params/version.rb +1 -1
- data/lib/easy_params.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 56cdc3f7e00c381786f025e6aca73baacbb8b8e7cf02aaa6d37c163db2c8463e
|
|
4
|
+
data.tar.gz: 531ef26595af187e646caa32a448c3c3b37b4d31f028d0202989424ea5761b90
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 59d619a8301928dc576050eee95959edb97a5dbaac9a7179e0d2d698317236707b7715208e8274db8c0379a15fdb5d5035a6b445eb7bc7543583668dcda0ffcb
|
|
7
|
+
data.tar.gz: 12a2e968a77f10af3c960bc57c6fc63b66c4dd2d1cbceff371114ac8f1c534e326ddf4b4365ccc4e826f979baa690b9661d54c1c9a3216bbdeac5e54f98248af
|
data/Gemfile.lock
CHANGED
data/lib/easy_params/base.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
data/lib/easy_params/version.rb
CHANGED
data/lib/easy_params.rb
CHANGED
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.
|
|
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
|