Class: Wordmove::Movefile

Inherits:
Object
  • Object
show all
Defined in:
lib/wordmove/movefile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, start_dir = current_dir) ⇒ Movefile

Returns a new instance of Movefile.



5
6
7
8
9
# File 'lib/wordmove/movefile.rb', line 5

def initialize(name = nil, start_dir = current_dir)
  @logger = Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
  @name = name
  @start_dir = start_dir
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/wordmove/movefile.rb', line 3

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/wordmove/movefile.rb', line 3

def name
  @name
end

#start_dirObject (readonly)

Returns the value of attribute start_dir.



3
4
5
# File 'lib/wordmove/movefile.rb', line 3

def start_dir
  @start_dir
end

Instance Method Details

#environment(cli_options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/wordmove/movefile.rb', line 29

def environment(cli_options = {})
  options = fetch(false)
  available_enviroments = extract_available_envs(options)
  options.merge!(cli_options).deep_symbolize_keys!

  if available_enviroments.size > 1 && options[:environment].nil?
    raise(
      UndefinedEnvironment,
      "You need to specify an environment with --environment parameter"
    )
  end

  (options[:environment] || available_enviroments.first).to_sym
end

#fetch(verbose = true) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wordmove/movefile.rb', line 11

def fetch(verbose = true)
  entries = if name.nil?
              Dir["#{File.join(start_dir, '{M,m}ovefile')}{,.yml,.yaml}"]
            else
              Dir["#{File.join(start_dir, name)}{,.yml,.yaml}"]
            end

  if entries.empty?
    raise MovefileNotFound, "Could not find a valid Movefile" if last_dir?(start_dir)
    @start_dir = upper_dir(start_dir)
    return fetch
  end

  found = entries.first
  logger.task("Using Movefile: #{found}") if verbose == true
  YAML.safe_load(ERB.new(File.read(found)).result, [], [], true).deep_symbolize_keys!
end