Class: Guard::CoffeeDripper

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/coffeedripper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ CoffeeDripper

Returns a new instance of CoffeeDripper.



20
21
22
23
24
25
26
27
28
# File 'lib/guard/coffeedripper.rb', line 20

def initialize(watchers=[], options={})
  super
  @watchers, @options = watchers, options
  @options[:output] ||= 'app/assets/'
  @options[:input] ||= 'app/beans/'
  @options[:ext] ||= 'bean'
  @options[:config] ||= 'config/coffee-dripper.yaml'
  load_config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/guard/coffeedripper.rb', line 8

def config
  @config
end

Class Method Details

.init(guard_name = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/guard/coffeedripper.rb', line 9

def self.init(guard_name = nil)
  super(guard_name)
  if !File.exist?("config/coffeedripper.yaml")
    puts "Writing new Guardfile to #{Dir.pwd}/config/coffee-dripper.yaml"
    FileUtils.cp(File.expand_path('../coffeedripper/templates/coffee-dripper.yaml', __FILE__), 'config/coffee-dripper.yaml')
  elsif guard_name.nil?
    ::Guard::UI.error "Guardfile already exists at #{Dir.pwd}/config/coffee-dripper.yaml"
    exit 1
  end
end

Instance Method Details

#change_bean(bean) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/guard/coffeedripper.rb', line 36

def change_bean(bean)
  @config.each do |coffee, beans|
    if beans.include? bean
      drip(coffee)
    end
  end
end

#drip(coffee_script) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/guard/coffeedripper.rb', line 51

def drip(coffee_script)
  puts "Drip Start for #{coffee_script}"
  beans = @config[coffee_script]
  output = File.join Dir.pwd, @options[:output], coffee_script
  str = ""
  beans.each do |bean|
    str += load_bean(bean)
  end
  write(output, str)
  puts "Drip End #{output}"
end

#drip_allObject



44
45
46
47
48
49
# File 'lib/guard/coffeedripper.rb', line 44

def drip_all
  puts "Drip Start for All"
  @config.each do |coffee, beans|
    drip(coffee)
  end
end

#load_bean(bean) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/guard/coffeedripper.rb', line 63

def load_bean(bean)
  str = ""
  input = File.join Dir.pwd, @options[:input], bean
  f = open(input)
  str = f.read
  f.close
  str << "\n"
end

#load_coffee(coffee) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/guard/coffeedripper.rb', line 72

def load_coffee(coffee)
  str = ""
  output = File.join Dir.pwd, @options[:output], coffee
  f = open(output)
  str = f.read
  f.close
  return str
end

#load_configObject



30
31
32
33
34
# File 'lib/guard/coffeedripper.rb', line 30

def load_config
  @config = YAML.load_file File.join Dir.pwd, @options[:config]
  return true if @config
  return false
end

#reloadObject

Called on Ctrl-Z signal This method should be mainly used for "reload" (really!) actions like reloading passenger/spork/bundler/...



106
107
108
109
# File 'lib/guard/coffeedripper.rb', line 106

def reload
  load_config
  run_all
end

#run_allObject

Called on Ctrl-\ signal This method should be principally used for long action like running all specs/tests/...



113
114
115
# File 'lib/guard/coffeedripper.rb', line 113

def run_all
  drip_all
end

#run_on_change(paths) ⇒ Object

Called on file(s) modifications



118
119
120
121
122
# File 'lib/guard/coffeedripper.rb', line 118

def run_on_change(paths)
  paths.each do |bean|
    change_bean(bean.split("/").last)
  end
end

#startObject

Called once when Guard starts Please override initialize method to init stuff



95
96
97
# File 'lib/guard/coffeedripper.rb', line 95

def start
  run_all
end

#stopObject

Called on Ctrl-C signal (when Guard quits)



100
101
102
# File 'lib/guard/coffeedripper.rb', line 100

def stop
  true
end

#write(output, str) ⇒ Object



81
82
83
84
85
# File 'lib/guard/coffeedripper.rb', line 81

def write(output,str)
  f = File.open(output,'w')
  f.puts str
  f.close
end