view_delegates 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e86b85a56ceaba2da6e3064fafbab4ca1f26f47908b631130eb3b648d39cf61c
4
- data.tar.gz: 6f87036ea78a719456b6831d7a1bb43fad387f90917120a52a66fc1f07671ee2
3
+ metadata.gz: 1648039de8d610f3ccc9e8cd6fee86695778a3ae3e90875b277c3b1bfb6ba67c
4
+ data.tar.gz: 1971fdebc9a6be05ffcadbe5f6616b4083e3577c2b168d6ef016cc5b9996390c
5
5
  SHA512:
6
- metadata.gz: 5953364fa322a3ba872cae75ac18303f692795f4d963fa35804b96a928decba145acbd4860105baeffebb4fd8db75626cb5f6d46d92d84e4049336ddd061b136
7
- data.tar.gz: 79b37df27e0f53d51b54bb85071e0d9654653e97e9f001b200cd882e8e7325bb7ea063a84a4ae23db79ad6b8031f2954195b62b635432ccaf0af7ca455a4df5a
6
+ metadata.gz: 969bc93127d9115c0087d7648c1fd207464787fd0efcf9c01fb255b918c8423c015ef032f3130f7af0168f8f96fb37db4aed528c07e676a14de3df06320b2346
7
+ data.tar.gz: f9b6de9f6789b71556f2f0947424033a35a621a5d87ded4603defbb4b24bee007e775dbe1c55daf0737baea1381e166d3d88ec04d87204ac9e67d684c8e5275f
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  <a href="https://codeclimate.com/github/coreSegmentFault/view_delegates/maintainability"><img src="https://api.codeclimate.com/v1/badges/a74e2a9f9198b29683a2/maintainability" /></a>
2
+ [![Gem Version](https://badge.fury.io/rb/view_delegates.svg)](https://badge.fury.io/rb/view_delegates)
3
+ - Work in progress
2
4
  # ViewDelegates
3
5
  ViewDelegates makes easy to write reusable view components with decoupled functionality from
4
6
  the active record models
@@ -16,6 +18,13 @@ just add your logic.
16
18
  view_local :test_method
17
19
  property :my_property
18
20
  model :dummy, properties: [:a]
21
+ polymorph do
22
+ if my_property == 1
23
+ BasicDelegate
24
+ else
25
+ PolymorphicDelegate
26
+ end
27
+ end
19
28
  cache true
20
29
  def test_method
21
30
  'test_method'
@@ -28,6 +37,8 @@ just add your logic.
28
37
  - model also creates an accessor but permits to reject other properties not wanted to have in your view. pass to to the properties array any properties you will need in your view
29
38
  and the rest will be discarted
30
39
  - cache is an optional parameter, pass a size: parameter to change the default size of the cache pool. Default: 50
40
+ - polymorph gives polymorphism to our delegates, just return a class based on conditions of the properties of your base delegate to transform your view delegate
41
+ this is really useful in case of STI based models.
31
42
 
32
43
  ## Render views
33
44
  Create an instance from the delegate you want to render
@@ -1,4 +1,6 @@
1
+ # View delegate minimal controller for rendering
1
2
  class ViewDelegateController < ActionController::Base
3
+ # Unnecesary modules to remove
2
4
  MODULES = [
3
5
  ActionController::ConditionalGet,
4
6
  ActionController::EtagWithTemplateDigest,
@@ -1,4 +1,5 @@
1
1
  module ViewDelegates
2
+ # View delegates plugin
2
3
  class Engine < ::Rails::Engine
3
4
  end
4
5
  end
@@ -1,22 +1,30 @@
1
1
  module ViewDelegates
2
+ # View cache class
2
3
  class Cache
4
+ # Internal struct to save all the values
3
5
  CacheEntry = Struct.new(:key, :value)
6
+ # Accessor for the current entries
7
+ # Entries is an array, the most recent elements are at the end of the array
4
8
  attr_accessor :entries
9
+ # Initializer
10
+ # @param [Integer] max_size
5
11
  def initialize(max_size: 10)
6
12
  @entries = []
7
13
  @max_size = max_size
8
14
  end
9
-
15
+ # Add a new entry
16
+ # @param [Symbol] key
17
+ # @param [String] value
10
18
  def add(key:, value:)
11
19
  @entries << CacheEntry.new(key, value)
12
- if @entries.length > @max_size
13
- @entries.delete_at 0
14
- end
20
+ # If the array is full, remove the first element, since its the oldest
21
+ @entries.delete_at 0 if @entries.length > @max_size
15
22
  end
16
-
23
+ # Get an entry
24
+ # @param [Symbol] key
17
25
  def get(key)
18
26
  result = nil
19
- index = @entries.index { |e| e.key == key }
27
+ index = @entries.index { |e| e.key == key } rescue byebug
20
28
  if index
21
29
  result = @entries[index].value
22
30
  update_element index
@@ -25,14 +33,15 @@ module ViewDelegates
25
33
  end
26
34
 
27
35
  private
28
-
36
+ # Put the element at index one step to the right
37
+ # @param [Integer] index
29
38
  def update_element(index)
30
39
  before = index - 1
31
40
  start = []
32
41
  start = @entries[0..before] unless before.negative?
33
42
  @entries = start, @entries[index..(index + 1)].reverse,
34
43
  @entries[(index + 2)..-1]
35
- @entries = @entries.flatten.uniq
44
+ @entries = @entries.flatten.uniq.reject &:nil?
36
45
  end
37
46
  end
38
47
  end
@@ -13,6 +13,7 @@ module ViewDelegates
13
13
  def delegate_cache
14
14
  @@delegate_cache
15
15
  end
16
+
16
17
  # Initialize method
17
18
  # @param [Hash] view_data hash containing all delegate properties
18
19
  def initialize(view_data = {})
@@ -26,8 +27,8 @@ module ViewDelegates
26
27
 
27
28
  # Renders as a string the view passed as params
28
29
  # @param [Symbol] view
29
- def render(view, local_params: {})
30
- locals = {}.merge(local_params)
30
+ def render(view, local_params: {}, &block)
31
+ locals = {}
31
32
  @@view_locals.each do |method|
32
33
  locals[method] = send(method)
33
34
  end
@@ -38,92 +39,112 @@ module ViewDelegates
38
39
  @@properties.each do |property|
39
40
  locals[property] = instance_variable_get "@#{property}"
40
41
  end
41
- locals = locals.merge(ar_models)
42
- ViewDelegateController.render(self.class.view_path + '/' + view.to_s,
43
- locals: locals)
42
+ locals = locals.merge(ar_models).merge(local_params)
43
+ result = ViewDelegateController.render(self.class.view_path + '/' + view.to_s,
44
+ locals: locals)
45
+
46
+ if block
47
+ block.call(result)
48
+ else
49
+ result
50
+ end
44
51
  end
45
- class << self
46
52
 
47
- def new *args
48
- if defined? @@polymorph_function
49
- command = (super(*args))
50
- klazz = command.instance_eval(&@@polymorph_function)
51
- if klazz == self
52
- super(*args)
53
- else
54
- klazz.new(*args)
55
- end
53
+ class << self
54
+ # Override the new, we may need polymorphism
55
+ # @param [Hash] args
56
+ def new *args
57
+ if defined? @@polymorph_function
58
+ command = super(*args)
59
+ klazz = command.instance_eval(&@@polymorph_function)
60
+ if klazz == self
61
+ super(*args)
56
62
  else
57
- super
63
+ klazz.new(*args)
58
64
  end
65
+ else
66
+ super
59
67
  end
60
- def cache(option, size: 50)
61
- if option
62
- render_method = instance_method :render
63
- @@delegate_cache = ViewDelegates::Cache.new(max_size: size)
64
- define_method(:render) do |view, local_params: {}|
65
- value_key = "#{hash}#{view.to_s}"
66
- cache = @@delegate_cache.get value_key
67
- if cache.nil?
68
- rendered = render_method.bind(self).call(view, local_params)
69
- @@delegate_cache.add key: value_key, value: rendered
70
- rendered
71
- else
72
- cache
73
- end
68
+ end
69
+
70
+ # Activates cache on the view delegate
71
+ # option must be true/false
72
+ # size is an optional parameter, controls the max size of the cache array
73
+ # @param [Boolean] option
74
+ # @param [Integer] size
75
+ def cache(option, size: 50)
76
+ if option
77
+ render_method = instance_method :render
78
+ @@delegate_cache = ViewDelegates::Cache.new(max_size: size)
79
+ define_method(:render) do |view, local_params: {}, &block|
80
+ value_key = "#{hash}#{local_params.hash}#{view.to_s}"
81
+ result = @@delegate_cache.get value_key
82
+ if result.nil?
83
+ result = render_method.bind(self).call(view, local_params: local_params)
84
+ @@delegate_cache.add key: value_key, value: result
85
+ end
86
+ if block
87
+ block.call(result)
88
+ else
89
+ result
74
90
  end
75
91
  end
76
92
  end
77
- # Gets the path for the delegate views
78
- def view_path
79
- @view_path ||= to_s.sub(/Delegate/, ''.freeze).underscore
80
- end
93
+ end
81
94
 
82
- # Marks a method as a view local
83
- # @param [Symbol] method
84
- def view_local(*methods)
85
- methods.each do |method|
86
- @@view_locals << method
87
- end
88
- end
95
+ # Gets the path for the delegate views
96
+ def view_path
97
+ @view_path ||= to_s.sub(/Delegate/, ''.freeze).underscore
98
+ end
89
99
 
90
- # View properties
91
- # @param [Symbol] method
92
- def property(*methods)
93
- methods.each do |method|
94
- @@properties << method
95
- attr_accessor method
96
- end
100
+ # Marks a method as a view local
101
+ # @param [Symbol] method
102
+ def view_local(*methods)
103
+ methods.each do |method|
104
+ @@view_locals << method
97
105
  end
106
+ end
98
107
 
99
- def polymorph &block
100
- @@polymorph_function = block
101
- end
102
- # The models this delegate will use
103
- # @param [method] method The variable name this model will use
104
- # @param [Array] properties The model properties to extract
105
- # from the active record model
106
- def model(method, properties: [])
108
+ # View properties
109
+ # @param [Symbol] method
110
+ def property(*methods)
111
+ methods.each do |method|
112
+ @@properties << method
107
113
  attr_accessor method
108
- # Add the method name to the array of delegate models
109
- @@ar_models << method
110
- # Define a setter for the model
111
- define_method "#{method}=" do |val|
112
- # Create a struct with the model properties
113
- model_delegate = if properties.any?
114
- Struct.new(*properties)
115
- else
116
- Struct.new(*val.attributes.keys)
117
- end
118
- initialize_hash = {}
119
- model_delegate.members.each do |k|
120
- initialize_hash[k] = val.send k
121
- end
122
- model_delegate = model_delegate.new(*initialize_hash.values_at(*model_delegate.members))
123
- # set the struct to instance model
124
- instance_variable_set(:"@#{method}", model_delegate)
114
+ end
115
+ end
116
+ # Polymorphism method
117
+ # The block must return the class we must use
118
+ # @param [Proc] block
119
+ def polymorph &block
120
+ @@polymorph_function = block
121
+ end
122
+
123
+ # The models this delegate will use
124
+ # @param [method] method The variable name this model will use
125
+ # @param [Array] properties The model properties to extract
126
+ # from the active record model
127
+ def model(method, properties: [])
128
+ attr_accessor method
129
+ # Add the method name to the array of delegate models
130
+ @@ar_models << method
131
+ # Define a setter for the model
132
+ define_method "#{method}=" do |val|
133
+ # Create a struct with the model properties
134
+ model_delegate = if properties.any?
135
+ Struct.new(*properties)
136
+ else
137
+ Struct.new(*val.attributes.keys)
138
+ end
139
+ initialize_hash = {}
140
+ model_delegate.members.each do |k|
141
+ initialize_hash[k] = val.send k
125
142
  end
143
+ model_delegate = model_delegate.new(*initialize_hash.values_at(*model_delegate.members))
144
+ # set the struct to instance model
145
+ instance_variable_set(:"@#{method}", model_delegate)
126
146
  end
127
147
  end
148
+ end
128
149
  end
129
150
  end
@@ -1,3 +1,4 @@
1
1
  module ViewDelegates
2
- VERSION = '0.2.1'.freeze
2
+ # Gem version
3
+ VERSION = '0.2.2'.freeze
3
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.1
4
+ version: 0.2.2
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-22 00:00:00.000000000 Z
11
+ date: 2018-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -104,10 +104,7 @@ files:
104
104
  - MIT-LICENSE
105
105
  - README.md
106
106
  - Rakefile
107
- - app/assets/javascripts/view_delegate.js
108
- - app/assets/stylesheets/view_delegate.css
109
107
  - app/controllers/view_delegate_controller.rb
110
- - app/helpers/view_delegate_helper.rb
111
108
  - lib/view_delegates.rb
112
109
  - lib/view_delegates/engine.rb
113
110
  - lib/view_delegates/poros/cache.rb
@@ -1,2 +0,0 @@
1
- // Place all the behaviors and hooks related to the matching controller here.
2
- // All this logic will automatically be available in application.js.
@@ -1,4 +0,0 @@
1
- /*
2
- Place all the styles related to the matching controller here.
3
- They will automatically be included in application.css.
4
- */
@@ -1,2 +0,0 @@
1
- module ViewDelegateHelper
2
- end