Class: Travis::Client::Entity

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

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.



114
115
116
117
118
119
120
# File 'lib/travis/client/entity.rb', line 114

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#curryObject

Returns the value of attribute curry.



10
11
12
# File 'lib/travis/client/entity.rb', line 10

def curry
  @curry
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

.aka(*names) ⇒ Object



26
27
28
# File 'lib/travis/client/entity.rb', line 26

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

.attributes(*list) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/travis/client/entity.rb', line 48

def self.attributes(*list)
  @attributes ||= []

  list.each do |name|
    name = name.to_s
    raise "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_pathObject



44
45
46
# File 'lib/travis/client/entity.rb', line 44

def self.base_path
  many
end

.cast_id(id) ⇒ Object



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

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

.has(*list) ⇒ Object



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

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

.has_singleton(*list) ⇒ Object



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

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) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.id?(object)
  object.is_a? Integer
end

.id_field(key = nil) ⇒ Object



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

def self.id_field(key = nil)
  @id_field = key.to_s if key
  @id_field || superclass.id_field
end

.inspect_info(name) ⇒ Object



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

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

.many(key = nil) ⇒ Object



39
40
41
42
# File 'lib/travis/client/entity.rb', line 39

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

.one(key = nil) ⇒ Object



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

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

.preloadableObject



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

def self.preloadable
  def self.preloadable?
    true
  end
end

.preloadable?Boolean

Returns:

  • (Boolean)


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

def self.preloadable?
  true
end

.relationsObject



14
15
16
# File 'lib/travis/client/entity.rb', line 14

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

.subclass_for(key) ⇒ Object



22
23
24
# File 'lib/travis/client/entity.rb', line 22

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

.subclassesObject



18
19
20
# File 'lib/travis/client/entity.rb', line 18

def self.subclasses
  MAP.values.uniq
end

.time(*list) ⇒ Object



64
65
66
67
68
# File 'lib/travis/client/entity.rb', line 64

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

.weak?Boolean

Returns:

  • (Boolean)


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

def self.weak?
  false
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  key = key.to_s
  send(key) if include? key
end

#[]=(key, value) ⇒ Object



141
142
143
144
# File 'lib/travis/client/entity.rb', line 141

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

#attribute_namesObject



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

def attribute_names
  self.class.attributes
end

#cancelable?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/travis/client/entity.rb', line 183

def cancelable?
  false
end

#complete?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/travis/client/entity.rb', line 165

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

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/travis/client/entity.rb', line 146

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

#inspectObject



169
170
171
172
173
# File 'lib/travis/client/entity.rb', line 169

def inspect
  klass = self.class
  klass = curry if curry&.name && curry.to_s.size < klass.to_s.size
  "#<#{klass}: #{inspect_info}>"
end

#loadObject



155
156
157
# File 'lib/travis/client/entity.rb', line 155

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

#missing?(key) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/travis/client/entity.rb', line 159

def missing?(key)
  return false unless include? key

  !attributes.include?(key.to_s)
end

#relationsObject



175
176
177
# File 'lib/travis/client/entity.rb', line 175

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

#reloadObject



150
151
152
153
# File 'lib/travis/client/entity.rb', line 150

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

#restartable?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/travis/client/entity.rb', line 179

def restartable?
  false
end

#to_hObject



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

def to_h
  Hash[attribute_names.map { |n| [n, self[n]] }]
end

#update_attributes(data) ⇒ Object



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

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