module Crabfarm::Modes::Generator

Public Instance Methods

ensure() click to toggle source
# File lib/crabfarm/modes/generator.rb, line 82
def ensure
  generate_dir([@base_path] + @path, false)
  self
end
generate_app(_target, _name, _default_remote=nil) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 10
def generate_app(_target, _name, _default_remote=nil)
  with_base_path _target do
    binding = {
      name: _name,
      remote: _default_remote,
      version: Crabfarm::VERSION
    }

    path(_name).ensure
    path(_name, '.gitignore').render('dot_gitignore')
    path(_name, 'Gemfile').render('Gemfile', binding)
    path(_name, 'Crabfile').render('Crabfile', binding)
    path(_name, '.rspec').render('dot_rspec', binding)
    path(_name, '.crabfarm').render('dot_crabfarm', binding)
    path(_name, 'boot.rb').render('boot.rb', binding)
    path(_name, 'bin', 'crabfarm').render('crabfarm_bin', binding, 0755)
    path(_name, 'app', 'navigators', '.gitkeep').render('dot_gitkeep')
    path(_name, 'app', 'reducers', '.gitkeep').render('dot_gitkeep')
    path(_name, 'app', 'structs', '.gitkeep').render('dot_gitkeep')
    path(_name, 'app', 'helpers', '.gitkeep').render('dot_gitkeep')
    path(_name, 'spec', 'spec_helper.rb').render('spec_helper.rb', binding)
    path(_name, 'spec', 'snapshots', '.gitkeep').render('dot_gitkeep')
    path(_name, 'spec', 'mementos', '.gitkeep').render('dot_gitkeep')
    path(_name, 'spec', 'integration', '.gitkeep').render('dot_gitkeep')
    path(_name, 'logs', '.gitkeep').render('dot_gitkeep')
    path(_name, 'README.md').render('README.md', binding)
  end
end
generate_navigator(_target, _class_name, _options={}) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 39
def generate_navigator(_target, _class_name, _options={})
  validate_class_name _class_name

  route = Utils::Naming.route_from_constant _class_name
  with_base_path _target do
    binding = { navigator_class: _class_name, navigator_url: _options[:url] }
    path(*(['app', 'navigators'] + route[0...-1] + [route.last + '.rb'])).render('navigator.rb', binding)
    path(*(['spec', 'navigators'] + route[0...-1] + [route.last + '_spec.rb'])).render('navigator_spec.rb', binding)
  end
end
generate_reducer(_target, _class_name) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 50
def generate_reducer(_target, _class_name)
  validate_class_name _class_name

  _class_name = _class_name + 'Reducer'
  route = Utils::Naming.route_from_constant _class_name
  with_base_path _target do
    binding = { reducer_class: _class_name }
    path(*(['app', 'reducers'] + route[0...-1] + [route.last + '.rb'])).render('reducer.rb', binding)
    path(*(['spec', 'reducers'] + route[0...-1] + [route.last + '_spec.rb'])).render('reducer_spec.rb', binding)
  end
end
generate_struct(_target, _class_name) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 62
def generate_struct(_target, _class_name)
  validate_class_name _class_name

  route = Utils::Naming.route_from_constant _class_name
  with_base_path _target do
    binding = { struct_class: _class_name }
    path(*(['app', 'structs'] + route[0...-1] + [route.last + '.rb'])).render('struct.rb', binding)
  end
end
path(*_args) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 77
def path(*_args)
  @path = _args
  self
end
render(_template, _binding={}, _mod=nil) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 87
def render(_template, _binding={}, _mod=nil)
  path = [@base_path] + @path
  generate_dir(path[0..-2], true)
  render_template(_template, _binding, path, _mod)
  self
end
with_base_path(_target) { || ... } click to toggle source
# File lib/crabfarm/modes/generator.rb, line 72
def with_base_path(_target)
  @base_path = _target
  yield
end

Private Instance Methods

eval_template_with_hash(_path, _hash) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 122
def eval_template_with_hash(_path, _hash)
  erb = ERB.new(File.read _path)
  erb.result(OpenStruct.new(_hash).instance_eval { binding })
end
generate_dir(_path, _silent) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 96
def generate_dir(_path, _silent)
  path = File.join(*_path)
  dir = Pathname.new path
  unless dir.exist?
    render_op "mkdir", path, :green
    dir.mkpath
  else
    render_op "skip", path, :yellow unless _silent
  end
end
render_op(_op, _message, _color) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 131
def render_op(_op, _message, _color)
  puts _op.rjust(10).color(_color) + '  ' + _message
end
render_template(_template, _binding, _path, _mod) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 107
def render_template(_template, _binding, _path, _mod)
  template = File.join(template_dir, _template) + '.erb'
  output = File.join(*_path)

  unless Pathname.new(output).exist?
    render_op "render", output, :green
    File.open(output, "w") do |f|
      f.write eval_template_with_hash(template, _binding)
      f.chmod(_mod) unless _mod.nil?
    end
  else
    render_op "skip", output, :yellow
  end
end
template_dir() click to toggle source
# File lib/crabfarm/modes/generator.rb, line 127
def template_dir
  File.expand_path('../../templates', __FILE__)
end
validate_class_name(_name) click to toggle source
# File lib/crabfarm/modes/generator.rb, line 135
def validate_class_name(_name)
  raise Crabfarm::ArgumentError.new "Invalid class name '#{_name}'" unless Utils::Naming.is_constant_name? _name
end