Class: ViewModels::Extensions::ModelReader::FilteredDelegationInstaller
- Inherits:
-
Object
- Object
- ViewModels::Extensions::ModelReader::FilteredDelegationInstaller
- Defined in:
- lib/view_models/extensions/model_reader.rb
Overview
The filtered delegation installer installs delegators on the target that are filtered.
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
The attributes target, attributes, filters.
-
#filters ⇒ Object
readonly
The attributes target, attributes, filters.
-
#target ⇒ Object
readonly
The attributes target, attributes, filters.
Instance Method Summary collapse
-
#filtered_left_parentheses ⇒ Object
Combines left parentheses and filters.
-
#initialize(target, options) ⇒ FilteredDelegationInstaller
constructor
Initialize a new filtered delegation.
-
#install ⇒ Object
Install a reader for each attribute.
-
#install_reader(attribute) ⇒ Object
Install a reader for the given name with the given filters.
-
#left_parentheses ⇒ Object
Generates an array of left parentheses with length <amount of filters>.
-
#reader_definition_for(attribute) ⇒ Object
Defines a reader for the given model attribute and filtering through the given filters.
-
#right_parentheses ⇒ Object
Generates the needed amount of parentheses to match the left parentheses.
Constructor Details
#initialize(target, options) ⇒ FilteredDelegationInstaller
Initialize a new filtered delegation
76 77 78 |
# File 'lib/view_models/extensions/model_reader.rb', line 76 def initialize target, @target, @attributes, @filters = target, * end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
The attributes target, attributes, filters
70 71 72 |
# File 'lib/view_models/extensions/model_reader.rb', line 70 def attributes @attributes end |
#filters ⇒ Object (readonly)
The attributes target, attributes, filters
70 71 72 |
# File 'lib/view_models/extensions/model_reader.rb', line 70 def filters @filters end |
#target ⇒ Object (readonly)
The attributes target, attributes, filters
70 71 72 |
# File 'lib/view_models/extensions/model_reader.rb', line 70 def target @target end |
Instance Method Details
#filtered_left_parentheses ⇒ Object
Combines left parentheses and filters.
106 107 108 |
# File 'lib/view_models/extensions/model_reader.rb', line 106 def filtered_left_parentheses filters.zip(left_parentheses).join end |
#install ⇒ Object
Install a reader for each attribute
82 83 84 |
# File 'lib/view_models/extensions/model_reader.rb', line 82 def install attributes.each { |attribute| install_reader(attribute) } end |
#install_reader(attribute) ⇒ Object
Install a reader for the given name with the given filters.
91 92 93 |
# File 'lib/view_models/extensions/model_reader.rb', line 91 def install_reader attribute target.class_eval reader_definition_for(attribute) end |
#left_parentheses ⇒ Object
Generates an array of left parentheses with length <amount of filters>
121 122 123 |
# File 'lib/view_models/extensions/model_reader.rb', line 121 def left_parentheses ['('] * filters.size end |
#reader_definition_for(attribute) ⇒ Object
The filters are applied from last to first element.
Defines a reader for the given model attribute and filtering through the given filters.
100 101 102 |
# File 'lib/view_models/extensions/model_reader.rb', line 100 def reader_definition_for attribute "def #{attribute}; #{filtered_left_parentheses}model.#{attribute}#{right_parentheses}; end" end |
#right_parentheses ⇒ Object
Generates the needed amount of parentheses to match the left parentheses.
112 113 114 |
# File 'lib/view_models/extensions/model_reader.rb', line 112 def right_parentheses ')' * filters.size end |