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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 441d41b7280b59cb1987f52c483f8a29b92437611a8e4920f04251c8ebcba3a5
4
- data.tar.gz: 85997a3ebaf6a666664e2f0027e4ffe3698c105b86b4a3e7a98dbc57513e78f8
3
+ metadata.gz: d9a2098f3a03b8fe17ed203cfee173b4fa423c6ce9bbd74759e8502f77763ec8
4
+ data.tar.gz: bd64101ba553ed5be452f64625bb66e690c3aea3f5ae8dedc39cc7bd70acdf14
5
5
  SHA512:
6
- metadata.gz: 24de4dc2169091d41b66918368415ad8a7896de00c66edfdca1f77f41a600f1aa5cc6cfaa85f3acd421ac710f8a184b1f86943620dbbb98a9eebdb0afc48573f
7
- data.tar.gz: 15670140091d3d129102df38aeb26170ecbb6462cac62e8ba8552408491bcafbcdc53288cc2b2ad799ec0378912bf7a93d81e79fa5d681a1703c58ac84f15f29
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
- # Override the new, we may need polymorphism
66
- # @param [Hash] args
67
- def new *args
68
- if defined? @polymorph_function
69
- command = super(*args)
70
- klazz = command.instance_eval(&@polymorph_function)
71
- if klazz == self
72
- super(*args)
73
- else
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
- super
79
+ klazz.new(*args)
78
80
  end
81
+ else
82
+ super
79
83
  end
84
+ end
80
85
 
81
- # Activates cache on the view delegate
82
- # option must be true/false
83
- # size is an optional parameter, controls the max size of the cache array
84
- # @param [Boolean] option
85
- # @param [Integer] size
86
- def cache(option, size: 50)
87
- if option
88
- render_method = instance_method :render
89
- @delegate_cache = ViewDelegates::Cache.new(max_size: size)
90
- define_method(:render) do |view, local_params: {}, &block|
91
- value_key = "#{hash}#{local_params.hash}#{view.to_s}"
92
- result = self.class.delegate_cache.get value_key
93
- if result.nil?
94
- result = render_method.bind(self).call(view, local_params: local_params)
95
- self.class.delegate_cache.add key: value_key, value: result
96
- end
97
- if block
98
- block.call(result)
99
- else
100
- result
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
- # Gets the path for the delegate views
107
- def view_path
108
- @view_path ||= to_s.sub(/Delegate/, ''.freeze).underscore
109
- end
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
- # Marks a method as a view local
112
- # @param [Symbol] method
113
- def view_local(*methods)
114
- @view_locals = [] if @view_locals.nil?
115
- methods.each do |method|
116
- @view_locals << method
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
- # View properties
121
- # @param [Symbol] method
122
- def property(*methods)
123
- @properties = [] if @properties.nil?
124
- methods.each do |method|
125
- @properties << method
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
- # Polymorphism method
131
- # The block must return the class we must use
132
- # @param [Proc] block
133
- def polymorph &block
134
- @polymorph_function = block
135
- end
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
- # The models this delegate will use
138
- # @param [method] method The variable name this model will use
139
- # @param [Array] properties The model properties to extract
140
- # from the active record model
141
- def model(method, properties: [])
142
- attr_accessor method
143
- @ar_models = [] if @ar_models.nil?
144
- # Add the method name to the array of delegate models
145
- @ar_models << method
146
- # Define a setter for the model
147
- define_method "#{method}=" do |val|
148
- # Create a struct with the model properties
149
- model_delegate = if properties.any?
150
- Struct.new(*properties)
151
- else
152
- Struct.new(*val.attributes.keys)
153
- end
154
- model_delegate = model_to_struct(val, model_delegate)
155
- # set the struct to instance model
156
- instance_variable_set(:"@#{method}", model_delegate)
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
- 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
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
@@ -1,4 +1,4 @@
1
1
  module ViewDelegates
2
2
  # Gem version
3
- VERSION = '0.2.3'.freeze
3
+ VERSION = '0.3.0'.freeze
4
4
  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.2.3
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-23 00:00:00.000000000 Z
11
+ date: 2018-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails