Class: Travis::Client::EnvVar::List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, &block) ⇒ List

Returns a new instance of List.



10
11
12
13
14
# File 'lib/travis/client/env_var.rb', line 10

def initialize(repository, &block)
  @repository = repository
  @generator  = block || ::Proc.new { session.get(EnvVar.path(repository))['env_vars'] }
  super(nil)
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



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

def repository
  @repository
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  return super if key.is_a? Integer
  detect { |e| e.name == key.to_s }
end

#[]=(key, value) ⇒ Object



59
60
61
62
# File 'lib/travis/client/env_var.rb', line 59

def []=(key, value)
  return super if key.is_a? Integer
  upsert(key.to_s, value)
end

#__getobj__Object Also known as: list



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

def __getobj__
  super || (self.list = @generator.call)
end

#add(name, value, options = {}) ⇒ Object



37
38
39
40
41
# File 'lib/travis/client/env_var.rb', line 37

def add(name, value, options = {})
  body       = JSON.dump(:env_var => options.merge(:name => name, :value => value))
  result     = session.post(EnvVar.path(self), body)
  self.list += [result['env_var']]
end

#list=(list) ⇒ Object



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

def list=(list)
  __setobj__ list.dup.freeze
end

#reloadObject



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

def reload
  __setobj__ nil
  self
end

#repository_idObject



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

def repository_id
  repository.id
end

#sessionObject



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

def session
  repository.session
end

#upsert(name, value, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/travis/client/env_var.rb', line 43

def upsert(name, value, options = {})
  entries = select { |e| e.name == name }
  if entries.any?
    entries.first.update(options.merge(:value => value))
    entries[1..-1].each { |e| e.delete }
  else
    add(name, value, options)
  end
  reload
end