actionfacade 0.1.0.1 → 0.2.4
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/actionfacade.gemspec +1 -0
- data/lib/action_facade/retrieval.rb +71 -2
- data/test/base_test.rb +1 -2
- data/test/inheritance_test.rb +1 -2
- data/test/rails_app/retrieval_test.rb +77 -0
- data/test/rails_app/test_app.rb +16 -0
- data/test/retrieval_test.rb +61 -6
- data/test/test_helper.rb +2 -0
- metadata +23 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36b54b632384d17a03da67a3c06b17af8f34d3860f4982d34a2085a0435d2050
|
4
|
+
data.tar.gz: d5b6cc6da5f073f084b42718f5cf00dbb5c33842aec0ba86eec5107ead413957
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cc9020127e0d5684dc29330a2a2d060b5466c56b2a14d59b2c26207b188ef20cf2a5010963b47c6b34b57f627afdd5107addb24c5e04badf3ff7bfb18141112
|
7
|
+
data.tar.gz: ec3730ca6f2a004aa8180e1d209ee9e5c5f400edb5e167c167afbc74b842d4bb162477f78e0b0202123c3cdc2552030a00ab9bfa0708eaac7730f6ed1c4142d7
|
data/actionfacade.gemspec
CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
s.test_files = s.files.grep(/^test/)
|
25
25
|
|
26
26
|
s.add_dependency "bundler", ">= 1.3"
|
27
|
+
s.add_dependency "activesupport", ">= 5.2"
|
27
28
|
s.add_development_dependency "rake", ">= 12.3.3"
|
28
29
|
s.add_development_dependency "test-unit", ">= 3.3"
|
29
30
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_support/core_ext/string/inflections"
|
4
|
+
|
3
5
|
module ActionFacade
|
4
6
|
# == Action \Facade \Retrieval
|
5
7
|
#
|
@@ -11,13 +13,80 @@ module ActionFacade
|
|
11
13
|
module Retrieval
|
12
14
|
# Retrieve data from `facade`
|
13
15
|
#
|
16
|
+
# If `facade_or_other` is a facade, retrieve data from it.
|
17
|
+
# If it is a string or a symbol, it will be merged to `variable_names` and
|
18
|
+
# retrieve_from method called with controller params.
|
19
|
+
# If it is a hash, retrieve_from method will be called with the hash as a payload.
|
20
|
+
#
|
14
21
|
# `variable_names` are symbols for the method names in the facade and
|
15
22
|
# they will be set as instance variables in the class that
|
16
23
|
# includes the module.
|
17
|
-
def retrieve(
|
18
|
-
|
24
|
+
def retrieve(facade_or_other, *variable_names)
|
25
|
+
facade = nil
|
26
|
+
names = variable_names.dup
|
27
|
+
case facade_or_other
|
28
|
+
when ActionFacade::Base
|
29
|
+
facade = facade_or_other
|
30
|
+
when String, Symbol
|
31
|
+
if defined?(params)
|
32
|
+
names.unshift(facade_or_other)
|
33
|
+
return retrieve_from(params, *names)
|
34
|
+
else
|
35
|
+
raise ArgumentError.new("Can't call the method with #{facade_or_other.class} if params is undefined.")
|
36
|
+
end
|
37
|
+
when Hash
|
38
|
+
return retrieve_from(facade_or_other, *variable_names)
|
39
|
+
else
|
40
|
+
raise ArgumentError.new("First parameter must be instance of ActionFacade::Base, String, Symbol or Hash")
|
41
|
+
end
|
42
|
+
names.each do |name|
|
19
43
|
instance_variable_set("@#{name}", facade.send(name.to_sym))
|
20
44
|
end
|
21
45
|
end
|
46
|
+
|
47
|
+
# Retrieve data from given payload
|
48
|
+
#
|
49
|
+
# If the class which includes the module is Rails controller,
|
50
|
+
# guessed facade name will be "Controller" is replaced by the action name + "Facade".
|
51
|
+
# If the class is not Rails controller, the name will be suffixed by "Facade".
|
52
|
+
#
|
53
|
+
# `payload` is the initialization parameter for the facade.
|
54
|
+
#
|
55
|
+
# `variable_names` are symbols for the method names in the facade and
|
56
|
+
# they will be set as instance variables in the class that
|
57
|
+
# includes the module.
|
58
|
+
def retrieve_from(payload, *variable_names)
|
59
|
+
facade = guess_facade
|
60
|
+
if facade.nil?
|
61
|
+
raise FacadeNotFoundError.new("Could not find Facade class #{guess_facade_name}.")
|
62
|
+
end
|
63
|
+
retrieve(facade.new(payload), *variable_names)
|
64
|
+
end
|
65
|
+
|
66
|
+
class FacadeNotFoundError < StandardError; end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def guess_facade
|
71
|
+
facade_name = guess_facade_name
|
72
|
+
begin
|
73
|
+
facade_name.constantize
|
74
|
+
rescue NameError
|
75
|
+
nil
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def guess_facade_name
|
80
|
+
klass_name = self.class.name
|
81
|
+
if klass_name.end_with?("Controller")
|
82
|
+
if defined?(params) && params[:action]
|
83
|
+
klass_name.delete_suffix("Controller") + "::#{params[:action].camelize}Facade"
|
84
|
+
else
|
85
|
+
klass_name.delete_suffix("Controller") + "Facade"
|
86
|
+
end
|
87
|
+
else
|
88
|
+
klass_name + "Facade"
|
89
|
+
end
|
90
|
+
end
|
22
91
|
end
|
23
92
|
end
|
data/test/base_test.rb
CHANGED
data/test/inheritance_test.rb
CHANGED
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../test_helper"
|
4
|
+
require_relative "./test_app"
|
5
|
+
|
6
|
+
module RailsApp
|
7
|
+
USER_DATA = [{ id: 1, name: "john" }, { id: 2, name: "bob" }]
|
8
|
+
end
|
9
|
+
|
10
|
+
module Home; end
|
11
|
+
|
12
|
+
class Home::IndexFacade < ActionFacade::Base
|
13
|
+
def all_users
|
14
|
+
RailsApp::USER_DATA
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class HomeController < ActionController::Base
|
19
|
+
include Rails.application.routes.url_helpers
|
20
|
+
include ActionFacade::Retrieval
|
21
|
+
|
22
|
+
def index
|
23
|
+
case params[:retrieve]
|
24
|
+
when "facade"
|
25
|
+
facade = Home::IndexFacade.new
|
26
|
+
retrieve(facade, :all_users)
|
27
|
+
when "string"
|
28
|
+
retrieve("all_users")
|
29
|
+
when "symbol"
|
30
|
+
retrieve(:all_users)
|
31
|
+
when "hash"
|
32
|
+
retrieve({}, :all_users)
|
33
|
+
else
|
34
|
+
retrieve_from({}, :all_users)
|
35
|
+
end
|
36
|
+
render plain: @all_users
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
require "rack/test"
|
41
|
+
|
42
|
+
module RailsApp; end
|
43
|
+
|
44
|
+
class RailsApp::RetrievalTest < Test::Unit::TestCase
|
45
|
+
include Rack::Test::Methods
|
46
|
+
|
47
|
+
test "@all_users is set after retrieve_from({}, :all_users)" do
|
48
|
+
get "/"
|
49
|
+
assert_equal(last_response.body, RailsApp::USER_DATA.to_s)
|
50
|
+
end
|
51
|
+
|
52
|
+
test "@all_users is set after retrieve(facade, :all_users)" do
|
53
|
+
get "/", params: { retrieve: "facade" }
|
54
|
+
assert_equal(last_response.body, RailsApp::USER_DATA.to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
test "@all_users is set after retrieve(:all_users)" do
|
58
|
+
get "/", params: { retrieve: "symbol" }
|
59
|
+
assert_equal(last_response.body, RailsApp::USER_DATA.to_s)
|
60
|
+
end
|
61
|
+
|
62
|
+
test "@all_users is set after retrieve(\"all_users\")" do
|
63
|
+
get "/", params: { retrieve: "string" }
|
64
|
+
assert_equal(last_response.body, RailsApp::USER_DATA.to_s)
|
65
|
+
end
|
66
|
+
|
67
|
+
test "@all_users is set after retrieve({}, :all_users)" do
|
68
|
+
get "/", params: { retrieve: "hash" }
|
69
|
+
assert_equal(last_response.body, RailsApp::USER_DATA.to_s)
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def app
|
75
|
+
Rails.application
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_controller/railtie"
|
4
|
+
|
5
|
+
class TestApp < Rails::Application
|
6
|
+
config.root = __dir__
|
7
|
+
config.hosts << "example.org"
|
8
|
+
secrets.secret_key_base = "secret_key_base"
|
9
|
+
|
10
|
+
config.logger = Logger.new($stdout)
|
11
|
+
Rails.logger = config.logger
|
12
|
+
|
13
|
+
routes.draw do
|
14
|
+
get "/" => "home#index"
|
15
|
+
end
|
16
|
+
end
|
data/test/retrieval_test.rb
CHANGED
@@ -1,15 +1,21 @@
|
|
1
|
-
|
2
|
-
require_relative "../lib/action_facade/base.rb"
|
3
|
-
require_relative "../lib/action_facade/retrieval.rb"
|
1
|
+
require_relative "./test_helper"
|
4
2
|
|
5
|
-
USER_DATA = [{ id: 1, name: "john" }]
|
3
|
+
USER_DATA = [{ id: 1, name: "john" }, { id: 2, name: "bob" }]
|
6
4
|
|
7
|
-
class
|
5
|
+
class UsersFacade < ActionFacade::Base
|
8
6
|
def john
|
9
7
|
USER_DATA.find { |user| user[:name] == "john" }
|
10
8
|
end
|
11
9
|
end
|
12
10
|
|
11
|
+
module Admin
|
12
|
+
class ShowFacade < ActionFacade::Base
|
13
|
+
def bob
|
14
|
+
USER_DATA.find { |user| user[:name] == "bob" }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
13
19
|
class UsersController
|
14
20
|
include ActionFacade::Retrieval
|
15
21
|
|
@@ -17,9 +23,26 @@ class UsersController
|
|
17
23
|
|
18
24
|
def show
|
19
25
|
payload = {}
|
20
|
-
facade =
|
26
|
+
facade = UsersFacade.new(payload)
|
21
27
|
retrieve(facade, :john)
|
22
28
|
end
|
29
|
+
|
30
|
+
def show_symbol
|
31
|
+
retrieve(:john)
|
32
|
+
end
|
33
|
+
|
34
|
+
def show_string
|
35
|
+
retrieve("john")
|
36
|
+
end
|
37
|
+
|
38
|
+
def show_hash
|
39
|
+
retrieve({}, :john)
|
40
|
+
end
|
41
|
+
|
42
|
+
def show_from
|
43
|
+
payload = {}
|
44
|
+
retrieve_from(payload, :john)
|
45
|
+
end
|
23
46
|
end
|
24
47
|
|
25
48
|
class RetrievalTest < Test::Unit::TestCase
|
@@ -33,4 +56,36 @@ class RetrievalTest < Test::Unit::TestCase
|
|
33
56
|
controller.show
|
34
57
|
assert_equal(controller.john, { id: 1, name: "john" })
|
35
58
|
end
|
59
|
+
|
60
|
+
test "raised exception when retrieve(:john)" do
|
61
|
+
controller = UsersController.new
|
62
|
+
assert_nil(controller.john)
|
63
|
+
error = assert_raises ArgumentError do
|
64
|
+
controller.show_symbol
|
65
|
+
end
|
66
|
+
assert_equal(error.message, "Can't call the method with Symbol if params is undefined.")
|
67
|
+
end
|
68
|
+
|
69
|
+
test "raised exception when retrieve(\"john\")" do
|
70
|
+
controller = UsersController.new
|
71
|
+
assert_nil(controller.john)
|
72
|
+
error = assert_raises ArgumentError do
|
73
|
+
controller.show_string
|
74
|
+
end
|
75
|
+
assert_equal(error.message, "Can't call the method with String if params is undefined.")
|
76
|
+
end
|
77
|
+
|
78
|
+
test "@john is set after retrieve({}, :john)" do
|
79
|
+
controller = UsersController.new
|
80
|
+
assert_nil(controller.john)
|
81
|
+
controller.show_hash
|
82
|
+
assert_equal(controller.john, { id: 1, name: "john" })
|
83
|
+
end
|
84
|
+
|
85
|
+
test "@john is set after retrieve_from(payload, :john)" do
|
86
|
+
controller = UsersController.new
|
87
|
+
assert_nil(controller.john)
|
88
|
+
controller.show_from
|
89
|
+
assert_equal(controller.john, { id: 1, name: "john" })
|
90
|
+
end
|
36
91
|
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionfacade
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Hashimoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.2'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,7 +80,10 @@ files:
|
|
66
80
|
- lib/actionfacade.rb
|
67
81
|
- test/base_test.rb
|
68
82
|
- test/inheritance_test.rb
|
83
|
+
- test/rails_app/retrieval_test.rb
|
84
|
+
- test/rails_app/test_app.rb
|
69
85
|
- test/retrieval_test.rb
|
86
|
+
- test/test_helper.rb
|
70
87
|
homepage: https://github.com/ryohashimoto/lightrails
|
71
88
|
licenses:
|
72
89
|
- MIT
|
@@ -86,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
103
|
- !ruby/object:Gem::Version
|
87
104
|
version: 1.8.11
|
88
105
|
requirements: []
|
89
|
-
rubygems_version: 3.
|
106
|
+
rubygems_version: 3.1.4
|
90
107
|
signing_key:
|
91
108
|
specification_version: 4
|
92
109
|
summary: Action Facade provides a simple interface for data required by view / controller
|
@@ -94,4 +111,7 @@ summary: Action Facade provides a simple interface for data required by view / c
|
|
94
111
|
test_files:
|
95
112
|
- test/base_test.rb
|
96
113
|
- test/inheritance_test.rb
|
114
|
+
- test/rails_app/retrieval_test.rb
|
115
|
+
- test/rails_app/test_app.rb
|
97
116
|
- test/retrieval_test.rb
|
117
|
+
- test/test_helper.rb
|