Class: ViewDelegates::ViewDelegate
- Inherits:
-
Object
- Object
- ViewDelegates::ViewDelegate
- Defined in:
- lib/view_delegates/poros/view_delegate.rb
Overview
Base class for delegates
Class Attribute Summary collapse
-
.delegate_cache ⇒ ViewDelegates::Cache
View delegate cache system.
-
.polymorph_function ⇒ Object
Returns the value of attribute polymorph_function.
Class Method Summary collapse
-
.cache(option, size: 50) ⇒ Object
Activates cache on the view delegate option must be true/false size is an optional parameter, controls the max size of the cache array.
-
.helper(*methods) ⇒ Object
Marks a method as view helper.
- .helpers_name ⇒ Object
-
.model(method, properties: []) ⇒ Object
The models this delegate will use from the active record model.
- .model_array(method, properties: []) ⇒ Object
-
.new(*args) ⇒ Object
Override the new, we may need polymorphism.
-
.polymorph(&block) ⇒ Object
Polymorphism method The block must return the class we must use.
-
.property(*methods) ⇒ Object
View properties.
-
.view_local(*methods) ⇒ Object
Marks a method as a view local.
-
.view_path ⇒ Object
Gets the path for the delegate views.
Instance Method Summary collapse
-
#initialize(view_data = {}) ⇒ ViewDelegate
constructor
Initialize method.
-
#render(view, local_params: {}, &block) ⇒ Object
Renders as a string the view passed as params.
Constructor Details
#initialize(view_data = {}) ⇒ ViewDelegate
Initialize method
30 31 32 33 34 35 36 37 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 30 def initialize(view_data = {}) self.class.ar_models&.each do |t| send("#{t}=", view_data[t]) if view_data[t] end self.class.properties&.each do |t| send("#{t}=", view_data[t]) if view_data[t] end end |
Class Attribute Details
.delegate_cache ⇒ ViewDelegates::Cache
View delegate cache system
20 21 22 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 20 def delegate_cache @delegate_cache end |
.polymorph_function ⇒ Object
Returns the value of attribute polymorph_function.
5 6 7 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 5 def polymorph_function @polymorph_function end |
Class Method Details
.cache(option, size: 50) ⇒ Object
Activates cache on the view delegate option must be true/false size is an optional parameter, controls the max size of the cache array
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 114 def cache(option, size: 50) if option render_method = instance_method :render @delegate_cache = ViewDelegates::Cache.new(max_size: size) define_method(:render) do |view, local_params: {}, &block| value_key = "#{hash}#{local_params.hash}#{view.to_s}" result = self.class.delegate_cache.get value_key if result.nil? result = render_method.bind(self).call(view, local_params: local_params) self.class.delegate_cache.add key: value_key, value: result end if block block.call(result) else result end end end end |
.helper(*methods) ⇒ Object
Marks a method as view helper
147 148 149 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 147 def helper(*methods) self.view_helpers += methods end |
.helpers_name ⇒ Object
38 39 40 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 38 def self.helpers_name "#{to_s.gsub(/^.*::/, '')}".sub(/Delegate/, ''.freeze).concat('Helper').underscore end |
.model(method, properties: []) ⇒ Object
The models this delegate will use from the active record model
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 171 def model(method, properties: []) attr_accessor method # Add the method name to the array of delegate models self.ar_models += [method] # Define a setter for the model define_method "#{method}=" do |val| # Create a struct with the model properties model_delegate = if properties.any? Struct.new(*properties) else Struct.new(*val.attributes.keys) end model_delegate = model_to_struct(val, model_delegate) # set the struct to instance model instance_variable_set(:"@#{method}", model_delegate) end end |
.model_array(method, properties: []) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 189 def model_array(method, properties: []) attr_accessor method # Add the method name to the array of delegate models self.ar_models += [method] # Define a setter for the model define_method "#{method}=" do |model_array| # Create a struct with the model properties model_delegate = if properties.any? Struct.new(*properties) else Struct.new(*val.attributes.keys) end model_array.map! {|e| model_to_struct(e, model_delegate)} instance_variable_set(:"@#{method}", model_array) end end |
.new(*args) ⇒ Object
Override the new, we may need polymorphism
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 95 def new(*args) if @polymorph_function command = super(*args) klazz = command.instance_eval(&@polymorph_function) if klazz == self super(*args) else klazz.new(*args) end else super end end |
.polymorph(&block) ⇒ Object
Polymorphism method The block must return the class we must use
163 164 165 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 163 def polymorph &block @polymorph_function = block end |
.property(*methods) ⇒ Object
View properties
153 154 155 156 157 158 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 153 def property(*methods) methods.each do |method| attr_accessor method end self.properties += methods end |
.view_local(*methods) ⇒ Object
Marks a method as a view local
141 142 143 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 141 def view_local(*methods) self.view_locals += methods end |
.view_path ⇒ Object
Gets the path for the delegate views
135 136 137 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 135 def view_path @view_path ||= to_s.sub(/Delegate/, ''.freeze).underscore end |
Instance Method Details
#render(view, local_params: {}, &block) ⇒ Object
Renders as a string the view passed as params
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/view_delegates/poros/view_delegate.rb', line 43 def render(view, local_params: {}, &block) locals = {} self.class.view_locals&.each do |method| locals[method] = send(method) end self.ar_models = {} self.view_helpers = {} self.class.ar_models&.each do |ar_model| ar_models[ar_model] = instance_variable_get(:"@#{ar_model}") end self.class.properties&.each do |property| locals[property] = instance_variable_get "@#{property}" end module_helpers = self.class.view_helpers module_methods = {} for method in module_helpers module_methods[method] = method(method) end helper_obj = Struct.new(self.class.helpers_name.camelcase) do module_helpers.each do |view_helper| define_method view_helper do |*args| module_methods[view_helper].call(*args) end end end.new locals = locals.merge(ar_models).merge(local_params) locals[self.class.helpers_name.to_sym] = helper_obj result = ViewDelegateController.render(self.class.view_path + '/' + view.to_s, locals: locals) if block block.call(result) else result end end |