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

- (FilteredDelegationInstaller) initialize(target, options)

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

- (Object) attributes (readonly)

The attributes target, attributes, filters



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

def attributes
  @attributes
end

- (Object) filters (readonly)

The attributes target, attributes, filters



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

def filters
  @filters
end

- (Object) target (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

- (Object) filtered_left_parentheses

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

- (Object) install

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

- (Object) install_reader(attribute)

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

- (Object) left_parentheses

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

- (Object) reader_definition_for(attribute)

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

- (Object) right_parentheses

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