Class: EndecaOnDemand::Collection

Inherits:
Proxy
  • Object
show all
Defined in:
lib/endeca_on_demand/collection.rb

Instance Attribute Summary collapse

Attributes inherited from Proxy

#xml

Instance Method Summary collapse

Constructor Details

#initialize(klass, target, mapping = nil) ⇒ Collection



6
7
8
9
10
# File 'lib/endeca_on_demand/collection.rb', line 6

def initialize(klass, target, mapping = nil)
  @klass, @target = klass, target
  @target = target.map { |object| @klass.new(mapping, object) } if mapping.present?
  extend klass.collection_class if klass.respond_to?(:collection_class)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



38
39
40
# File 'lib/endeca_on_demand/collection.rb', line 38

def method_missing(name, *args, &block)
  target.send(name, *args, &block)
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass



4
5
6
# File 'lib/endeca_on_demand/collection.rb', line 4

def klass
  @klass
end

#targetObject (readonly)

Returns the value of attribute target



4
5
6
# File 'lib/endeca_on_demand/collection.rb', line 4

def target
  @target
end

Instance Method Details

#classObject

override proxy ##



14
15
16
# File 'lib/endeca_on_demand/collection.rb', line 14

def class
  EndecaOnDemand::Collection
end

#inspectObject



18
19
20
# File 'lib/endeca_on_demand/collection.rb', line 18

def inspect
  target.to_a.inspect
end

#where(conditions = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/endeca_on_demand/collection.rb', line 24

def where(conditions = {})
  target.select do |object|
    conditions.all? do |key,value|
      value.is_a?(Regexp) ? object.send(key) =~ value : object.send(key) == value
    end
  end
end