Class: ViewModels::Extensions::ModelReader::FilteredDelegationInstaller

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(target, options) ⇒ FilteredDelegationInstaller

Initialize a new filtered delegation

Parameters:

  • target (ViewModel)

    the target to install the filtered delegation in

  • options (ModelReader::Options)

    The options for the filtered delegation



76
77
78
# File 'lib/view_models/extensions/model_reader.rb', line 76

def initialize target, options
  @target, @attributes, @filters = target, *options
end

Instance Attribute Details

#attributesObject (readonly)

The attributes target, attributes, filters



70
71
72
# File 'lib/view_models/extensions/model_reader.rb', line 70

def attributes
  @attributes
end

#filtersObject (readonly)

The attributes target, attributes, filters



70
71
72
# File 'lib/view_models/extensions/model_reader.rb', line 70

def filters
  @filters
end

#targetObject (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_parenthesesObject

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

#installObject

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.

Examples:

Installs a reader for model.attribute

install_reader :attribute


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_parenthesesObject

Generates an array of left parentheses with length <amount of filters>

Examples:

for 4 Filters

left_parentheses # => ['(', '(', '(', '(']


121
122
123
# File 'lib/view_models/extensions/model_reader.rb', line 121

def left_parentheses
  ['('] * filters.size
end

#reader_definition_for(attribute) ⇒ Object

Note:

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_parenthesesObject

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