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