Class: Travis::Client::Entity

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, href) ⇒ Entity

Returns a new instance of Entity.



36
37
38
39
40
# File 'lib/travis/client/entity.rb', line 36

def initialize(session, href)
  @session = session
  @href    = href
  @payload = {}
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

.add_action(resource_type, name, action, instance_only: false) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/travis/client/entity.rb', line 9

def self.add_action(resource_type, name, action, instance_only: false)
  if name == action.name and !respond_to?(name, true) and !instance_only
    define_singleton_method(name) { |params = {}| action.call(session, params) }
  end

  if action.instance_action?(resource_type) and not method_defined? name
    define_method(name) { |params = {}| action.call(session, params.merge(resource_type => self)) }
  end
end

.add_attribute(name) ⇒ Object



4
5
6
7
# File 'lib/travis/client/entity.rb', line 4

def self.add_attribute(name)
  define_method(name) { self[name] } unless method_defined? name
  define_method("#{name}?") { !!public_send(name) } unless method_defined? "#{name}?"
end


19
20
21
22
23
24
25
26
27
28
# File 'lib/travis/client/entity.rb', line 19

def self.add_related_action(own_type, other_type, name, action)
  if name == 'find' or name.start_with? 'for_'
    add_action(own_type, other_type, action, instance_only: true)
  end

  if name != "for_#{own_type}"
    add_action(own_type, "#{other_type}_#{name}", action, instance_only: true)
    add_action(own_type, "#{name}_#{other_type}", action, instance_only: true)
  end
end

.for_session(session) ⇒ Object



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

def self.for_session(session)
  Class.new(self) { define_singleton_method(:session) { session } }
end

Instance Method Details

#[](key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/travis/client/entity.rb', line 54

def [](key)
  key = key.to_s

  if @href and type = @payload['@type'] and not @payload.include? key
    href = @href.dup
    href.query_values = { 'include' => "#{type}.#{key}" }
    @session.request('GET', href)
    @payload.fetch(key) { @payload[key] = nil }
  end

  result = @payload[key]
  result = result.fetch if result.is_a? Link
  result
end

#inspectObject



42
43
44
# File 'lib/travis/client/entity.rb', line 42

def inspect
  @href ? "#<%p:%p>" % [self.class, @href.path] : "#<%p>" % [self.class]
end

#merge!(data) ⇒ Object



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

def merge!(data)
  @payload.merge! data
end

#permission?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def permission?(key)
  Hash(self['@permissions'])[key.to_s]
end

#to_entityObject



50
51
52
# File 'lib/travis/client/entity.rb', line 50

def to_entity
  self
end

#to_hObject



46
47
48
# File 'lib/travis/client/entity.rb', line 46

def to_h
  @payload.dup
end