class GTFS::ORM::Resource::Scope

Constants

DEFAULT_CONDITIONS
DEFAULT_LIMIT

CONSTANTS

DEFAULT_PAGE

Attributes

collection[R]
conditions[R]

Public Class Methods

new(collection) click to toggle source

INITIALIZER

# File lib/gtfs/orm/resource.rb, line 70
def initialize(collection)
  @collection = collection
  @limit = DEFAULT_LIMIT
  @page  = DEFAULT_PAGE
  @conditions = DEFAULT_CONDITIONS
end

Public Instance Methods

all() click to toggle source
# File lib/gtfs/orm/resource.rb, line 114
def all
  scoped_collection
end
first() click to toggle source
# File lib/gtfs/orm/resource.rb, line 106
def first
  all.first
end
last() click to toggle source
# File lib/gtfs/orm/resource.rb, line 110
def last
  all.last
end
limit(limit = nil) click to toggle source

INSTANCE METHODS

# File lib/gtfs/orm/resource.rb, line 79
def limit(limit = nil)
  if limit
    @limit = limit
    return self
  else
    @limit
  end
end
page(page=nil) click to toggle source
# File lib/gtfs/orm/resource.rb, line 97
def page(page=nil)
  if page
    @page = page
    return self
  else
    @page
  end
end
where(conditions = nil) click to toggle source
# File lib/gtfs/orm/resource.rb, line 88
def where(conditions = nil)
  if conditions
    @conditions = conditions
    return self
  else
    @conditions
  end
end

Private Instance Methods

scoped_collection() click to toggle source
# File lib/gtfs/orm/resource.rb, line 122
def scoped_collection
  arr = collection.select do |item|
    match = true
    if conditions
      conditions.each_pair do |k,v|
        if item.respond_to?(k)
          match = false unless item.send(k) == v
        end
      end
    end
    match
  end

  arr[ ((page - 1) * limit)...(page * limit)] || []
end