Class: Batteries::Tasks::Secret

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/batteries/tasks/secret.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :secret, options: {}) {|_self| ... } ⇒ Secret

Returns a new instance of Secret.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
# File 'lib/batteries/tasks/secret.rb', line 10

def initialize(name = :secret, options: {})
  @name = name
  @secret_file = options.fetch(:secret_file) { ".session_secret" }
  @description = options.fetch(:description) { default_description }
  @generator = options.fetch(:generator) { method(:default_generator) }

  yield self if block_given?

  define
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/batteries/tasks/secret.rb', line 8

def description
  @description
end

#generatorObject

Returns the value of attribute generator.



8
9
10
# File 'lib/batteries/tasks/secret.rb', line 8

def generator
  @generator
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/batteries/tasks/secret.rb', line 8

def name
  @name
end

#secret_fileObject

Returns the value of attribute secret_file.



8
9
10
# File 'lib/batteries/tasks/secret.rb', line 8

def secret_file
  @secret_file
end

Instance Method Details

#default_descriptionObject



33
34
35
# File 'lib/batteries/tasks/secret.rb', line 33

def default_description
  "Generate a session secret in #{secret_file}"
end

#default_generatorObject



28
29
30
31
# File 'lib/batteries/tasks/secret.rb', line 28

def default_generator
  require "securerandom"
  File.write(secret_file, SecureRandom.random_bytes(40))
end

#defineObject



21
22
23
24
25
26
# File 'lib/batteries/tasks/secret.rb', line 21

def define
  desc description
  task name do
    default_generator.call if default_generator.respond_to?(:call)
  end
end