Module: Fontcustom::Utility

Includes:
Thor::Actions
Included in:
Base, CLI, Generator::Font, Generator::Template, Manifest, Options, Watcher
Defined in:
lib/fontcustom/utility.rb

Defined Under Namespace

Modules: HashWithMethodAccess

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.shellObject

Hacks that allow Thor::Actions and Thor::Shell to be used in Fontcustom classes.



18
19
20
# File 'lib/fontcustom/utility.rb', line 18

def self.shell
  @shell || Thor::Shell::Color.new
end

Instance Method Details

#behaviorObject



26
27
28
# File 'lib/fontcustom/utility.rb', line 26

def behavior
  :invoke
end

#destination_rootObject



34
35
36
37
# File 'lib/fontcustom/utility.rb', line 34

def destination_root
  @destination_stack ||= [project_root]
  @destination_stack.last
end

#line_break(n = 14) ⇒ Object

magic number for Thor say_status line breaks



105
106
107
# File 'lib/fontcustom/utility.rb', line 105

def line_break(n = 14)
  "\n#{" " * n}"
end

#methodize_hash(hash) ⇒ Object



61
62
63
# File 'lib/fontcustom/utility.rb', line 61

def methodize_hash(hash)
  hash.extend HashWithMethodAccess
end

#optionsObject



109
110
111
112
113
114
115
# File 'lib/fontcustom/utility.rb', line 109

def options
  if @data
    @data[:options]
  else
    @options || @cli_options || @config_options || {}
  end
end

#project_rootObject

Paths



69
70
71
72
73
74
75
# File 'lib/fontcustom/utility.rb', line 69

def project_root
  if @manifest.is_a? String
    File.dirname @manifest
  else
    File.dirname @manifest.manifest
  end
end

#say_changed(status, changed) ⇒ Object



99
100
101
102
# File 'lib/fontcustom/utility.rb', line 99

def say_changed(status, changed)
  return if options[:quiet] || ! options[:debug] && status == :delete
  say_status status, changed.join(line_break)
end

#say_message(status, message, color = nil) ⇒ Object

Messages



93
94
95
96
97
# File 'lib/fontcustom/utility.rb', line 93

def say_message(status, message, color = nil)
  return if options[:quiet] && status != :error && status != :debug
  color = :red if [:error, :debug, :warn].include?(status)
  say_status status, message, color
end

#say_status(*args) ⇒ Object



30
31
32
# File 'lib/fontcustom/utility.rb', line 30

def say_status(*args)
  shell.say_status *args
end

#shellObject



22
23
24
# File 'lib/fontcustom/utility.rb', line 22

def shell
  Fontcustom::Utility.shell
end

#source_pathsObject



39
40
41
# File 'lib/fontcustom/utility.rb', line 39

def source_paths
  @source_paths ||= [File.join(Fontcustom.gem_lib, "templates"), Dir.pwd]
end

#symbolize_hash(hash) ⇒ Object



57
58
59
# File 'lib/fontcustom/utility.rb', line 57

def symbolize_hash(hash)
  hash.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
end

#write_file(file, content = "", message = nil, message_body = nil) ⇒ Object

File Manipulation



81
82
83
84
85
86
87
# File 'lib/fontcustom/utility.rb', line 81

def write_file(file, content = "", message = nil, message_body = nil)
  File.open(file, "w") { |f| f.write(content) }
  if message
    body = message_body || file
    say_message message, body
  end
end