view_delegates 0.2.2 → 0.2.3
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 +55 -27
- data/lib/view_delegates/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 441d41b7280b59cb1987f52c483f8a29b92437611a8e4920f04251c8ebcba3a5
|
4
|
+
data.tar.gz: 85997a3ebaf6a666664e2f0027e4ffe3698c105b86b4a3e7a98dbc57513e78f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24de4dc2169091d41b66918368415ad8a7896de00c66edfdca1f77f41a600f1aa5cc6cfaa85f3acd421ac710f8a184b1f86943620dbbb98a9eebdb0afc48573f
|
7
|
+
data.tar.gz: 15670140091d3d129102df38aeb26170ecbb6462cac62e8ba8552408491bcafbcdc53288cc2b2ad799ec0378912bf7a93d81e79fa5d681a1703c58ac84f15f29
|
@@ -1,26 +1,27 @@
|
|
1
1
|
module ViewDelegates
|
2
2
|
# Base class for delegates
|
3
3
|
class ViewDelegate
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
class << self
|
5
|
+
attr_accessor :polymorph_function
|
6
|
+
attr_accessor :view_locals
|
7
|
+
attr_accessor :models
|
8
|
+
attr_accessor :ar_models
|
9
|
+
attr_accessor :properties
|
10
|
+
attr_accessor :delegate_cache
|
11
|
+
end
|
11
12
|
# View delegate cache system
|
12
13
|
# @return [ViewDelegates::Cache]
|
13
14
|
def delegate_cache
|
14
|
-
|
15
|
+
@delegate_cache
|
15
16
|
end
|
16
17
|
|
17
18
|
# Initialize method
|
18
19
|
# @param [Hash] view_data hash containing all delegate properties
|
19
20
|
def initialize(view_data = {})
|
20
|
-
|
21
|
+
self.class.ar_models&.each do |t|
|
21
22
|
send("#{t}=", view_data[t]) if view_data[t]
|
22
23
|
end
|
23
|
-
|
24
|
+
self.class.properties&.each do |t|
|
24
25
|
send("#{t}=", view_data[t]) if view_data[t]
|
25
26
|
end
|
26
27
|
end
|
@@ -29,14 +30,14 @@ module ViewDelegates
|
|
29
30
|
# @param [Symbol] view
|
30
31
|
def render(view, local_params: {}, &block)
|
31
32
|
locals = {}
|
32
|
-
|
33
|
+
self.class.view_locals&.each do |method|
|
33
34
|
locals[method] = send(method)
|
34
35
|
end
|
35
36
|
ar_models = {}
|
36
|
-
|
37
|
+
self.class.ar_models&.each do |ar_model|
|
37
38
|
ar_models[ar_model] = instance_variable_get(:"@#{ar_model}")
|
38
39
|
end
|
39
|
-
|
40
|
+
self.class.properties&.each do |property|
|
40
41
|
locals[property] = instance_variable_get "@#{property}"
|
41
42
|
end
|
42
43
|
locals = locals.merge(ar_models).merge(local_params)
|
@@ -49,14 +50,24 @@ module ViewDelegates
|
|
49
50
|
result
|
50
51
|
end
|
51
52
|
end
|
53
|
+
private
|
52
54
|
|
55
|
+
def model_to_struct model, struct
|
56
|
+
initialize_hash = {}
|
57
|
+
struct_members = struct.members
|
58
|
+
struct_members.each do |k|
|
59
|
+
initialize_hash[k] = model.send k
|
60
|
+
end
|
61
|
+
struct.new(*initialize_hash.values_at(*struct_members))
|
62
|
+
end
|
53
63
|
class << self
|
64
|
+
|
54
65
|
# Override the new, we may need polymorphism
|
55
66
|
# @param [Hash] args
|
56
67
|
def new *args
|
57
|
-
if defined?
|
68
|
+
if defined? @polymorph_function
|
58
69
|
command = super(*args)
|
59
|
-
klazz = command.instance_eval(
|
70
|
+
klazz = command.instance_eval(&@polymorph_function)
|
60
71
|
if klazz == self
|
61
72
|
super(*args)
|
62
73
|
else
|
@@ -75,13 +86,13 @@ module ViewDelegates
|
|
75
86
|
def cache(option, size: 50)
|
76
87
|
if option
|
77
88
|
render_method = instance_method :render
|
78
|
-
|
89
|
+
@delegate_cache = ViewDelegates::Cache.new(max_size: size)
|
79
90
|
define_method(:render) do |view, local_params: {}, &block|
|
80
91
|
value_key = "#{hash}#{local_params.hash}#{view.to_s}"
|
81
|
-
result =
|
92
|
+
result = self.class.delegate_cache.get value_key
|
82
93
|
if result.nil?
|
83
94
|
result = render_method.bind(self).call(view, local_params: local_params)
|
84
|
-
|
95
|
+
self.class.delegate_cache.add key: value_key, value: result
|
85
96
|
end
|
86
97
|
if block
|
87
98
|
block.call(result)
|
@@ -100,24 +111,27 @@ module ViewDelegates
|
|
100
111
|
# Marks a method as a view local
|
101
112
|
# @param [Symbol] method
|
102
113
|
def view_local(*methods)
|
114
|
+
@view_locals = [] if @view_locals.nil?
|
103
115
|
methods.each do |method|
|
104
|
-
|
116
|
+
@view_locals << method
|
105
117
|
end
|
106
118
|
end
|
107
119
|
|
108
120
|
# View properties
|
109
121
|
# @param [Symbol] method
|
110
122
|
def property(*methods)
|
123
|
+
@properties = [] if @properties.nil?
|
111
124
|
methods.each do |method|
|
112
|
-
|
125
|
+
@properties << method
|
113
126
|
attr_accessor method
|
114
127
|
end
|
115
128
|
end
|
129
|
+
|
116
130
|
# Polymorphism method
|
117
131
|
# The block must return the class we must use
|
118
132
|
# @param [Proc] block
|
119
133
|
def polymorph &block
|
120
|
-
|
134
|
+
@polymorph_function = block
|
121
135
|
end
|
122
136
|
|
123
137
|
# The models this delegate will use
|
@@ -126,8 +140,9 @@ module ViewDelegates
|
|
126
140
|
# from the active record model
|
127
141
|
def model(method, properties: [])
|
128
142
|
attr_accessor method
|
143
|
+
@ar_models = [] if @ar_models.nil?
|
129
144
|
# Add the method name to the array of delegate models
|
130
|
-
|
145
|
+
@ar_models << method
|
131
146
|
# Define a setter for the model
|
132
147
|
define_method "#{method}=" do |val|
|
133
148
|
# Create a struct with the model properties
|
@@ -136,15 +151,28 @@ module ViewDelegates
|
|
136
151
|
else
|
137
152
|
Struct.new(*val.attributes.keys)
|
138
153
|
end
|
139
|
-
|
140
|
-
model_delegate.members.each do |k|
|
141
|
-
initialize_hash[k] = val.send k
|
142
|
-
end
|
143
|
-
model_delegate = model_delegate.new(*initialize_hash.values_at(*model_delegate.members))
|
154
|
+
model_delegate = model_to_struct(val, model_delegate)
|
144
155
|
# set the struct to instance model
|
145
156
|
instance_variable_set(:"@#{method}", model_delegate)
|
146
157
|
end
|
147
158
|
end
|
159
|
+
|
160
|
+
def models method, properties: []
|
161
|
+
attr_accessor method
|
162
|
+
# Add the method name to the array of delegate models
|
163
|
+
@ar_models << method
|
164
|
+
# Define a setter for the model
|
165
|
+
define_method "#{method}=" do |model_array|
|
166
|
+
# Create a struct with the model properties
|
167
|
+
model_delegate = if properties.any?
|
168
|
+
Struct.new(*properties)
|
169
|
+
else
|
170
|
+
Struct.new(*val.attributes.keys)
|
171
|
+
end
|
172
|
+
model_array.map! {|e| model_to_struct(e, model_delegate)}
|
173
|
+
instance_variable_set(:"@#{method}", model_array)
|
174
|
+
end
|
175
|
+
end
|
148
176
|
end
|
149
177
|
end
|
150
178
|
end
|