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',
	'inf' => 'infinite'
}
EMPTY_OPT =
{}
FILE_OPT =

Shared file options

{ 
	 'mode' => [ :mode ], 'owner' => [ :owner ], 'group' => [ :group ]
}
DIR_OPT =

directories:

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

files:

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

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 ]
MAP =

map a Cfruby method with its options

{ 
	'directories' => DIR_OPT,
	'files'       => FILES_OPT,
	'copy'        => COPY_OPT
}

Instance Method Summary collapse

Instance Method Details

#expand_synonyms(list) ⇒ Object

Clone a list with expanded keywords



158
159
160
161
162
163
164
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 158

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 ]



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 97

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 value != nil
				# ---- a value was passed (e.g. mode=0644)
				if isproc
					# it is an in place method, so let it handle the request
					ret_options.merge! default.call(name,value)
				else
					# Use the actual value
					ret_options[parname] = value
				end
				argslist.delete(name)
			elsif default != nil and !isproc
				# ---- 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
	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



141
142
143
144
145
146
147
148
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 141

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

#synonym(name) ⇒ Object



150
151
152
153
154
155
# File 'lib/libcfenjin/cfp_mapoptions.rb', line 150

def synonym name
	if SYNONYM[name]
		return SYNONYM[name]
	end
	name
end