Class: Travis::Client::Entity
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
          - Object
- Travis::Client::Entity
 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.
   
 
  
    | 
91
92
93
94
95
96 | # File 'lib/travis/client/entity.rb', line 91
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
    
      
      
      
  
  
    #attributes  ⇒ Object  
  
  
  
  
    
Returns the value of attribute attributes.
   
 
  
  
    | 
7
8
9 | # File 'lib/travis/client/entity.rb', line 7
def attributes
  @attributes
end | 
 
    
      
      
      
  
  
    #curry  ⇒ Object 
  
  
  
  
    
Returns the value of attribute curry.
   
 
  
  
    | 
8
9
10 | # File 'lib/travis/client/entity.rb', line 8
def curry
  @curry
end | 
 
    
      
      
      
  
  
    #id  ⇒ Object  
  
  
  
  
    
Returns the value of attribute id.
   
 
  
  
    | 
7
8
9 | # File 'lib/travis/client/entity.rb', line 7
def id
  @id
end | 
 
    
      
      
      
  
  
    #session  ⇒ Object  
  
  
  
  
    
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 
  
  
  
  
    | 
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 | # File 'lib/travis/client/entity.rb', line 42
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 
  
  
  
  
    | 
76
77
78 | # File 'lib/travis/client/entity.rb', line 76
def self.cast_id(id)
  Integer(id)
end | 
 
    
      
  
  
    .has(*list)  ⇒ Object 
  
  
  
  
    | 
64
65
66
67
68
69 | # File 'lib/travis/client/entity.rb', line 64
def self.has(*list)
  list.each do |name|
    relations << name
    define_method(name) { relation(name.to_s) }
  end
end | 
 
    
      
  
  
    .id?(object)  ⇒ Boolean 
  
  
  
  
    | 
80
81
82 | # File 'lib/travis/client/entity.rb', line 80
def self.id?(object)
  object.is_a? Integer
end | 
 
    
      
  
  
    .id_field(key = nil)  ⇒ Object 
  
  
  
  
    | 
84
85
86
87 | # File 'lib/travis/client/entity.rb', line 84
def self.id_field(key = nil)
  @id_field = key.to_s if key
  @id_field || superclass.id_field
end | 
 
    
      
  
  
    .inspect_info(name)  ⇒ Object 
  
  
  
  
    | 
71
72
73
74 | # File 'lib/travis/client/entity.rb', line 71
def self.inspect_info(name)
  alias_method(:inspect_info, name)
  private(:inspect_info)
end | 
 
    
      
  
  
    .many(key = nil)  ⇒ Object 
  
  
  
  
    | 
37
38
39
40 | # 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 | 
 
    
      
  
  
    .one(key = nil)  ⇒ Object 
  
  
  
  
    | 
32
33
34
35 | # 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 | 
 
    
      
  
  
    .relations  ⇒ Object 
  
  
  
  
    | 
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.to_s)
end | 
 
    
      
  
  
    .subclasses  ⇒ Object 
  
  
  
  
    | 
16
17
18 | # File 'lib/travis/client/entity.rb', line 16
def self.subclasses
  MAP.values.uniq
end | 
 
    
      
  
  
    .time(*list)  ⇒ Object 
  
  
  
  
    | 
58
59
60
61
62 | # File 'lib/travis/client/entity.rb', line 58
def self.time(*list)
  list.each do |name|
    define_method("#{name}=") { |value| set_attribute(name, time(value)) }
  end
end | 
 
    
      
  
  
    .weak?  ⇒ Boolean 
  
  
  
  
    | 
28
29
30 | # File 'lib/travis/client/entity.rb', line 28
def self.weak?
  false
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #[](key)  ⇒ Object 
  
  
  
  
    | 
108
109
110 | # File 'lib/travis/client/entity.rb', line 108
def [](key)
  send(key) if include? key
end | 
 
    
      
  
  
    #[]=(key, value)  ⇒ Object 
  
  
  
  
    | 
112
113
114 | # File 'lib/travis/client/entity.rb', line 112
def []=(key, value)
  send("#{key}=", value) if include? key
end | 
 
    
      
  
  
    #attribute_names  ⇒ Object 
  
  
  
  
    | 
104
105
106 | # File 'lib/travis/client/entity.rb', line 104
def attribute_names
  self.class.attributes
end | 
 
    
      
  
  
    #cancelable?  ⇒ Boolean 
  
  
  
  
    | 
152
153
154 | # File 'lib/travis/client/entity.rb', line 152
def cancelable?
  false
end | 
 
    
      
  
  
    #complete?  ⇒ Boolean 
  
  
  
  
    | 
134
135
136 | # File 'lib/travis/client/entity.rb', line 134
def complete?
  attribute_names.all? { |key| attributes.include? key }
end | 
 
    
      
  
  
    #include?(key)  ⇒ Boolean 
  
  
  
  
    | 
116
117
118 | # File 'lib/travis/client/entity.rb', line 116
def include?(key)
  attributes.include? key or attribute_names.include? key.to_s
end | 
 
    
      
  
  
    #inspect  ⇒ Object 
  
  
  
  
    | 
138
139
140
141
142 | # File 'lib/travis/client/entity.rb', line 138
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  ⇒ Object 
  
  
  
  
    | 
125
126
127 | # File 'lib/travis/client/entity.rb', line 125
def load
  session.reload(self) unless complete?
end | 
 
    
      
  
  
    #missing?(key)  ⇒ Boolean 
  
  
  
  
    | 
129
130
131
132 | # File 'lib/travis/client/entity.rb', line 129
def missing?(key)
  return false unless include? key
  !attributes.include?(key.to_s)
end | 
 
    
      
  
  
    #relations  ⇒ Object 
  
  
  
  
    | 
144
145
146 | # File 'lib/travis/client/entity.rb', line 144
def relations
  self.class.relations.map { |r| public_send(r) }.flatten(1)
end | 
 
    
      
  
  
    #reload  ⇒ Object 
  
  
  
  
    | 
120
121
122
123 | # File 'lib/travis/client/entity.rb', line 120
def reload
  relations.each { |e| session.reset(e) }
  session.reset(self)
end | 
 
    
      
  
  
    #restartable?  ⇒ Boolean 
  
  
  
  
    | 
148
149
150 | # File 'lib/travis/client/entity.rb', line 148
def restartable?
  false
end | 
 
    
      
  
  
    #update_attributes(data)  ⇒ Object 
  
  
  
  
    | 
98
99
100
101
102 | # File 'lib/travis/client/entity.rb', line 98
def update_attributes(data)
  data.each_pair do |key, value|
    self[key] = value
  end
end |