Class: ConfigFor::Capistrano::Task

Inherits:
Rake::TaskLib
  • Object
show all
Includes:
Capistrano::DSL
Defined in:
lib/config_for/capistrano.rb

Overview

Rake Task generator for generating and uploading config files through Capistrano.

Examples:

generating task for database.yml

ConfigFor::Capistrano::Task.new(:database)

changing the folder

ConfigFor::Capistrano::Task.new(:database) { |task| task.folder = 'configuration' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) {|task| ... } ⇒ Task

Generates new tasks with for uploading #name

Parameters:

  • name (String, Symbol)

    name of this tasks and subtasks

  • &block

    gets evaluated before defining the tasks

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :override (true, false) — default: false

    upload file on every run

Yield Parameters:

  • task (Task)

    the task itself so you can modify it before it gets defined



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/config_for/capistrano.rb', line 112

def initialize(name, options = {}, &block)
  @name = name
  @folder = 'config'
  @file = "#{name}.yml"
  @variable = "#{name}_yml".to_sym
  @roles = options.fetch(:roles, :all)
  @override = options.fetch(:override, false)

  yield(self) if block_given?

  @config = ConfigFor::Capistrano::UploadFileTask.new(path, roles: @roles, override: @override, &method(:generate))

  desc "Generate #{name} uploader" unless ::Rake.application.last_description
  define
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



97
98
99
# File 'lib/config_for/capistrano.rb', line 97

def config
  @config
end

#folderString

Returns folder to upload the generated config.

Returns:

  • (String)

    folder to upload the generated config



105
106
107
# File 'lib/config_for/capistrano.rb', line 105

def folder
  @folder
end

#nameString

Returns the name of the task and subtasks namespace.

Returns:

  • (String)

    the name of the task and subtasks namespace



101
102
103
# File 'lib/config_for/capistrano.rb', line 101

def name
  @name
end

Instance Method Details

#pathObject

Is a join of #folder and #file



130
131
132
# File 'lib/config_for/capistrano.rb', line 130

def path
  ::File.join(@folder, @file)
end

#run_task(_task, _args) ⇒ Object

Invokes the task to do the upload



135
136
137
# File 'lib/config_for/capistrano.rb', line 135

def run_task(_task, _args)
  invoke("#{name}:upload")
end

#yamlString

Generated YAML content Gets the configuration from #variable, deep stringifies keys and returns YAML

Returns:

  • (String)

    serialized YAML



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/config_for/capistrano.rb', line 142

def yaml
  config = fetch(@variable, {})
  stringified = if config.respond_to?(:deep_stringify_keys)
                  config.deep_stringify_keys
                else
                  # TODO: remove when dropping rails 3 support
                  # two level stringify for rails 3 compatibility
                  config.stringify_keys.each_value(&:stringify_keys!)
                end
  stringified.to_yaml
end