Class: Magicka::Aggregator

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/magicka/aggregator.rb,
lib/magicka/aggregator/class_methods.rb,
lib/magicka/aggregator/method_builder.rb

Overview

Class representing an element agregator, representing a model

Direct Known Subclasses

Display, Form

Defined Under Namespace

Modules: ClassMethods Classes: MethodBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

type, with_element

Constructor Details

#initialize(renderer, model) ⇒ Aggregator

Returns a new instance of Aggregator.

Parameters:

  • renderer (ActionView::Base)

    Object responsible for rendering

  • model (String)

    Model where the form elements will focus



48
49
50
51
# File 'lib/magicka/aggregator.rb', line 48

def initialize(renderer, model)
  @renderer = renderer
  @model    = model
end

Instance Attribute Details

#modelString (readonly)

Model where the form elements will focus

Returns:

  • (String)


14
15
16
# File 'lib/magicka/aggregator.rb', line 14

def model
  @model
end

Class Method Details

.with_element(element_class, method_name = nil, template: nil) ⇒ Array<NilClass> .with_element(element_class_name, method_name = nil, template: nil) ⇒ Array<NilClass>

Configure an Magicka::Aggregator adding a method to render an element

Overloads:

  • .with_element(element_class, method_name = nil, template: nil) ⇒ Array<NilClass>

    Parameters:

    • element_class (Class<Magicka::Element>)

      Class of the element to be rendered

    • method_name (String, Symbol) (defaults to: nil)

      Name of the method that will render the element

    • template (String) (defaults to: nil)

      custom template file to be used

  • .with_element(element_class_name, method_name = nil, template: nil) ⇒ Array<NilClass>

    Parameters:

    • element_class_name (String)

      String representation of a class of Element of the element to be rendered

    • method_name (String, Symbol) (defaults to: nil)

      Name of the method that will render the element

    • template (String) (defaults to: nil)

      custom template file to be used

Returns:

  • (Array<NilClass>)

See Also:



# File 'lib/magicka/aggregator.rb', line 21

Instance Method Details

#equal?(other) ⇒ TrueClass, FalseClass Also known as: ==

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if other aggragate is equal to this one

Parameters:

  • other (Object)

    object to be compared

Returns:

  • (TrueClass, FalseClass)


97
98
99
100
101
102
# File 'lib/magicka/aggregator.rb', line 97

def equal?(other)
  return unless other.class == self.class

  other.renderer == renderer &&
    other.model == model
end

#except(*types) ⇒ Object

Executes a block only when aggregator is not one of given types

Parameters:

  • types (Array<Symbol>)

    posssible types

Returns:

  • (Object)

    Result of the block



85
86
87
88
89
# File 'lib/magicka/aggregator.rb', line 85

def except(*types)
  return if types.include?(self.class.type)

  yield
end

#only(*types) ⇒ Object

Executes a block only when aggregator is one of given types

Parameters:

  • types (Array<Symbol>)

    posssible types

Returns:

  • (Object)

    Result of the block



74
75
76
77
78
# File 'lib/magicka/aggregator.rb', line 74

def only(*types)
  return unless types.include?(self.class.type)

  yield
end

#with_model(model, base: self.model) {|Aggregator| ... } ⇒ Aggregator

Returns a new aggregator focusing on a new model

The new model is an attribute of model unless base is given

Parameters:

  • model (String)

    Model where the form elements will focus

  • base (String) (defaults to: self.model)

    Model base

Yields:

  • (Aggregator)

    new aggregator focused in the new model

Returns:



63
64
65
66
67
# File 'lib/magicka/aggregator.rb', line 63

def with_model(model, base: self.model)
  new_model = [base, model].compact.join('.')

  yield self.class.new(renderer, new_model)
end