class Pliny::Commands::Generator::Base

Attributes

name[R]
options[R]
stream[R]

Public Class Methods

new(name, options = {}, stream = $stdout) click to toggle source
# File lib/pliny/commands/generator/base.rb, line 12
def initialize(name, options = {}, stream = $stdout)
  @name = name ? normalize_name(name) : nil
  @options = options
  @stream = stream
end

Public Instance Methods

display(msg) click to toggle source
# File lib/pliny/commands/generator/base.rb, line 38
def display(msg)
  stream.puts msg
end
field_name() click to toggle source
# File lib/pliny/commands/generator/base.rb, line 26
def field_name
  name.tableize.singularize
end
plural_class_name() click to toggle source
# File lib/pliny/commands/generator/base.rb, line 22
def plural_class_name
  name.pluralize.camelize
end
pluralized_file_name() click to toggle source
# File lib/pliny/commands/generator/base.rb, line 30
def pluralized_file_name
  name.tableize
end
render_template(template_file, vars = {}) click to toggle source
# File lib/pliny/commands/generator/base.rb, line 42
def render_template(template_file, vars = {})
  template_path = File.dirname(__FILE__) + "/../../templates/#{template_file}"
  template = ERB.new(File.read(template_path), trim_mode: '>')
  context = OpenStruct.new(vars)
  template.result(context.instance_eval { binding })
end
singular_class_name() click to toggle source
# File lib/pliny/commands/generator/base.rb, line 18
def singular_class_name
  name.singularize.camelize
end
table_name() click to toggle source
# File lib/pliny/commands/generator/base.rb, line 34
def table_name
  name.tableize.tr('/', '_')
end
write_file(destination_path) { || ... } click to toggle source
# File lib/pliny/commands/generator/base.rb, line 55
def write_file(destination_path)
  FileUtils.mkdir_p(File.dirname(destination_path))
  File.open(destination_path, 'w') do |f|
    f.puts yield
  end
end
write_template(template_file, destination_path, vars = {}) click to toggle source
# File lib/pliny/commands/generator/base.rb, line 49
def write_template(template_file, destination_path, vars = {})
  write_file(destination_path) do
    render_template(template_file, vars)
  end
end

Private Instance Methods

normalize_name(name) click to toggle source
# File lib/pliny/commands/generator/base.rb, line 64
def normalize_name(name)
  name.underscore.tr(' ', '_')
end