class Horza::Entities::Collection

Public Class Methods

new(collection) click to toggle source
# File lib/horza/entities/collection.rb, line 6
def initialize(collection)
  @collection = collection
end

Public Instance Methods

[](index) click to toggle source
# File lib/horza/entities/collection.rb, line 10
def [](index)
  singular_entity(@collection[index])
end

Private Instance Methods

enum_method(method) { |singular_entity(result)| ... } click to toggle source
# File lib/horza/entities/collection.rb, line 27
def enum_method(method, &block)
  @collection.send(method) do |result|
    yield singular_entity(result)
  end
end
method_missing(method, &block) click to toggle source
# File lib/horza/entities/collection.rb, line 16
def method_missing(method, &block)
  if [:length, :size, :empty?, :present?].include? method
    @collection.send(method)
  elsif [:first, :last, :pop].include? method
    result = @collection.send(method)
    singular_entity(result) unless result.nil?
  elsif [:each, :map, :collect]
    enum_method(method, &block)
  end
end
singular_entity(record) click to toggle source

Collection classes have the form Horza::Entities::Users Single output requires the form Horza::Entities::User

# File lib/horza/entities/collection.rb, line 35
def singular_entity(record)
  attributes = record.respond_to?(:to_hash) ? record.to_hash : Horza.adapter.new(record).to_hash
  ::Horza::Entities::single_entity_for(record.class.name.split('::').last.symbolize, attributes)
end