Class: Travis::Client::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/client/entity.rb

Direct Known Subclasses

Account, Artifact, Broadcast, Build, Commit, Job, Repository, User, Worker

Constant Summary collapse

MAP =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, id) ⇒ Entity

Returns a new instance of Entity.



76
77
78
79
80
# File 'lib/travis/client/entity.rb', line 76

def initialize(session, id)
  @attributes = {}
  @session    = session
  @id         = self.class.cast_id(id)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/travis/client/entity.rb', line 7

def attributes
  @attributes
end

#curryObject

Returns the value of attribute curry.



8
9
10
# File 'lib/travis/client/entity.rb', line 8

def curry
  @curry
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/travis/client/entity.rb', line 7

def id
  @id
end

#sessionObject (readonly)

Returns the value of attribute session.



7
8
9
# File 'lib/travis/client/entity.rb', line 7

def session
  @session
end

Class Method Details

.aka(*names) ⇒ Object



24
25
26
# File 'lib/travis/client/entity.rb', line 24

def self.aka(*names)
  names.each { |n| MAP[n.to_s] = self }
end

.attributes(*list) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/travis/client/entity.rb', line 38

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

.cast_id(id) ⇒ Object



72
73
74
# File 'lib/travis/client/entity.rb', line 72

def self.cast_id(id)
  Integer(id)
end

.has(*list) ⇒ Object



60
61
62
63
64
65
# File 'lib/travis/client/entity.rb', line 60

def self.has(*list)
  list.each do |name|
    relations << name
    define_method(name) { relation(name.to_s) }
  end
end

.inspect_info(name) ⇒ Object



67
68
69
70
# File 'lib/travis/client/entity.rb', line 67

def self.inspect_info(name)
  alias_method(:inspect_info, name)
  private(:inspect_info)
end

.many(key = nil) ⇒ Object



33
34
35
36
# File 'lib/travis/client/entity.rb', line 33

def self.many(key = nil)
  MAP[key.to_s] = self if key
  @many ||= key.to_s
end

.one(key = nil) ⇒ Object



28
29
30
31
# File 'lib/travis/client/entity.rb', line 28

def self.one(key = nil)
  MAP[key.to_s] = self if key
  @one ||= key.to_s
end

.relationsObject



12
13
14
# File 'lib/travis/client/entity.rb', line 12

def self.relations
  @relations ||= []
end

.subclass_for(key) ⇒ Object



20
21
22
# File 'lib/travis/client/entity.rb', line 20

def self.subclass_for(key)
  MAP.fetch(key)
end

.subclassesObject



16
17
18
# File 'lib/travis/client/entity.rb', line 16

def self.subclasses
  MAP.values.uniq
end

.time(*list) ⇒ Object



54
55
56
57
58
# File 'lib/travis/client/entity.rb', line 54

def self.time(*list)
  list.each do |name|
    define_method("#{name}=") { |value| set_attribute(name, time(value)) }
  end
end

Instance Method Details

#[](key) ⇒ Object



92
93
94
# File 'lib/travis/client/entity.rb', line 92

def [](key)
  send(key) if include? key
end

#[]=(key, value) ⇒ Object



96
97
98
# File 'lib/travis/client/entity.rb', line 96

def []=(key, value)
  send("#{key}=", value) if include? key
end

#attribute_namesObject



88
89
90
# File 'lib/travis/client/entity.rb', line 88

def attribute_names
  self.class.attributes
end

#cancelable?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/travis/client/entity.rb', line 136

def cancelable?
  false
end

#complete?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/travis/client/entity.rb', line 118

def complete?
  attribute_names.all? { |key| attributes.include? key }
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/travis/client/entity.rb', line 100

def include?(key)
  attributes.include? key or attribute_names.include? key.to_s
end

#inspectObject



122
123
124
125
126
# File 'lib/travis/client/entity.rb', line 122

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

#loadObject



109
110
111
# File 'lib/travis/client/entity.rb', line 109

def load
  session.reload(self) unless complete?
end

#missing?(key) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/travis/client/entity.rb', line 113

def missing?(key)
  return false unless include? key
  !attributes.include?(key.to_s)
end

#relationsObject



128
129
130
# File 'lib/travis/client/entity.rb', line 128

def relations
  self.class.relations.map { |r| public_send(r) }.flatten(1)
end

#reloadObject



104
105
106
107
# File 'lib/travis/client/entity.rb', line 104

def reload
  relations.each { |e| session.reset(e) }
  session.reset(self)
end

#restartable?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/travis/client/entity.rb', line 132

def restartable?
  false
end

#update_attributes(data) ⇒ Object



82
83
84
85
86
# File 'lib/travis/client/entity.rb', line 82

def update_attributes(data)
  data.each_pair do |key, value|
    self[key] = value
  end
end