Module: Hobber

Includes:
FileUtils, Thor::Shell
Defined in:
lib/hobber.rb,
lib/hobber/version.rb,
lib/hobber/render_action.rb,
lib/hobber/renderer/tilt.rb,
lib/hobber/rendered_object.rb,
lib/hobber/renderable_object.rb,
lib/hobber/rendered_object_saver.rb,
lib/hobber/tmpl_var_extractor/yaml.rb

Defined Under Namespace

Modules: Renderer, TmplVarExtractor Classes: ProblemParsingYaml, RenderAction, RenderError, RenderableObject, RenderedObject, RenderedObjectSaver

Constant Summary collapse

VERSION =
"0.6.5"

Instance Method Summary collapse

Instance Method Details

#assetsObject



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

def assets
  @assets ||= Sprockets::Environment.new
end

#chdir(path, &block) ⇒ Object



53
54
55
# File 'lib/hobber.rb', line 53

def chdir path, &block
  Dir.chdir(path, &block)
end

#default_rewrite_path(*args) ⇒ Object



31
32
33
34
# File 'lib/hobber.rb', line 31

def default_rewrite_path *args
  @default_rewrite_paths ||= []
  @default_rewrite_paths << args
end

#default_rewrite_url(*args) ⇒ Object



36
37
38
39
# File 'lib/hobber.rb', line 36

def default_rewrite_url *args
  @default_rewrite_urls ||= []
  @default_rewrite_urls << args
end

#default_tmpl_vars(vars) ⇒ Object



26
27
28
29
# File 'lib/hobber.rb', line 26

def default_tmpl_vars vars
  @default_tmpl_vars ||= {}
  @default_tmpl_vars.merge!(vars)
end

#file(path) ⇒ Object



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

def file path
  RenderableObject.new(path)
end

#load_yaml_config(path) ⇒ Object



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

def load_yaml_config path
  HashWithIndifferentAccess.new(YAML.load_file(path))
end

#remove_output_dir(dirname, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hobber.rb', line 41

def remove_output_dir dirname, options={}
  safe_mode = options.fetch(:safe, false)

  dirname = Pathname.new(dirname)
  if safe_mode && no?("Do you want to remove output dir [#{dirname.realpath}]? (y/yes/other=no)")
    return
  end
  FileUtils.rm_r dirname.realpath
rescue Errno::ENOENT
  # directory does not resolve
end

#render(robjects, opts = {}, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hobber.rb', line 65

def render robjects, opts={}, &block
  render_action = RenderAction.new(robjects.to_a, block.binding, &block)
  if render_action.rewrite_paths.empty?
    @default_rewrite_paths && @default_rewrite_paths.map { |args| render_action.rewrite_path *args }
  end
  if render_action.rewrite_urls.empty?
    @default_rewrite_urls && @default_rewrite_urls.map { |args| render_action.rewrite_url *args }
  end
  if @default_tmpl_vars
    render_action.tmpl_vars @default_tmpl_vars.merge(render_action.tmpl_vars)
  end
  rendered_objects = render_action.perform
  robjects.is_a?(Array) ? rendered_objects : rendered_objects.first
end

#save(*args) ⇒ Object



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

def save *args 
  options = args.last.is_a?(Hash) ? args.pop : {}
  logger  = options.fetch(:logger, Logger.new(STDOUT))
  safe_mode = options.fetch(:safe, false)
  args.flatten.map { |rendered_object|
    RenderedObjectSaver.new(rendered_object, safe_mode, logger).save
  }
end

#scan(glob) ⇒ Object



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

def scan glob
  Dir[glob].map { |p| RenderableObject.new(p) }
end