dryer_routes 0.5.1 → 0.5.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
  SHA256:
3
- metadata.gz: db3c5e4d0a5b6da6c17cd4c52744a5ef90382e6de723d17a3b8661408033c4df
4
- data.tar.gz: 409d7ba0396eea7e623d719861cd030438fa7dde30fbe7b71c72536acb962d6b
3
+ metadata.gz: 5c692380d53fff96240d6883e92c95894cd6eaf16ba0d18de34ae70eed293c0c
4
+ data.tar.gz: 3ce4ca0aa75e253a70f994c86efca79491257bd4562deff01f1621c3eff78d0c
5
5
  SHA512:
6
- metadata.gz: 062e66fb4a47a501e52bc88764c774ff8a2715b5fe0445bf33c3ca11436ada9846c79cc734f984f219b668845a41087b48614b04004d0fec82db8fc0b73e6861
7
- data.tar.gz: cecbf962ae3c78329a2a0aa2bc8da858cc15942ca1f07b60d1178bb32c86226b2aa69a6ed15b13c4d90617b83f441a00b37287227e6ace70770227224d5c57af
6
+ metadata.gz: 1fb38fc119af2c46a455ebe7e647060c02de3abff1604dd40ae2e63a324b44ee74180630d3d84f068866572db658b5cbfef4a30d6b28e7cd7648c63fb37523df
7
+ data.tar.gz: cc9083eb11bd03929b09e40be4cc1d6c33f824c0ba3fc91cee0f30f9b23d70287a13a2b590519aa9863775d29bfd6c9736c7b36a5028ec4812a2ef0981806d6e
data/dryer_routes.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'dryer_routes'
3
- spec.version = "0.5.1"
3
+ spec.version = "0.5.3"
4
4
  spec.authors = ['John Bernier']
5
5
  spec.email = ['john.b.bernier@gmail.com']
6
6
  spec.summary = 'Typed routing for rails leveraging dry-validation contracts'
@@ -11,9 +11,7 @@ module Dryer
11
11
  resource[:actions].map do |action, config|
12
12
  Route.new(
13
13
  controller: resource[:controller],
14
- url: Dryer::Routes::UrlBuilder.call(
15
- config[:url] || resource[:url]
16
- ),
14
+ url: config[:url] || resource[:url],
17
15
  method: config[:method],
18
16
  controller_action: action,
19
17
  request_contract: config[:request_contract],
@@ -1,16 +1,25 @@
1
+ require 'dryer_services'
2
+
1
3
  module Dryer
2
4
  module Routes
3
- class HashObject
4
- def initialize(hash)
5
+ class HashObject < Dryer::Services::SimpleService
6
+ def initialize(hash)
5
7
  hash.each do |k,v|
6
8
  key = k.is_a?(Numeric) ? "_#{k}" : k
7
9
 
8
- self.instance_variable_set(
9
- "@#{key}", v.is_a?(Hash) ? HashObject.new(v) : v
10
- )
11
- self.class.send(
12
- :define_method, key, proc{self.instance_variable_get("@#{key}")}
13
- )
10
+ if v.is_a?(Proc)
11
+ self.send(:define_singleton_method, key, proc do |*args, **kwargs, &block|
12
+ v.arity != 0 ? v.call(*args, **kwargs, &block) : v.call
13
+ end)
14
+ else
15
+ self.instance_variable_set(
16
+ "@#{key}", v.is_a?(Hash) ? HashObject.new(v) : v
17
+ )
18
+
19
+ self.send(
20
+ :define_singleton_method, key, proc{self.instance_variable_get("@#{key}")}
21
+ )
22
+ end
14
23
  end
15
24
  end
16
25
  end
@@ -14,7 +14,7 @@ module Dryer
14
14
  @routes = resources.map do |r|
15
15
  BuildFromResource.call(r)
16
16
  end.flatten
17
- add_accessors_for_resources(resources)
17
+ ResourceAccessors.call(object: self, resources: resources)
18
18
  @routes
19
19
  end
20
20
 
@@ -72,30 +72,6 @@ module Dryer
72
72
  private
73
73
  attr_writer :routes, :resources
74
74
 
75
- def add_accessors_for_resources(resources)
76
- denormalize_resources(resources).inject(self) do |obj, (key, value)|
77
- obj.define_singleton_method(key) { HashObject.new(value) }
78
- obj
79
- end
80
- end
81
-
82
- def denormalize_resources(resources)
83
- resources.inject({}) do | h, resource |
84
- h[
85
- resource[:controller].controller_name.to_sym
86
- ] = denormalize_resource(resource)
87
- h
88
- end
89
- end
90
-
91
- def denormalize_resource(resource)
92
- resource[:actions].each do |key, value|
93
- resource[:actions][key][:url] =
94
- resource[:actions][key][:url] || resource[:url]
95
- end
96
- resource.merge(resource[:actions])
97
- end
98
-
99
75
  def validate_resources!(resources)
100
76
  errors = resources.map do |r|
101
77
  ResourceSchema.new.call(r)
@@ -0,0 +1,41 @@
1
+ require 'dryer_services'
2
+
3
+ module Dryer
4
+ module Routes
5
+ class ResourceAccessors < Dryer::Services::SimpleService
6
+
7
+ def initialize(object:, resources:)
8
+ @object = object
9
+ @resources = resources
10
+ end
11
+
12
+ def call
13
+ denormalize_resources(resources).inject(object) do |obj, (key, value)|
14
+ obj.define_singleton_method(key) { HashObject.new(value) }
15
+ obj
16
+ end
17
+ end
18
+
19
+ private
20
+ attr_reader :object, :resources
21
+
22
+ def denormalize_resources(resources)
23
+ resources.inject({}) do | h, resource|
24
+ h[
25
+ resource[:controller].controller_name.to_sym
26
+ ] = denormalize_resource(resource)
27
+ h
28
+ end
29
+ end
30
+
31
+ def denormalize_resource(resource)
32
+ resource[:actions].each do |key, value|
33
+ resource[:actions][key][:url] = UrlBuilder.call(
34
+ resource[:actions][key][:url] || resource[:url]
35
+ )
36
+ end
37
+ resource.merge(resource[:actions])
38
+ end
39
+ end
40
+ end
41
+ end
data/lib/dryer/routes.rb CHANGED
@@ -3,9 +3,11 @@ require "rubygems"
3
3
 
4
4
  module Dryer
5
5
  module Routes
6
- VERSION = Gem::Specification::load(
7
- "./dryer_routes.gemspec"
8
- ).version
6
+ def self.version
7
+ Gem::Specification::load(
8
+ "./dryer_routes.gemspec"
9
+ ).version
10
+ end
9
11
 
10
12
  def self.loader
11
13
  @loader ||= Zeitwerk::Loader.new.tap do |loader|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dryer_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Bernier
@@ -111,6 +111,7 @@ files:
111
111
  - lib/dryer/routes/extract_validated_keys.rb
112
112
  - lib/dryer/routes/hash_object.rb
113
113
  - lib/dryer/routes/registry.rb
114
+ - lib/dryer/routes/resource_accessors.rb
114
115
  - lib/dryer/routes/resource_schema.rb
115
116
  - lib/dryer/routes/route.rb
116
117
  - lib/dryer/routes/url_builder.rb