class OneEye::Base

Public Class Methods

api() click to toggle source
# File lib/models/base.rb, line 9
def self.api
  OneEye::API.new
end
create(attributes={}) click to toggle source
# File lib/models/base.rb, line 49
def self.create(attributes={})
  new(api.post(resources_url, attributes))
end
find(attributes={}) click to toggle source
# File lib/models/base.rb, line 39
def self.find(attributes={})
  new(api.get(resources_url, attributes))
end
new(attributes = {}) click to toggle source
# File lib/models/base.rb, line 13
def initialize(attributes = {})
  self.attributes = attributes
  defaults
end
where(attributes={}) click to toggle source
# File lib/models/base.rb, line 43
def self.where(attributes={})
  api.get(resources_url, attributes).map do |model|
    new model
  end
end

Private Class Methods

method_missing(name, *args, &block) click to toggle source
# File lib/models/base.rb, line 72
def self.method_missing(name, *args, &block)
  missing_finder name, *args, &block
end
missing_finder(name, *args, &block) click to toggle source
# File lib/models/base.rb, line 92
def self.missing_finder(name, *args, &block)
  @models = api.get("#{self.name.gsub(/OneEye\:\:/) {}.underscore}/#{name.to_s.underscore}", *args)
  return @models.map { |model| new model } if @models.map
  new @models
end
resources_url() click to toggle source
# File lib/models/base.rb, line 68
def self.resources_url
  self.name.sub(/\w+\:\:/) {}.underscore.downcase.pluralize
end

Public Instance Methods

attributes() click to toggle source
# File lib/models/base.rb, line 31
def attributes
  instance_variables.inject Hash.new do |attributes, key|
    key = key.to_s.gsub(/\@/) { "" }
    attributes[key] = send key
    attributes
  end
end
attributes=(attrs) click to toggle source
# File lib/models/base.rb, line 26
def attributes=(attrs)
  make_array_convertable attrs if attrs.is_a? Array
  attrs.to_hash.each { |k, v| send "#{k.to_s.gsub(/\-/) {}}=", v }
end
defaults() click to toggle source
# File lib/models/base.rb, line 18
    def defaults
      <<-eos
        Please define behavior in subclasses.
        See: Template method pattern.
        http://en.wikipedia.org/wiki/Template_method_pattern
      eos
    end
delete() click to toggle source
# File lib/models/base.rb, line 63
def delete
  destroy
end
destroy() click to toggle source
# File lib/models/base.rb, line 59
def destroy
  api.delete resources_url, attributes
end
update(attributes={}) click to toggle source
# File lib/models/base.rb, line 53
def update(attributes={})
  self.attributes = attributes
  self.attributes = api.put(resources_url, self.attributes)
  self
end

Private Instance Methods

api() click to toggle source
# File lib/models/base.rb, line 84
def api
  self.class.api
end
make_array_convertable(attrs) click to toggle source
# File lib/models/base.rb, line 98
def make_array_convertable(attrs)
  def attrs.to_hash
    Hash[self.each_slice(2).to_a]
  end
end
method_missing(name, *args, &block) click to toggle source
# File lib/models/base.rb, line 76
def method_missing(name, *args, &block)
  if name[-1] == "="
    return instance_variable_set "@#{name[0..-2]}", args[0]
  else
    instance_variable_get "@#{name}"
  end
end
resources_url() click to toggle source
# File lib/models/base.rb, line 88
def resources_url
  self.class.resources_url
end