Method: Cfruby::Cfp_MapOptions#map

Defined in:
lib/libcfenjin/cfp_mapoptions.rb

#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