Class: TemplateBuilder::App::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/template_builder/app/command.rb

Direct Known Subclasses

Add, New, Show

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Command

Returns a new instance of Command.



10
11
12
13
14
15
16
17
18
# File 'lib/template_builder/app/command.rb', line 10

def initialize( opts = {} )
  @stdout = opts[:stdout] || $stdout
  @stderr = opts[:stderr] || $stderr
  
  @config = {:name => nil, :force => nil, :verbose =>nil}
  @config_param = {}
  @command = []
  standard_parameters.each_key{ |key| @config_param[key] = TemplateBuilder::App::Helper::Parameter.new(:priority=>priority(key)) }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/template_builder/app/command.rb', line 8

def config
  @config
end

#stderrObject (readonly)

Returns the value of attribute stderr.



7
8
9
# File 'lib/template_builder/app/command.rb', line 7

def stderr
  @stderr
end

#stdoutObject (readonly)

:startdoc:



6
7
8
# File 'lib/template_builder/app/command.rb', line 6

def stdout
  @stdout
end

Class Method Details

.inherited(other) ⇒ Object



205
206
207
# File 'lib/template_builder/app/command.rb', line 205

def self.inherited( other )
  other.extend ClassMethods
end

.standard_optionsObject



109
110
111
112
113
114
# File 'lib/template_builder/app/command.rb', line 109

def self.standard_options 
  @standard_options = {:verbose => ['-v', '--verbose', 'Enable verbose output.',
          lambda { config[:verbose] = true }],
                       :force =>  ['-f', '--force', 'Force creating file.',
          lambda { config[:force] = true }] }
end

.standard_parametersObject



116
117
118
119
120
# File 'lib/template_builder/app/command.rb', line 116

def self.standard_parameters 
  return @standard_parameters if @standard_parameters
  @standard_parameters = FileAnalyzer.load_standard_parameters
  @standard_parameters      
end

Instance Method Details

#ask_for(framework_name) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/template_builder/app/command.rb', line 122

def ask_for(framework_name)
  all_frameworks = FileAnalyzer.all_frameworks_for framework_name
  puts "Choose your "+framework_name.to_s+" framework ? (enter 0 for none)"
  until @config_param[framework_name].name
    all_frameworks.each_with_index{ |value,index| puts "("+(index+1).to_s+") "+value.to_s+"\r\n" }
    answer = STDIN.gets
    @config_param[framework_name].name = all_frameworks[answer.to_i-1] if (1..all_frameworks.length).include? answer.to_i
    @config_param[framework_name] = "none" if 0 == answer
  end 
  
end

#forceObject



33
34
35
# File 'lib/template_builder/app/command.rb', line 33

def force
   @config[:force]
end

#nameObject

The project name from the command line.



29
30
31
# File 'lib/template_builder/app/command.rb', line 29

def name
  @config[:name]
end

#parse(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/template_builder/app/command.rb', line 54

def parse( args )
  opts = OptionParser.new

  opts.banner = 'NAME'
  opts.separator "  template_builder v#{::TemplateBuilder.version}"
  opts.separator ''
  if self.class.synopsis
    opts.separator 'SYNOPSIS'
    self.class.synopsis.split("\n").each { |line| opts.separator "  #{line.strip}" }
    opts.separator ''
  end

  if self.class.description
    opts.separator 'DESCRIPTION'
    self.class.description.split("\n").each { |line| opts.separator "  #{line.strip}" }
    opts.separator ''
  end
  if self.class.parameters and not self.class.parameters.empty?
    opts.separator 'PARAMETER'
    self.class.parameters.each { |parameter|
      case parameter
      when Array
        parameter << method(parameter.pop) if parameter.last =~ %r/^__/
        opts.on(*parameter)
      when String
        opts.separator("  #{parameter.strip}")
      else puts parameter;opts.separator('') end
    }
    opts.separator ''
  end
  if self.class.options and not self.class.options.empty?
    opts.separator 'OPTION'
    self.class.options.each { |option|
      case option
      when Array
        option << method(option.pop) if option.last =~ %r/^__/
        opts.on(*option)
      when String
        opts.separator("  #{option.strip}")
      else opts.separator('') end
    }
    opts.separator ''
  end
  opts.separator '  Common Options:'
  opts.on_tail( '-h', '--help', 'show this message' ) {
    stdout.puts opts
    exit
  }
  opts.on_tail ''
  opts.parse! args
  return opts
end

#priority(key) ⇒ Object



24
25
26
# File 'lib/template_builder/app/command.rb', line 24

def priority(key)
  FileAnalyzer.load_priority key
end

#run(args) ⇒ Object

Raises:

  • (NotImplementedError)


20
21
22
# File 'lib/template_builder/app/command.rb', line 20

def run( args )
  raise NotImplementedError
end

#run_framework(fileManager, opts = {}) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/template_builder/app/command.rb', line 134

def run_framework(fileManager, opts = {})
  framework = FileAnalyzer.load_framework opts
  fileManager.write_framework_introduction opts[:name]
  framework.run fileManager
  special_case_data_mapper fileManager if @config_param[:orm].name == "data_mapper"
  @command << framework.command
end

#special_case_data_mapper(fileManager) ⇒ Object



142
143
144
# File 'lib/template_builder/app/command.rb', line 142

def special_case_data_mapper(fileManager)
  fileManager.write "gem 'dm-"+@config_param[:database].name.to_s+"-adapter'"
end

#standard_optionsObject



44
45
46
# File 'lib/template_builder/app/command.rb', line 44

def standard_options
  Command.standard_options
end

#standard_parametersObject



48
49
50
# File 'lib/template_builder/app/command.rb', line 48

def standard_parameters
  Command.standard_parameters
end

#verbose?Boolean

Returns true if the user has requested verbose messages.

Returns:

  • (Boolean)


39
40
41
# File 'lib/template_builder/app/command.rb', line 39

def verbose?
  @config[:verbose]
end