view_delegates 0.2.3 → 0.3.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/lib/view_delegates/poros/view_delegate.rb +107 -104
- data/lib/view_delegates/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9a2098f3a03b8fe17ed203cfee173b4fa423c6ce9bbd74759e8502f77763ec8
|
4
|
+
data.tar.gz: bd64101ba553ed5be452f64625bb66e690c3aea3f5ae8dedc39cc7bd70acdf14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c520ff513cec3f1369f6033c559ee027f00bef82e0ce33e014c7f9174ea7784feb4a1cdda8fb86af27079f71c621ee455fcf19f5f544f7338fec4a6207956e96
|
7
|
+
data.tar.gz: ec6d451757a6d8f69a67530f48f1b0e52df25239e63c38c78f2dab5210d5c3e04baadff815cbf4bb4d9aa43be29d06a74855dac2ecedf54790569db878c995e0
|
@@ -3,18 +3,22 @@ module ViewDelegates
|
|
3
3
|
class ViewDelegate
|
4
4
|
class << self
|
5
5
|
attr_accessor :polymorph_function
|
6
|
-
attr_accessor :view_locals
|
7
|
-
attr_accessor :models
|
8
|
-
attr_accessor :ar_models
|
9
|
-
attr_accessor :properties
|
10
6
|
attr_accessor :delegate_cache
|
11
7
|
end
|
8
|
+
class_attribute :view_locals
|
9
|
+
class_attribute :models
|
10
|
+
class_attribute :ar_models
|
11
|
+
class_attribute :properties
|
12
12
|
# View delegate cache system
|
13
13
|
# @return [ViewDelegates::Cache]
|
14
|
-
def delegate_cache
|
14
|
+
def self.delegate_cache
|
15
15
|
@delegate_cache
|
16
16
|
end
|
17
17
|
|
18
|
+
def self.polymorph_function
|
19
|
+
@polymorph_function
|
20
|
+
end
|
21
|
+
|
18
22
|
# Initialize method
|
19
23
|
# @param [Hash] view_data hash containing all delegate properties
|
20
24
|
def initialize(view_data = {})
|
@@ -33,14 +37,14 @@ module ViewDelegates
|
|
33
37
|
self.class.view_locals&.each do |method|
|
34
38
|
locals[method] = send(method)
|
35
39
|
end
|
36
|
-
ar_models = {}
|
40
|
+
self.ar_models = {}
|
37
41
|
self.class.ar_models&.each do |ar_model|
|
38
|
-
ar_models[ar_model] = instance_variable_get(:"@#{ar_model}")
|
42
|
+
self.ar_models[ar_model] = instance_variable_get(:"@#{ar_model}")
|
39
43
|
end
|
40
44
|
self.class.properties&.each do |property|
|
41
45
|
locals[property] = instance_variable_get "@#{property}"
|
42
46
|
end
|
43
|
-
locals = locals.merge(ar_models).merge(local_params)
|
47
|
+
locals = locals.merge(self.ar_models).merge(local_params)
|
44
48
|
result = ViewDelegateController.render(self.class.view_path + '/' + view.to_s,
|
45
49
|
locals: locals)
|
46
50
|
|
@@ -50,6 +54,7 @@ module ViewDelegates
|
|
50
54
|
result
|
51
55
|
end
|
52
56
|
end
|
57
|
+
|
53
58
|
private
|
54
59
|
|
55
60
|
def model_to_struct model, struct
|
@@ -60,118 +65,116 @@ module ViewDelegates
|
|
60
65
|
end
|
61
66
|
struct.new(*initialize_hash.values_at(*struct_members))
|
62
67
|
end
|
63
|
-
class << self
|
64
68
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
klazz.new(*args)
|
75
|
-
end
|
69
|
+
|
70
|
+
# Override the new, we may need polymorphism
|
71
|
+
# @param [Hash] args
|
72
|
+
def self.new *args
|
73
|
+
if @polymorph_function
|
74
|
+
command = super(*args)
|
75
|
+
klazz = command.instance_eval(&@polymorph_function)
|
76
|
+
if klazz == self
|
77
|
+
super(*args)
|
76
78
|
else
|
77
|
-
|
79
|
+
klazz.new(*args)
|
78
80
|
end
|
81
|
+
else
|
82
|
+
super
|
79
83
|
end
|
84
|
+
end
|
80
85
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
86
|
+
# Activates cache on the view delegate
|
87
|
+
# option must be true/false
|
88
|
+
# size is an optional parameter, controls the max size of the cache array
|
89
|
+
# @param [Boolean] option
|
90
|
+
# @param [Integer] size
|
91
|
+
def self.cache(option, size: 50)
|
92
|
+
if option
|
93
|
+
render_method = instance_method :render
|
94
|
+
@delegate_cache = ViewDelegates::Cache.new(max_size: size)
|
95
|
+
define_method(:render) do |view, local_params: {}, &block|
|
96
|
+
value_key = "#{hash}#{local_params.hash}#{view.to_s}"
|
97
|
+
result = self.class.delegate_cache.get value_key
|
98
|
+
if result.nil?
|
99
|
+
result = render_method.bind(self).call(view, local_params: local_params)
|
100
|
+
self.class.delegate_cache.add key: value_key, value: result
|
101
|
+
end
|
102
|
+
if block
|
103
|
+
block.call(result)
|
104
|
+
else
|
105
|
+
result
|
102
106
|
end
|
103
107
|
end
|
104
108
|
end
|
109
|
+
end
|
105
110
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
111
|
+
# Gets the path for the delegate views
|
112
|
+
def self.view_path
|
113
|
+
@view_path ||= to_s.sub(/Delegate/, ''.freeze).underscore
|
114
|
+
end
|
110
115
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
end
|
118
|
-
end
|
116
|
+
# Marks a method as a view local
|
117
|
+
# @param [Symbol] method
|
118
|
+
def self.view_local(*methods)
|
119
|
+
self.view_locals = [] if view_locals.nil?
|
120
|
+
self.view_locals += methods
|
121
|
+
end
|
119
122
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
attr_accessor method
|
127
|
-
end
|
123
|
+
# View properties
|
124
|
+
# @param [Symbol] method
|
125
|
+
def self.property(*methods)
|
126
|
+
self.properties = [] if self.properties.nil?
|
127
|
+
methods.each do |method|
|
128
|
+
attr_accessor method
|
128
129
|
end
|
130
|
+
self.properties += methods
|
131
|
+
end
|
129
132
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
133
|
+
# Polymorphism method
|
134
|
+
# The block must return the class we must use
|
135
|
+
# @param [Proc] block
|
136
|
+
def self.polymorph &block
|
137
|
+
@polymorph_function = block
|
138
|
+
end
|
136
139
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
end
|
140
|
+
# The models this delegate will use
|
141
|
+
# @param [method] method The variable name this model will use
|
142
|
+
# @param [Array] properties The model properties to extract
|
143
|
+
# from the active record model
|
144
|
+
def self.model(method, properties: [])
|
145
|
+
attr_accessor method
|
146
|
+
self.ar_models = [] if self.ar_models.nil?
|
147
|
+
# Add the method name to the array of delegate models
|
148
|
+
self.ar_models += [method]
|
149
|
+
# Define a setter for the model
|
150
|
+
define_method "#{method}=" do |val|
|
151
|
+
# Create a struct with the model properties
|
152
|
+
model_delegate = if properties.any?
|
153
|
+
Struct.new(*properties)
|
154
|
+
else
|
155
|
+
Struct.new(*val.attributes.keys)
|
156
|
+
end
|
157
|
+
model_delegate = model_to_struct(val, model_delegate)
|
158
|
+
# set the struct to instance model
|
159
|
+
instance_variable_set(:"@#{method}", model_delegate)
|
158
160
|
end
|
161
|
+
end
|
159
162
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
163
|
+
def self.model_array method, properties: []
|
164
|
+
attr_accessor method
|
165
|
+
# Add the method name to the array of delegate models
|
166
|
+
self.ar_models = [] if self.ar_models.nil?
|
167
|
+
self.ar_models += [method]
|
168
|
+
# Define a setter for the model
|
169
|
+
define_method "#{method}=" do |model_array|
|
170
|
+
# Create a struct with the model properties
|
171
|
+
model_delegate = if properties.any?
|
172
|
+
Struct.new(*properties)
|
173
|
+
else
|
174
|
+
Struct.new(*val.attributes.keys)
|
175
|
+
end
|
176
|
+
model_array.map! {|e| model_to_struct(e, model_delegate)}
|
177
|
+
instance_variable_set(:"@#{method}", model_array)
|
175
178
|
end
|
176
179
|
end
|
177
180
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: view_delegates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Miguel Aurelio Lamas Murias
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|