activefunction 0.3.3 → 0.3.4

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: ed2b67a8be1601c72b18a3be2f053a183be0a59b56b344176487ab4e628f6241
4
- data.tar.gz: 48256c3c954462a328993f71897c4413bb9df5f738a48f27c221eabf9ce74b4e
3
+ metadata.gz: 0f8ffe6930725c9dce5663fe50a080f431c8234e3cc1d69a49ac76455e69d118
4
+ data.tar.gz: c5b9d34f893fdecd80311f759d51583f07aa6fbc568e983c24b303e1ca84e8f4
5
5
  SHA512:
6
- metadata.gz: f34764a21c44e461f65eaf467393316ba73119199b9c86f0d8936e8b42927858a2363528030f1e6dd420c56cc853b68f8f6a0ee7d6f2116c166aefb304890821
7
- data.tar.gz: cd748cf2973a364d34000c696a638e230c333c88d4aab632544a047a5b4c339f80dd19d93142127c9d7e44528240435c249fe97c75633a268b2ddc00ebccd723
6
+ metadata.gz: 9990d9da21bf90351a35299cd9aac85da3e590212d1905ca13abb43d2249650e18eafc644fb8e523db266b814b85f28d93b3ec71ea9f6b9696117122ada49906
7
+ data.tar.gz: 58b4e7e1fd6084db2b0d3130541ad349364852a6a68dbe2027c27af88d68dc1fe0550be65d809c30d88d5fdfa023147c6fdcf461f9b32b36a758cb6f5a0be3a1
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module ActiveFunction
6
+ class ParameterMissingError < Error
7
+ MESSAGE_TEMPLATE = "Missing parameter: %s"
8
+
9
+ attr_reader :message
10
+
11
+ def initialize(param)
12
+ MESSAGE_TEMPLATE % param
13
+ end
14
+ end
15
+
16
+ class UnpermittedParameterError < Error
17
+ MESSAGE_TEMPLATE = "Unpermitted parameter: %s"
18
+
19
+ attr_reader :message
20
+
21
+ def initialize(param)
22
+ MESSAGE_TEMPLATE % param
23
+ end
24
+ end
25
+
26
+ module Functions
27
+ module StrongParameters
28
+ def params
29
+ @_params ||= Parameters.new(@request)
30
+ end
31
+
32
+ class Parameters
33
+ extend Forwardable
34
+ def_delegators :@parameters, :each, :map
35
+ include Enumerable
36
+
37
+ def initialize(parameters, permitted: false)
38
+ @parameters = parameters
39
+ @permitted = permitted
40
+ end
41
+
42
+ def [](attribute)
43
+ nested_attribute(parameters[attribute])
44
+ end
45
+
46
+ def require(attribute)
47
+ value = self[attribute]
48
+
49
+ raise ParameterMissingError, attribute if value.nil?
50
+
51
+ value
52
+ end
53
+
54
+ def permit(*attributes)
55
+ pparams = {}
56
+
57
+ attributes.each do |attribute|
58
+ if attribute.is_a? Hash
59
+ attribute.each do |k, v|
60
+ pparams[k] = process_nested(self[k], :permit, v)
61
+ end
62
+ else
63
+ next unless parameters.key?(attribute)
64
+
65
+ pparams[attribute] = self[attribute]
66
+ end
67
+ end
68
+
69
+ Parameters.new(pparams, permitted: true)
70
+ end
71
+
72
+ def to_h
73
+ raise UnpermittedParameterError, parameters.keys unless @permitted
74
+
75
+ parameters.transform_values { |_1| process_nested(_1, :to_h) }
76
+ end
77
+
78
+ private
79
+
80
+ def nested_attribute(attribute)
81
+ if attribute.is_a? Hash
82
+ Parameters.new(attribute)
83
+ elsif attribute.is_a?(Array) && attribute[0].is_a?(Hash)
84
+ attribute.map { |_1| Parameters.new(_1) }
85
+ else
86
+ attribute
87
+ end
88
+ end
89
+
90
+ def process_nested(attribute, method, options = [])
91
+ if attribute.is_a? Parameters
92
+ attribute.send(method, *options)
93
+ elsif attribute.is_a?(Array) && attribute[0].is_a?(Parameters)
94
+ attribute.map { |_1| _1.send(method, *options) }
95
+ else
96
+ attribute
97
+ end
98
+ end
99
+
100
+ attr_reader :parameters
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveFunction
4
+ class MissingRouteMethod < Error
5
+ MESSAGE_TEMPLATE = "Missing function route: %s"
6
+
7
+ attr_reader :message
8
+
9
+ def initialize(context)
10
+ @message = MESSAGE_TEMPLATE % context
11
+ end
12
+ end
13
+
14
+ class NotRenderedError < Error
15
+ MESSAGE_TEMPLATE = "render was not called: %s"
16
+
17
+ attr_reader :message
18
+
19
+ def initialize(context)
20
+ @message = MESSAGE_TEMPLATE % context
21
+ end
22
+ end
23
+
24
+ module Functions
25
+ module Core
26
+ attr_reader :action_name, :request, :response
27
+
28
+ def dispatch(action_name, request, response)
29
+ @action_name = action_name
30
+ @request = request
31
+ @response = response
32
+
33
+ raise MissingRouteMethod, @action_name unless respond_to?(action_name)
34
+
35
+ process(@action_name)
36
+
37
+ raise NotRenderedError, @action_name unless performed?
38
+
39
+ @response.to_h
40
+ end
41
+
42
+ private
43
+
44
+ def process(action) ; public_send(action); end
45
+
46
+ def performed? ; @response.committed?; end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveFunction
4
+ module Functions
5
+ class Response
6
+ attr_accessor :status, :headers, :body
7
+
8
+ def initialize(status: 200, headers: {}, body: nil)
9
+ @status = status
10
+ @headers = headers
11
+ @body = body
12
+ @committed = false
13
+ end
14
+
15
+ def to_h
16
+ {
17
+ statusCode: status,
18
+ headers: headers,
19
+ body: body
20
+ }
21
+ end
22
+
23
+ def commit!
24
+ @committed = true
25
+ end
26
+
27
+ def committed? ; @committed; end
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveFunction
4
- VERSION = "0.3.3"
4
+ VERSION = "0.3.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activefunction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nerbyk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-11 00:00:00.000000000 Z
11
+ date: 2023-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-next-core
@@ -97,6 +97,9 @@ files:
97
97
  - bin/console
98
98
  - bin/rake
99
99
  - bin/setup
100
+ - lib/.rbnext/2.7/active_function/functions/strong_parameters.rb
101
+ - lib/.rbnext/3.0/active_function/functions/core.rb
102
+ - lib/.rbnext/3.0/active_function/functions/response.rb
100
103
  - lib/active_function.rb
101
104
  - lib/active_function/base.rb
102
105
  - lib/active_function/functions/callbacks.rb
@@ -129,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
132
  - !ruby/object:Gem::Version
130
133
  version: '0'
131
134
  requirements: []
132
- rubygems_version: 3.3.26
135
+ rubygems_version: 3.4.10
133
136
  signing_key:
134
137
  specification_version: 4
135
138
  summary: rails/action_controller like gem which provides callbacks, strong parameters