Module: Peony::Utils
- Defined in:
- lib/peony/utils.rb
Instance Method Summary collapse
-
#erb(file, b = binding) ⇒ Object
### erb Evaluates an ERB block in the current scope and returns a string.
- #find_in_directories(paths, name, file_only) ⇒ Object
- #find_recipes(name, file_only = true) ⇒ Object
- #find_templates(name, file_only = true) ⇒ Object
-
#method_missing(meth, *args, &blk) ⇒ Object
### method_missing Hook to get settings.
- #parse_args ⇒ Object
- #recipes_paths ⇒ Object
-
#set(key, *args, &block) ⇒ Object
### set Sets settings.
-
#set_default(key, *args, &block) ⇒ Object
### set_default Sets default settings.
-
#settings ⇒ Object
### settings Accesses the settings hash.
- #template(from, to, override = false) ⇒ Object
- #template_paths ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
### method_missing Hook to get settings. See #settings for an explanation.
Returns things.
106 107 108 109 110 111 112 |
# File 'lib/peony/utils.rb', line 106 def method_missing(meth, *args, &blk) if settings.include? meth settings.send meth, *args, &blk else super end end |
Instance Method Details
#erb(file, b = binding) ⇒ Object
### erb Evaluates an ERB block in the current scope and returns a string.
a = 1
b = 2
# Assuming foo.erb is <%= a %> and <%= b %>
puts erb('foo.erb')
#=> "1 and 2"
Returns the output string of the ERB template.
32 33 34 35 |
# File 'lib/peony/utils.rb', line 32 def erb(file, b=binding) require 'erb' ERB.new(File.read(file), nil, '-').result(b) end |
#find_in_directories(paths, name, file_only) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/peony/utils.rb', line 92 def find_in_directories(paths, name, file_only) templates = [] paths.each do|path| templates += Dir[File.(name, path)].reject{|filename| file_only && File.directory?(filename) } end templates end |
#find_recipes(name, file_only = true) ⇒ Object
88 89 90 |
# File 'lib/peony/utils.rb', line 88 def find_recipes(name, file_only=true) find_in_directories(recipes_paths, name, file_only) end |
#find_templates(name, file_only = true) ⇒ Object
80 81 82 |
# File 'lib/peony/utils.rb', line 80 def find_templates(name, file_only=true) find_in_directories(template_paths, name, file_only) end |
#parse_args ⇒ Object
4 5 6 7 8 |
# File 'lib/peony/utils.rb', line 4 def parse_args ARGV.each do|arg| set $1.strip.to_sym, $2 if arg =~ /([^=]+)=(.+)/ end end |
#recipes_paths ⇒ Object
84 85 86 |
# File 'lib/peony/utils.rb', line 84 def recipes_paths ["#{Dir.pwd}/recipes", File.('../../recipes', __dir__)] end |
#set(key, *args, &block) ⇒ Object
### set Sets settings. Sets given symbol ‘key` to value in `value`.
Returns the value.
set :domain, 'kickflip.me'
44 45 46 |
# File 'lib/peony/utils.rb', line 44 def set(key, *args, &block) settings.send :"#{key}=", *args, block end |
#set_default(key, *args, &block) ⇒ Object
### set_default Sets default settings. Sets given symbol ‘key` to value in `value` only if the key isn’t set yet.
Returns the value.
set_default :term_mode, :pretty
set :term_mode, :system
settings.term_mode.should == :system
set :term_mode, :system
set_default :term_mode, :pretty
settings.term_mode.should == :system
61 62 63 |
# File 'lib/peony/utils.rb', line 61 def set_default(key, *args, &block) set(key, *args, block) unless settings.send(:"#{key}?") end |
#settings ⇒ Object
### settings Accesses the settings hash.
set :domain, 'kickflip.me'
settings.domain #=> 'kickflip.me'
domain #=> 'kickflip.me'
72 73 74 |
# File 'lib/peony/utils.rb', line 72 def settings @settings ||= Settings.new end |
#template(from, to, override = false) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/peony/utils.rb', line 10 def template(from, to, override=false) template = find_templates(from).first raise "Can't find tempalte #{from} in directory #{search_paths}." unless template raise "File #{to} have already exists." if !override && File.exists?(to) say "copy #{template} to #{to}", :green open(to, "w+") do|out| out.write(erb(template)) end end |
#template_paths ⇒ Object
76 77 78 |
# File 'lib/peony/utils.rb', line 76 def template_paths ["#{Dir.pwd}/templates", File.('../../templates', __dir__)] end |