Class: Pliny::Commands::Generator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pliny/commands/generator/base.rb

Direct Known Subclasses

Endpoint, Mediator, Migration, Model, Schema, Serializer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, stream = $stdout) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
# File 'lib/pliny/commands/generator/base.rb', line 14

def initialize(name, options = {}, stream = $stdout)
  @name = name ? normalize_name(name) : nil
  @options = options
  @stream = stream
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/pliny/commands/generator/base.rb', line 12

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/pliny/commands/generator/base.rb', line 12

def options
  @options
end

#streamObject (readonly)

Returns the value of attribute stream.



12
13
14
# File 'lib/pliny/commands/generator/base.rb', line 12

def stream
  @stream
end

Instance Method Details

#display(msg) ⇒ Object



40
41
42
# File 'lib/pliny/commands/generator/base.rb', line 40

def display(msg)
  stream.puts msg
end

#field_nameObject



28
29
30
# File 'lib/pliny/commands/generator/base.rb', line 28

def field_name
  name.tableize.singularize
end

#plural_class_nameObject



24
25
26
# File 'lib/pliny/commands/generator/base.rb', line 24

def plural_class_name
  name.pluralize.camelize
end

#pluralized_file_nameObject



32
33
34
# File 'lib/pliny/commands/generator/base.rb', line 32

def pluralized_file_name
  name.tableize
end

#render_template(template_file, vars = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/pliny/commands/generator/base.rb', line 44

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_nameObject



20
21
22
# File 'lib/pliny/commands/generator/base.rb', line 20

def singular_class_name
  name.singularize.camelize
end

#table_nameObject



36
37
38
# File 'lib/pliny/commands/generator/base.rb', line 36

def table_name
  name.tableize.tr("/", "_")
end

#write_file(destination_path) ⇒ Object



57
58
59
60
61
62
# File 'lib/pliny/commands/generator/base.rb', line 57

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 = {}) ⇒ Object



51
52
53
54
55
# File 'lib/pliny/commands/generator/base.rb', line 51

def write_template(template_file, destination_path, vars = {})
  write_file(destination_path) do
    render_template(template_file, vars)
  end
end