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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1648039de8d610f3ccc9e8cd6fee86695778a3ae3e90875b277c3b1bfb6ba67c
4
- data.tar.gz: 1971fdebc9a6be05ffcadbe5f6616b4083e3577c2b168d6ef016cc5b9996390c
3
+ metadata.gz: 441d41b7280b59cb1987f52c483f8a29b92437611a8e4920f04251c8ebcba3a5
4
+ data.tar.gz: 85997a3ebaf6a666664e2f0027e4ffe3698c105b86b4a3e7a98dbc57513e78f8
5
5
  SHA512:
6
- metadata.gz: 969bc93127d9115c0087d7648c1fd207464787fd0efcf9c01fb255b918c8423c015ef032f3130f7af0168f8f96fb37db4aed528c07e676a14de3df06320b2346
7
- data.tar.gz: f9b6de9f6789b71556f2f0947424033a35a621a5d87ded4603defbb4b24bee007e775dbe1c55daf0737baea1381e166d3d88ec04d87204ac9e67d684c8e5275f
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
- # This property will contain all the methods the view delegate
5
- # will execute to add as view locals
6
- @@view_locals = []
7
- # All models this view delegate will contain
8
- @@ar_models = []
9
- # Properties
10
- @@properties = []
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
- @@delegate_cache
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
- @@ar_models.each do |t|
21
+ self.class.ar_models&.each do |t|
21
22
  send("#{t}=", view_data[t]) if view_data[t]
22
23
  end
23
- @@properties.each do |t|
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
- @@view_locals.each do |method|
33
+ self.class.view_locals&.each do |method|
33
34
  locals[method] = send(method)
34
35
  end
35
36
  ar_models = {}
36
- @@ar_models.each do |ar_model|
37
+ self.class.ar_models&.each do |ar_model|
37
38
  ar_models[ar_model] = instance_variable_get(:"@#{ar_model}")
38
39
  end
39
- @@properties.each do |property|
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? @@polymorph_function
68
+ if defined? @polymorph_function
58
69
  command = super(*args)
59
- klazz = command.instance_eval(&@@polymorph_function)
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
- @@delegate_cache = ViewDelegates::Cache.new(max_size: size)
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 = @@delegate_cache.get value_key
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
- @@delegate_cache.add key: value_key, value: result
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
- @@view_locals << method
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
- @@properties << method
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
- @@polymorph_function = block
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
- @@ar_models << method
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
- initialize_hash = {}
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
@@ -1,4 +1,4 @@
1
1
  module ViewDelegates
2
2
  # Gem version
3
- VERSION = '0.2.2'.freeze
3
+ VERSION = '0.2.3'.freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_delegates
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Aurelio Lamas Murias