Class: Lidia::Command
- Inherits:
-
Object
- Object
- Lidia::Command
- Defined in:
- lib/lidia/command.rb
Constant Summary collapse
- SYNTAX_REX =
/\%([a-z_]*)\{([a-z_]*)\}/
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#current_action_cmd ⇒ Object
Returns the value of attribute current_action_cmd.
-
#current_action_config ⇒ Object
Returns the value of attribute current_action_config.
-
#current_action_name ⇒ Object
Returns the value of attribute current_action_name.
-
#definition_file ⇒ Object
readonly
Returns the value of attribute definition_file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #command_for(name) ⇒ Object
- #get_parameter_value(parameter_name) ⇒ Object
-
#initialize(command_name, parameters = {}, options = {}) {|_self| ... } ⇒ Command
constructor
Lidia’s is written to provide an easy way to manage commands calls.
- #inspect ⇒ Object
- #set_parameter_value(parameter_name, value) ⇒ Object
Constructor Details
#initialize(command_name, parameters = {}, options = {}) {|_self| ... } ⇒ Command
Lidia’s is written to provide an easy way to manage commands calls. Commands structure can be defined in an easy yaml file.
# $your editor ls.lidia.yml
:parameters:
:columns:
:option: -l
:description: listing format
By default, lidia loads definition files from gem installation folder ../source, or you can pass with :definition_file
the location
>> Lidia::Command.new('ls', {}, :definition_file => 'ls.lidia.yml')
=> Lidia::Command
+ name: ls,
+ definition file at:/var/www/lidia/sources/ls.lidia.yml
+ parameters:
* name: columns group: option , value: nil
Values can be passed as argument on new, or creating by block
>> Lidia::Command.new('ls', {:columns => true })
?> Lidia::Command.new('ls') do |l|
?> l.columns = true
>> end
=> Lidia::Command
+ name: ls,
+ definition file at:/var/www/lidia/sources/ls.lidia.yml
+ parameters:
* name: columns group: option , value: true
Once u have defined all the parameters that your command will use, define which actions can be done, por example, list_by_columns
# your editor ls.lidia.yml
# :actions:
# :list_by_columns:
# :parameters:
# :required: [ :columns ]
Now ask for command_for
lidia list_by_columns
>> l = Lidia::Command.new('ls', {:columns => nil })
=> Lidia::Command
+ name: ls,
+ definition file at:/var/www/lidia/sources/ls.lidia.yml
+ parameters:
* name: columns group: option , value: nil
>> l.command_for(:list_by_columns)
ArgumentError: Parameter columns required but not valid
Ooops, parameter -l is required, to list_by_columns action, so set it to true
>> l.columns = true
=> true
>> l.command_for(:list_by_columns)
=> "/bin/ls -l"
We want sometimes, list other directory. so prepare defintion file to get an argument
# $your editor ls.lidia.yml
# :parameters:
# :list_what:
# :description: what do u want to list?
this option can be used for listing_by_columns to, but it is not mandatary
# $your editor ls.lidia.yml
# :actions:
# :list_by_columns:
# :parameters:
# :required: [ :columns ]
# :optional: [ :list_what ]
Now, no exceptions is raised, but argument can be used
>> l = Lidia::Command.new('ls', {:columns => true })
=> Lidia::Command
+ name: ls,
+ definition file at:/var/www/lidia/sources/ls.lidia.yml
+ parameters:
* name: columns group: option , value: true
* name: list_what group: argument , value: nil
>> l.command_for(:list_by_columns)
=> "/bin/ls -l "
>> l.list_what = '/tmp'
=> "/tmp"
>> l.command_for(:list_by_columns)
=> "/bin/ls -l \"/tmp\" "
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lidia/command.rb', line 107 def initialize(command_name, parameters = {}, = {}) @name = command_name @options = @source = Lidia::Source.new({:include => ([:definition_file] ? File.dirname([:definition_file]) : nil) }) @config = load_config @path = @options[:path] || @config[:path] || %x[ which #{ command_name } ].strip @parameters = Lidia::Parameter.build(self, parameters) @parameters.each do |parameter| get_block = Proc.new{ self.get_parameter_value(parameter.name) } set_block = Proc.new{ |v| self.set_parameter_value(parameter.name, v) } self.class.send(:define_method, "#{ parameter.name }", get_block) self.class.send(:define_method, "#{ parameter.name }=", set_block) end yield self if block_given? end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/lidia/command.rb', line 8 def config @config end |
#current_action_cmd ⇒ Object
Returns the value of attribute current_action_cmd.
16 17 18 |
# File 'lib/lidia/command.rb', line 16 def current_action_cmd @current_action_cmd end |
#current_action_config ⇒ Object
Returns the value of attribute current_action_config.
15 16 17 |
# File 'lib/lidia/command.rb', line 15 def current_action_config @current_action_config end |
#current_action_name ⇒ Object
Returns the value of attribute current_action_name.
14 15 16 |
# File 'lib/lidia/command.rb', line 14 def current_action_name @current_action_name end |
#definition_file ⇒ Object (readonly)
Returns the value of attribute definition_file.
9 10 11 |
# File 'lib/lidia/command.rb', line 9 def definition_file @definition_file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/lidia/command.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/lidia/command.rb', line 11 def @options end |
#parameters ⇒ Object
Returns the value of attribute parameters.
13 14 15 |
# File 'lib/lidia/command.rb', line 13 def parameters @parameters end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/lidia/command.rb', line 10 def path @path end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
7 8 9 |
# File 'lib/lidia/command.rb', line 7 def source @source end |
Instance Method Details
#command_for(name) ⇒ Object
128 129 130 |
# File 'lib/lidia/command.rb', line 128 def command_for(name) build_action(name) ? @current_action_cmd : raise(ArgumentError, "not valid action #{ name }") end |
#get_parameter_value(parameter_name) ⇒ Object
141 142 143 144 |
# File 'lib/lidia/command.rb', line 141 def get_parameter_value(parameter_name) parameter = @parameters.select{ |p| p.name == parameter_name }.first parameter ? parameter.value : raise(NoMethodError) end |
#inspect ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/lidia/command.rb', line 132 def inspect %(#{ self.class.name } + name: #{ @name }, + definition file at:#{ @definition_file } + parameters: #{ @parameters.sort{|a,b| a.name.to_s <=> b.name.to_s }.map(&:inspect).join("\n ") } ) end |
#set_parameter_value(parameter_name, value) ⇒ Object
146 147 148 149 |
# File 'lib/lidia/command.rb', line 146 def set_parameter_value(parameter_name, value) parameter = @parameters.select{ |p| p.name == parameter_name }.first parameter ? (parameter.value = value) : raise(NoMethodError) end |