Class: Capistrano::SharedConfig::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/shared_config/config_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, capistrano_binding) ⇒ ConfigFile

Returns a new instance of ConfigFile.



7
8
9
10
# File 'lib/capistrano/shared_config/config_file.rb', line 7

def initialize name, capistrano_binding
  @name = self.class.name(name)
  @capistrano_binding = capistrano_binding
end

Instance Attribute Details

#capistrano_bindingObject (readonly)

Returns the value of attribute capistrano_binding.



12
13
14
# File 'lib/capistrano/shared_config/config_file.rb', line 12

def capistrano_binding
  @capistrano_binding
end

#errorObject (readonly)

Returns the value of attribute error.



12
13
14
# File 'lib/capistrano/shared_config/config_file.rb', line 12

def error
  @error
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/capistrano/shared_config/config_file.rb', line 12

def name
  @name
end

Class Method Details

.name(name) ⇒ Object



18
19
20
21
# File 'lib/capistrano/shared_config/config_file.rb', line 18

def self.name name
  name += '.yml' unless name =~ /\.(yml|conf|rb|ppk|pem)$/
  name
end

Instance Method Details

#contentObject



32
33
34
# File 'lib/capistrano/shared_config/config_file.rb', line 32

def content
  @content ||= ERB.new(File.read(location)).result(capistrano_binding)
end

#envObject



14
15
16
# File 'lib/capistrano/shared_config/config_file.rb', line 14

def env
  @env ||= eval('rails_env', capistrano_binding)
end

#locationObject



23
24
25
26
27
28
29
30
# File 'lib/capistrano/shared_config/config_file.rb', line 23

def location
  @location ||= [
    File.join('.', 'config', [env, name, 'erb'].join(?.)),
    File.join('.', 'config', [env, name].join(?.)),
    File.join('.', 'config', [name, 'erb'].join(?.)),
    File.join('.', 'config', name)
  ].detect(&File.method(:exists?))
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capistrano/shared_config/config_file.rb', line 36

def valid?
  begin
    case name
    when /\.yml$/
      YAML.load content
    when /\.rb$/
      eval("BEGIN {return true}\n#{content}", nil, name, 0)
    else
      # hope it is valid
    end

    true
  rescue Exception => exception
    @error = "Error in config file: #{exception.inspect}\n#{exception.message}"
    false
  end
end