class Namely::Collection

Attributes

resource_gateway[R]

Public Class Methods

new(resource_gateway) click to toggle source
# File lib/namely/collection.rb, line 5
def initialize(resource_gateway)
  @resource_gateway = resource_gateway
end

Public Instance Methods

all() click to toggle source

Return every instance of this model.

A model might have quite a few instances. If this is the case, the query may take some time (several seconds) and the resulting array may be very large.

@return [Array<Model>]

# File lib/namely/collection.rb, line 16
def all
  resource_gateway.json_index.map { |model| build(model) }
end
build(attributes) click to toggle source

Instantiate (but don't save) a new Model with the given attributes.

@param [Hash] attributes the attributes of the model being built.

@return [Model]

# File lib/namely/collection.rb, line 25
def build(attributes)
  Model.new(resource_gateway, attributes)
end
create!(attributes) click to toggle source

Create a new Model on the server with the given attributes.

@param [Hash] attributes the attributes of the model being created.

@example

profiles_collection.create!(
  first_name: "Beardsly",
  last_name: "McDog",
  email: "beardsly@namely.com"
)

@return [Model] the created model.

# File lib/namely/collection.rb, line 41
def create!(attributes)
  build(attributes).save!
end
endpoint() click to toggle source
# File lib/namely/collection.rb, line 45
def endpoint
  resource_gateway.endpoint
end
exists?(id) click to toggle source

Returns true if a Model with this ID exists, false otherwise.

@param [#to_s] id

@return [Boolean]

# File lib/namely/collection.rb, line 54
def exists?(id)
  resource_gateway.show_head(id)
  true
rescue RestClient::ResourceNotFound
  false
end
find(id) click to toggle source

Fetch a model from the server by its ID.

@param [#to_s] id

@raise [NoSuchModelError] if the model wasn't found.

@return [Model]

# File lib/namely/collection.rb, line 68
def find(id)
  build(resource_gateway.json_show(id))
rescue RestClient::ResourceNotFound
  raise NoSuchModelError, "Can't find any #{endpoint} with id \"#{id}\""
end