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.



12
13
14
15
16
# File 'lib/travis/client/env_var.rb', line 12

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.



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

def repository
  @repository
end

Instance Method Details

#[](key) ⇒ Object



56
57
58
59
60
# File 'lib/travis/client/env_var.rb', line 56

def [](key)
  return super if key.is_a? Integer

  detect { |e| e.name == key.to_s }
end

#[]=(key, value) ⇒ Object



62
63
64
65
66
# File 'lib/travis/client/env_var.rb', line 62

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

  upsert(key.to_s, value)
end

#__getobj__Object Also known as: list



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

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

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



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

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

#list=(list) ⇒ Object



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

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

#reloadObject



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

def reload
  __setobj__ nil
  self
end

#repository_idObject



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

def repository_id
  repository.id
end

#sessionObject



31
32
33
# File 'lib/travis/client/env_var.rb', line 31

def session
  repository.session
end

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



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

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