Class: Pliny::Commands::Generator::Base
- Inherits:
-
Object
- Object
- Pliny::Commands::Generator::Base
show all
- Defined in:
- lib/pliny/commands/generator/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, options = {}, stream = $stdout) ⇒ Base
Returns a new instance of Base.
12
13
14
15
16
|
# 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
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
10
11
12
|
# File 'lib/pliny/commands/generator/base.rb', line 10
def name
@name
end
|
#options ⇒ Object
Returns the value of attribute options.
10
11
12
|
# File 'lib/pliny/commands/generator/base.rb', line 10
def options
@options
end
|
#stream ⇒ Object
Returns the value of attribute stream.
10
11
12
|
# File 'lib/pliny/commands/generator/base.rb', line 10
def stream
@stream
end
|
Instance Method Details
#display(msg) ⇒ Object
38
39
40
|
# File 'lib/pliny/commands/generator/base.rb', line 38
def display(msg)
stream.puts msg
end
|
#field_name ⇒ Object
26
27
28
|
# File 'lib/pliny/commands/generator/base.rb', line 26
def field_name
name.tableize.singularize
end
|
#plural_class_name ⇒ Object
22
23
24
|
# File 'lib/pliny/commands/generator/base.rb', line 22
def plural_class_name
name.pluralize.camelize
end
|
#pluralized_file_name ⇒ Object
30
31
32
|
# File 'lib/pliny/commands/generator/base.rb', line 30
def pluralized_file_name
name.tableize
end
|
#render_template(template_file, vars = {}) ⇒ Object
42
43
44
45
46
47
|
# 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), 0, '>')
context = OpenStruct.new(vars)
template.result(context.instance_eval { binding })
end
|
#singular_class_name ⇒ Object
18
19
20
|
# File 'lib/pliny/commands/generator/base.rb', line 18
def singular_class_name
name.singularize.camelize
end
|
#table_name ⇒ Object
34
35
36
|
# File 'lib/pliny/commands/generator/base.rb', line 34
def table_name
name.tableize.tr('/', '_')
end
|
#write_file(destination_path) ⇒ Object
55
56
57
58
59
60
|
# 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 = {}) ⇒ Object
49
50
51
52
53
|
# 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
|