Class: Merb::Controller::Aggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/merb_component/controller_ext.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, controller, &block) ⇒ Aggregator

Returns a new instance of Aggregator.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/merb_component/controller_ext.rb', line 103

def initialize(context, controller, &block)
  @context = context
  @controller = controller
  @agg_name = @context.controller_name.singular.intern
  model_class = Object.full_const_get(controller.name.singular)
  @object = @context.instance_variable_get("@#{@agg_name}")
  @scope = {}

  if @object
    relationship = model_class.relationships[@agg_name]
    key_names = relationship.child_key.map{|i| i.name}
    @scope = Hash[key_names.zip(@object.key)] if @object
  end

  @result = begin
    Thread.critical = true
    aggregators = Thread::current[:aggregators] ||= {}
    (aggregators[controller] ||= []).push(self)
    if model_class.respond_to?(:with_scope)
      model_class.send(:with_scope, @scope, &block)
    else
      block.call
    end
  ensure
    aggregators[controller].pop
    Thread.critical = false
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



101
102
103
# File 'lib/merb_component/controller_ext.rb', line 101

def context
  @context
end

#controllerObject (readonly)

Returns the value of attribute controller.



101
102
103
# File 'lib/merb_component/controller_ext.rb', line 101

def controller
  @controller
end

#objectObject (readonly)

Returns the value of attribute object.



101
102
103
# File 'lib/merb_component/controller_ext.rb', line 101

def object
  @object
end

#resultObject (readonly)

Returns the value of attribute result.



101
102
103
# File 'lib/merb_component/controller_ext.rb', line 101

def result
  @result
end

Instance Method Details

#keyObject



132
133
134
# File 'lib/merb_component/controller_ext.rb', line 132

def key
  @object || @agg_name
end