Module: Cfruby::Cfp_MapOptions

Included in:
CfrubyRuntime
Defined in:
lib/libcfenjin/cfp_mapoptions.rb

Defined Under Namespace

Modules: Args Classes: UnknownParameterError

Constant Summary collapse

SYNONYM =

Synonym list - may be read from file later

{
	'a' => 'action', 'act'=>'action',
	'b' => 'backup',
	'm' => 'mode',
	'd' => 'dest',
	'o' => 'owner',
	'g' => 'group',
	'r' => 'recurse', 'rec'=>'recurse',
    'p' => 'pattern',
	'inf' => 'infinite'
}
EMPTY_OPT =
{}
FILE_OPT =

Shared file options

{ 
	'mode' => [ :mode ], 'owner' => [ :user ], 'group' => [ :group ],
    'shell' => [ :shell ]
}
RECURSE_OPT =
{ 
  'recurse'	=> [ :void, Proc.new { |n,v| Args.recurse(n,v) } ] 
}
AGE_OPT =
{ 
  'age'	=> [ :void, Proc.new { |n,v| Args.age(n,v) } ] 
}
PATTERN_OPT =
{
  'pattern' => [ :glob ]
}
REMOVE_OPT =
{
  'rmdirs'	=> [ :void, Proc.new { |n,v| Args.rmdirs(n,v) } ] 
}
DIR =

directories:

dirname m=766 o=myname ...
[ { 'mode' => [ :mode, 0775 ], 
    'makeparent' => [ :makeparent, true] }, FILE_OPT 
]
FILES =

files:

filename o=user g=user m=444 rec=inf ...
[ RECURSE_OPT, FILE_OPT, PATTERN_OPT,
  { 'action' 	=> [ :void, Proc.new { |n,v| Args.action(n,v) } ] }
]
COPY =

FILES_OPT = [ { ‘recurse’ => [ :recursive, false ], ‘action’ => [:filesonly, false] }, FILE_OPT ] copy:

filename dest=destname o=user ...
[ { 'mode' => [ :mode, 0400 ], 'backup' => [ :backup, true ] ,
'onlyonchange' => [ :onlyonchange , true ], 'force' => [ :force, true ] }, 
FILE_OPT ]
TIDY =
[ RECURSE_OPT, REMOVE_OPT, PATTERN_OPT, AGE_OPT, 
			{ 'links' => [ :followsymlinks, false ] }
]
MAP =

map a Cfruby method with its options

{ 
	'directories' => DIR,
	'files'       => FILES,
	'copy'        => COPY,
	'tidy'        => TIDY
}

Instance Method Summary collapse

Instance Method Details

#check_pattern(attrib, filename) ⇒ Object

If a pattern parameter is supported make sure recurse is set to 1 if the filename points to a directory.



206
207
208
209
210
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 206

def check_pattern attrib,filename
  return attrib if attrib['recurse']!=nil
  attrib['recurse']='1' if attrib['pattern'] and File.directory?(filename)
  attrib
end

#expand_synonyms(list) ⇒ Object

Clone a list with expanded keywords



230
231
232
233
234
235
236
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 230

def expand_synonyms list
	explist = {}
	list.each do | key, value |
		explist[synonym(key)] = value
	end
	explist
end

#map(action, attribs) ⇒ Object

Attributes are mapped including default values. The first options found in the map is returned. If an option is not on the MAP an exception is raised. Mind: options handled by the Cfruby parser itself are deleted beforehand (see cfrubyruntime.rb) by a call to pop_options.

Each option that is legal should be in the list. The translated value is pointed to. The second parameter is the default value - which can be a method that is called instead to figure out the right value. Examples:

Just translate the value of mode m=444 ‘mode’ => [ :mode ] Pass the default value of mode if not given ‘mode’ => [ :mode, 0644 ] Call the translator method on m=444, passing the value as a parameter ‘mode’ => [ :mode, translate_mode ]



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 160

def map action,attribs
	ret_options = {}
	argslist = expand_synonyms(attribs) # keeps track of found items
	# argslist contains Cfruby style options list, e.g. 
	#   {"mode"=>0644, "owner"=>"user", "group"=>"users"}
	#
	# with this list walk the whole map
	MAP[action].each do | maps |
		maps.each do | option, a |
			name = synonym(option)
			value = synonym(argslist[name])
			parname = a[0]
			default = a[1]
			# p [name,value,default,a]
			isproc = (default and default.kind_of? Proc)
        if isproc
				# it is an in place method, so let it handle the request
				ret_options.merge! default.call(name,value)
				argslist.delete(name)
        else  
          if value != nil
            # ---- a value was passed (e.g. mode=0644)
					# Use the actual value
					ret_options[parname] = value
            argslist.delete(name)
			  elsif default != nil
            # ---- no value passed, but a default value is available
            ret_options[parname] = default
            argslist.delete(name)
          else
            # ---- No value and no default! Just means this is a legal and
            # unused parameter
            next
          end
        end
		end
	end
	if argslist.size > 0
		raise UnknownParameterError,"Unresolved options for action '#{action}': <#{argslist.keys.join(',')}>"
	end
	# p [ret_options]
	ret_options
end

#pop_options(opts, *named) ⇒ Object

Remove options from a list and return them



213
214
215
216
217
218
219
220
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 213

def pop_options opts,*named
	ret = []
	named.each do | n |
		ret.push opts[n]
		opts.delete(n)
	end
	ret
end

#synonym(name) ⇒ Object



222
223
224
225
226
227
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 222

def synonym name
	if name!= nil and name.kind_of?(String) and SYNONYM[name.downcase]
		return SYNONYM[name]
	end
	name
end