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



40
41
42
43
# File 'lib/magicka/aggregator.rb', line 40

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

Instance Attribute Details

#modelString (readonly)

Model where the form elements will focus

Returns:

  • (String)


16
17
18
# File 'lib/magicka/aggregator.rb', line 16

def model
  @model
end

Class Method Details

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

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

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

Returns:

  • (Array<NilClass>)

See Also:



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

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)


89
90
91
92
93
94
# File 'lib/magicka/aggregator.rb', line 89

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



77
78
79
80
81
# File 'lib/magicka/aggregator.rb', line 77

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



66
67
68
69
70
# File 'lib/magicka/aggregator.rb', line 66

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:



55
56
57
58
59
# File 'lib/magicka/aggregator.rb', line 55

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

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