Class: Capistrano::Redmine::Deployment::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/redmine/deployment/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, file: nil) ⇒ Config

Returns a new instance of Config.



44
45
46
47
48
# File 'lib/capistrano/redmine/deployment/config.rb', line 44

def initialize(config = {}, file: nil)
  @config = config
  @file = file
  load
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



83
84
85
# File 'lib/capistrano/redmine/deployment/config.rb', line 83

def method_missing(name)
  get(name)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



10
11
12
# File 'lib/capistrano/redmine/deployment/config.rb', line 10

def file
  @file
end

Class Method Details

.config_from_capistrano(capistrano) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/capistrano/redmine/deployment/config.rb', line 28

def config_from_capistrano(capistrano)
  config = {
    host: capistrano.fetch(:redmine_host),
    project: capistrano.fetch(:redmine_project) || capistrano.fetch(:redmine_project_id),
    repository: capistrano.fetch(:redmine_repository),
    host_verification: capistrano.fetch(:redmine_host_verification)
  }

  new(config)
end

.config_from_file(file) ⇒ Object



39
40
41
# File 'lib/capistrano/redmine/deployment/config.rb', line 39

def config_from_file(file)
  new(file: file)
end

.resolve(capistrano: nil, file: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/capistrano/redmine/deployment/config.rb', line 13

def resolve(capistrano: nil, file: nil)
  # build new empty config
  config = new

  # try to resolve from capistrano
  config.assign!(config_from_capistrano(capistrano)) if capistrano

  # try to resolve from current PWD
  config.assign!(config_from_file(File.join(Dir.pwd, '.redmine')))
  config.assign!(config_from_file(File.join(ENV['HOME'], '.redmine')))
  config.assign!(config_from_file(file)) if file

  config
end

Instance Method Details

#assign!(other) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/capistrano/redmine/deployment/config.rb', line 66

def assign!(other)
  return unless other

  other.to_h.each do |key, value|
    next if value == nil || value == ''

    set(key, value)
  end
end

#get(key) ⇒ Object



58
59
60
# File 'lib/capistrano/redmine/deployment/config.rb', line 58

def get(key)
  @config[key.to_sym]
end

#loadObject



93
94
95
96
97
# File 'lib/capistrano/redmine/deployment/config.rb', line 93

def load
  if @file && File.exist?(@file)
    @config = YAML.load_file(@file)
  end
end

#saveObject



87
88
89
90
91
# File 'lib/capistrano/redmine/deployment/config.rb', line 87

def save
  return false unless @file

  File.open(@file, 'w') { |f| f.write(YAML.dump(@config)) } rescue false
end

#set(key, value) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/capistrano/redmine/deployment/config.rb', line 50

def set(key, value)
  if value != nil && value != ''
    @config[key.to_sym] = value
  else
    @config.delete(key.to_sym)
  end
end

#to_hObject



62
63
64
# File 'lib/capistrano/redmine/deployment/config.rb', line 62

def to_h
  @config
end

#valid?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
# File 'lib/capistrano/redmine/deployment/config.rb', line 76

def valid?
  i[host project repository api_key].all? { |key|
    val = get(key)
    val && val != ''
  }
end