Class: Travis::Client::Entity
- Inherits:
-
Object
- Object
- Travis::Client::Entity
- Defined in:
- lib/travis/client/entity.rb
Direct Known Subclasses
Constant Summary collapse
- MAP =
{}
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#curry ⇒ Object
Returns the value of attribute curry.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Class Method Summary collapse
- .attributes(*list) ⇒ Object
- .inspect_info(name) ⇒ Object
- .many(key = nil) ⇒ Object
- .one(key = nil) ⇒ Object
- .subclass_for(key) ⇒ Object
- .subclasses ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #attribute_names ⇒ Object
- #complete? ⇒ Boolean
- #include?(key) ⇒ Boolean
-
#initialize(session, id) ⇒ Entity
constructor
A new instance of Entity.
- #inspect ⇒ Object
- #load ⇒ Object
- #missing?(key) ⇒ Boolean
- #reload ⇒ Object
- #update_attributes(data) ⇒ Object
Constructor Details
#initialize(session, id) ⇒ Entity
Returns a new instance of Entity.
47 48 49 50 51 |
# File 'lib/travis/client/entity.rb', line 47 def initialize(session, id) @attributes = {} @session = session @id = Integer(id) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
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 (readonly)
Returns the value of attribute id.
7 8 9 |
# File 'lib/travis/client/entity.rb', line 7 def id @id end |
#session ⇒ Object (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
.attributes(*list) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/travis/client/entity.rb', line 30 def self.attributes(*list) @attributes ||= [] list.each do |name| name = name.to_s @attributes << name define_method(name) { load_attribute(name) } define_method("#{name}=") { |value| set_attribute(name, value) } define_method("#{name}?") { !!send(name) } end @attributes end |
.inspect_info(name) ⇒ Object
42 43 44 45 |
# File 'lib/travis/client/entity.rb', line 42 def self.inspect_info(name) alias_method(:inspect_info, name) private(:inspect_info) end |
.many(key = nil) ⇒ Object
25 26 27 28 |
# File 'lib/travis/client/entity.rb', line 25 def self.many(key = nil) MAP[key.to_s] = self if key @many ||= key.to_s end |
.one(key = nil) ⇒ Object
20 21 22 23 |
# File 'lib/travis/client/entity.rb', line 20 def self.one(key = nil) MAP[key.to_s] = self if key @one ||= key.to_s end |
.subclass_for(key) ⇒ Object
16 17 18 |
# File 'lib/travis/client/entity.rb', line 16 def self.subclass_for(key) MAP.fetch(key) end |
.subclasses ⇒ Object
12 13 14 |
# File 'lib/travis/client/entity.rb', line 12 def self.subclasses MAP.values.uniq end |
Instance Method Details
#[](key) ⇒ Object
63 64 65 |
# File 'lib/travis/client/entity.rb', line 63 def [](key) send(key) if include? key end |
#[]=(key, value) ⇒ Object
67 68 69 |
# File 'lib/travis/client/entity.rb', line 67 def []=(key, value) send("#{key}=", value) if include? key end |
#attribute_names ⇒ Object
59 60 61 |
# File 'lib/travis/client/entity.rb', line 59 def attribute_names self.class.attributes end |
#complete? ⇒ Boolean
88 89 90 |
# File 'lib/travis/client/entity.rb', line 88 def complete? attribute_names.all? { |key| attributes.include? key } end |
#include?(key) ⇒ Boolean
71 72 73 |
# File 'lib/travis/client/entity.rb', line 71 def include?(key) attributes.include? key or attribute_names.include? key.to_s end |
#inspect ⇒ Object
92 93 94 95 96 |
# File 'lib/travis/client/entity.rb', line 92 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
79 80 81 |
# File 'lib/travis/client/entity.rb', line 79 def load reload unless complete? end |
#missing?(key) ⇒ Boolean
83 84 85 86 |
# File 'lib/travis/client/entity.rb', line 83 def missing?(key) return false unless include? key !attributes.include?(key.to_s) end |
#reload ⇒ Object
75 76 77 |
# File 'lib/travis/client/entity.rb', line 75 def reload session.reload(self) end |
#update_attributes(data) ⇒ Object
53 54 55 56 57 |
# File 'lib/travis/client/entity.rb', line 53 def update_attributes(data) data.each_pair do |key, value| self[key] = value end end |