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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9a2098f3a03b8fe17ed203cfee173b4fa423c6ce9bbd74759e8502f77763ec8
4
- data.tar.gz: bd64101ba553ed5be452f64625bb66e690c3aea3f5ae8dedc39cc7bd70acdf14
3
+ metadata.gz: a4f301f9e859df07bfe34ad06884a7d88d4f7e4c32d36012e869a05ea0c1fd76
4
+ data.tar.gz: c65ce7cf16879bde4ac489c827ebc1dcb0216735609f3839321b91ba85f37b80
5
5
  SHA512:
6
- metadata.gz: c520ff513cec3f1369f6033c559ee027f00bef82e0ce33e014c7f9174ea7784feb4a1cdda8fb86af27079f71c621ee455fcf19f5f544f7338fec4a6207956e96
7
- data.tar.gz: ec6d451757a6d8f69a67530f48f1b0e52df25239e63c38c78f2dab5210d5c3e04baadff815cbf4bb4d9aa43be29d06a74855dac2ecedf54790569db878c995e0
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
- self.ar_models[ar_model] = instance_variable_get(:"@#{ar_model}")
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(self.ar_models).merge(local_params)
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 model, 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
- # 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)
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
- klazz.new(*args)
86
+ super
80
87
  end
81
- else
82
- super
83
88
  end
84
- end
85
89
 
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
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
- # Gets the path for the delegate views
112
- def self.view_path
113
- @view_path ||= to_s.sub(/Delegate/, ''.freeze).underscore
114
- end
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
- # 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
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
- # 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
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
- # 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
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
- # 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)
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
- 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)
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
@@ -1,4 +1,4 @@
1
1
  module ViewDelegates
2
2
  # Gem version
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.3.5'.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.3.0
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-04-26 00:00:00.000000000 Z
11
+ date: 2018-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails