class Thor::Tree::File

Public Class Methods

===(args) click to toggle source
# File lib/thor/tree/file.rb, line 7
def ===(args)
  args.is_a?(String) ||
  args.is_a?(Hash) && !args.empty? && args.keys.first.start_with?(':')
end
new(args, options = {}, config = {}) click to toggle source

@examples

File.new [ 'path/to/dst', ':create_file' ]
File.new [ 'path/to/dst', { ':create_file' => 'file content' } ]
File.new [ 'path/to/dst', ':copy_file' ]
File.new [ 'path/to/dst', { ':copy_file' => 'source_file' } ]
File.new [ 'path/to/dst', ':template' ]
File.new [ 'path/to/dst', { ':template' => 'source_file' } ]
Calls superclass method
# File lib/thor/tree/file.rb, line 29
def initialize(args, options = {}, config = {})
  @_path = Path.new args[0].to_s
  @_filename = @_path.basename
  options.merge! options_from_args(args[1])
  super
  self.destination_root = Writer.root_path
  File.template_variables.each { |key, value| instance_variable_set key, value }
end
set_template_variable(key, value) click to toggle source
# File lib/thor/tree/file.rb, line 12
def set_template_variable(key, value)
  @_template_variables ||= {}
  @_template_variables[key] = value
end
template_variables() click to toggle source
# File lib/thor/tree/file.rb, line 17
def template_variables
  @_template_variables || {}
end

Public Instance Methods

write() click to toggle source
# File lib/thor/tree/file.rb, line 39
def write
  case options[:action]
  when :copy_file
    copy_file options[:source] || @_filename, @_path, options[:thor_opts]
  when :create_file
    create_file @_path, options[:content].to_s, options[:thor_opts]
  when :template
    template options[:source] || @_filename, @_path, options[:thor_opts]
  end
end

Private Instance Methods

options_from_args(args) click to toggle source
# File lib/thor/tree/file.rb, line 53
def options_from_args(args)
  Hash.new.tap do |opts|
    [:copy_file, :create_file, :template].tap do |actions|
      case args
      when String
        opts[:action]  = actions.find { |action| action == args[1..-1].to_sym } || :create_file
      when Hash
        h = args.map { |k, v| [k[1..-1].to_sym, v] }.to_h
        opts[:action]  = actions.find { |action| action == h.keys.first } || :create_file
        opts[:content] = h.delete :create_file
        opts[:source]  = h.delete(:copy_file) || h.delete(:template)
        opts[:thor_opts] = h
      end
    end
  end
end