class Travis::Client::Entity

Constants

MAP

Attributes

attributes[R]
curry[RW]
id[R]
session[R]

Public Class Methods

aka(*names) click to toggle source
# File lib/travis/client/entity.rb, line 24
def self.aka(*names)
  names.each { |n| MAP[n.to_s] = self }
end
attributes(*list) click to toggle source
# File lib/travis/client/entity.rb, line 46
def self.attributes(*list)
  @attributes ||= []

  list.each do |name|
    name = name.to_s
    fail "can't call an attribute id" if name == "id"

    @attributes << name
    define_method(name) { load_attribute(name) }
    define_method("#{name}=") { |value| set_attribute(name, value) }
    define_method("#{name}?") { !!send(name) }
  end

  @attributes
end
base_path() click to toggle source
# File lib/travis/client/entity.rb, line 42
def self.base_path
  many
end
cast_id(id) click to toggle source
# File lib/travis/client/entity.rb, line 87
def self.cast_id(id)
  Integer(id)
end
has(*list) click to toggle source
# File lib/travis/client/entity.rb, line 68
def self.has(*list)
  list.each do |name|
    relations << name
    define_method(name) { relation(name.to_s) }
  end
end
has_singleton(*list) click to toggle source
# File lib/travis/client/entity.rb, line 75
def self.has_singleton(*list)
  list.each do |name|
    alias_method :"#{name}_id", :id unless method_defined? :"#{name}_id"
    has(name)
  end
end
id?(object) click to toggle source
# File lib/travis/client/entity.rb, line 91
def self.id?(object)
  object.is_a? Integer
end
id_field(key = nil) click to toggle source
# File lib/travis/client/entity.rb, line 95
def self.id_field(key = nil)
  @id_field = key.to_s if key
  @id_field || superclass.id_field
end
inspect_info(name) click to toggle source
# File lib/travis/client/entity.rb, line 82
def self.inspect_info(name)
  alias_method(:inspect_info, name)
  private(:inspect_info)
end
many(key = nil) click to toggle source
# File lib/travis/client/entity.rb, line 37
def self.many(key = nil)
  MAP[key.to_s] = self if key
  @many ||= key.to_s
end
new(session, id) click to toggle source
# File lib/travis/client/entity.rb, line 112
def initialize(session, id)
  raise Travis::Client::Error, '%p is not a valid id' % id unless self.class.id? id
  @attributes = {}
  @session    = session
  @id         = self.class.cast_id(id) if id
end
one(key = nil) click to toggle source
# File lib/travis/client/entity.rb, line 32
def self.one(key = nil)
  MAP[key.to_s] = self if key
  @one ||= key.to_s
end
preloadable() click to toggle source
# File lib/travis/client/entity.rb, line 100
def self.preloadable
  def self.preloadable?
    true
  end
end
preloadable?() click to toggle source
# File lib/travis/client/entity.rb, line 101
def self.preloadable?
  true
end
relations() click to toggle source
# File lib/travis/client/entity.rb, line 12
def self.relations
  @relations ||= []
end
subclass_for(key) click to toggle source
# File lib/travis/client/entity.rb, line 20
def self.subclass_for(key)
  MAP.fetch(key.to_s)
end
subclasses() click to toggle source
# File lib/travis/client/entity.rb, line 16
def self.subclasses
  MAP.values.uniq
end
time(*list) click to toggle source
# File lib/travis/client/entity.rb, line 62
def self.time(*list)
  list.each do |name|
    define_method("#{name}=") { |value| set_attribute(name, time(value)) }
  end
end
weak?() click to toggle source
# File lib/travis/client/entity.rb, line 28
def self.weak?
  false
end

Public Instance Methods

[](key) click to toggle source
# File lib/travis/client/entity.rb, line 133
def [](key)
  key = key.to_s
  send(key) if include? key
end
[]=(key, value) click to toggle source
# File lib/travis/client/entity.rb, line 138
def []=(key, value)
  key = key.to_s
  send("#{key}=", value) if include? key
end
attribute_names() click to toggle source
# File lib/travis/client/entity.rb, line 125
def attribute_names
  self.class.attributes
end
cancelable?() click to toggle source
# File lib/travis/client/entity.rb, line 179
def cancelable?
  false
end
complete?() click to toggle source
# File lib/travis/client/entity.rb, line 161
def complete?
  attribute_names.all? { |key| attributes.include? key }
end
include?(key) click to toggle source
# File lib/travis/client/entity.rb, line 143
def include?(key)
  attributes.include? key or attribute_names.include? key.to_s
end
inspect() click to toggle source
# File lib/travis/client/entity.rb, line 165
def inspect
  klass = self.class
  klass = curry if curry and curry.name and curry.to_s.size < klass.to_s.size
  "#<#{klass}: #{inspect_info}>"
end
load() click to toggle source
# File lib/travis/client/entity.rb, line 152
def load
  session.reload(self) unless complete?
end
missing?(key) click to toggle source
# File lib/travis/client/entity.rb, line 156
def missing?(key)
  return false unless include? key
  !attributes.include?(key.to_s)
end
relations() click to toggle source
# File lib/travis/client/entity.rb, line 171
def relations
  self.class.relations.map { |r| public_send(r) }.flatten(1)
end
reload() click to toggle source
# File lib/travis/client/entity.rb, line 147
def reload
  relations.each { |e| session.reset(e) }
  session.reset(self)
end
restartable?() click to toggle source
# File lib/travis/client/entity.rb, line 175
def restartable?
  false
end
to_h() click to toggle source
# File lib/travis/client/entity.rb, line 129
def to_h
  Hash[attribute_names.map { |n| [n, self[n]] }]
end
update_attributes(data) click to toggle source
# File lib/travis/client/entity.rb, line 119
def update_attributes(data)
  data.each_pair do |key, value|
    self[key] = value
  end
end

Private Instance Methods

inspect_info() click to toggle source
# File lib/travis/client/entity.rb, line 199
def inspect_info
  id
end
load_attribute(name) click to toggle source
# File lib/travis/client/entity.rb, line 207
def load_attribute(name)
  session.reload(self) if missing? name
  attributes[name.to_s]
end
relation(name) click to toggle source
# File lib/travis/client/entity.rb, line 185
def relation(name)
  name   = name.to_s
  entity = Entity.subclass_for(name)

  if entity.many == name
    Array(send("#{entity.one}_ids")).map do |id|
      session.find_one(entity, id)
    end
  else
    id = send("#{name}_id")
    session.find_one(entity, id) unless id.nil?
  end
end
set_attribute(name, value) click to toggle source
# File lib/travis/client/entity.rb, line 203
def set_attribute(name, value)
  attributes[name.to_s] = value
end
time(value) click to toggle source

shamelessly stolen from sinatra

# File lib/travis/client/entity.rb, line 213
def time(value)
  if value.respond_to? :to_time
    value.to_time
  elsif value.is_a? Time
    value
  elsif value.respond_to? :new_offset
    d = value.new_offset 0
    t = Time.utc d.year, d.mon, d.mday, d.hour, d.min, d.sec + d.sec_fraction
    t.getlocal
  elsif value.respond_to? :mday
    Time.local(value.year, value.mon, value.mday)
  elsif value.is_a? Numeric
    Time.at value
  elsif value.nil? or value.empty?
    nil
  else
    Time.parse value.to_s
  end
rescue ArgumentError => boom
  raise boom
rescue Exception
  raise ArgumentError, "unable to convert #{value.inspect} to a Time object"
end