Module: NamedParameters

Defined in:
lib/named_parameters.rb

Overview

Include this module to enable the NamedParameters#named_parameters macros

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

Enables the <method>_has_named_parameter ghost methods



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/named_parameters.rb', line 63

def method_missing method, *args, &block
  # catch only methods like <method_name> + _has_named_parameters + <mode definition>
  if /\A(?<method_name>.+)_has_named_parameters(?<mode>_super_strict|_strict_super|_super|_strict)?\z/ =~ method.to_s
    # find out mode
    mode = "" if mode.nil?
    call_super = mode.include? 'super'
    # find out strict
    strict     = mode.include? 'strict'
    # redefine
    _redefine_with_named_parameters(method_name,
                                    args[0] || { },
                                    strict,
                                    call_super)
  else
    # if method is not like above pattern, do the original stuff
    super method, *args, &block
  end
end

Instance Method Details

#my_method_has_named_parameters(optionals = { }) ⇒ Object

Works like NamedParameters#named_parameters, but instead of using it right before a method definition, you use it afterwards. You can also add the strict and super mode. Replace the my_method with the name of the method that you want to have named parameters



13
14
15
# File 'lib/named_parameters.rb', line 13

def my_method_has_named_parameters(optionals = { })
  # Empty method just for the documentation. Those methods are realized in NamedParameters#method_missing
end

#named_parameters(optionals = { }) ⇒ Object

Makes the next method callable via named parameters (options hash) The caller hash may contain keys that are not contained in the parameters of the next method

Parameters:

  • optionals (Hash[Symbol,Object]) (defaults to: { })

    Optional default values for the parameters



20
21
22
# File 'lib/named_parameters.rb', line 20

def named_parameters(optionals = { })
  _named_parameters optionals, false, false
end

#named_parameters_strict(optionals = { }) ⇒ Object

Makes the next method callable via named parameters (options hash) The caller hash may only contain keys that are in the parameters of the next method

Parameters:

  • optionals (Hash[Symbol,Object]) (defaults to: { })

    Optional default values for the parameters



27
28
29
# File 'lib/named_parameters.rb', line 27

def named_parameters_strict(optionals = { })
  _named_parameters optionals, true, false
end

#named_parameters_strict_super(optionals = { }) ⇒ Object Also known as: named_parameters_super_strict

Makes the next method callable via named parameters (options hash) and calls super with this options hash as the only parameter The caller hash may only contain keys that are in the parameters of the next method

Parameters:

  • optionals (Hash[Symbol,Object]) (defaults to: { })

    Optional default values for the parameters



43
44
45
# File 'lib/named_parameters.rb', line 43

def named_parameters_strict_super(optionals = { })
  _named_parameters optionals, true, true
end

#named_parameters_super(optionals = { }) ⇒ Object

Makes the next method callable via named parameters (options hash) and calls super with this options hash as the only parameter The caller hash may contain keys that are not contained in the parameters of the next method

Parameters:

  • optionals (Hash[Symbol,Object]) (defaults to: { })

    Optional default values for the parameters



35
36
37
# File 'lib/named_parameters.rb', line 35

def named_parameters_super(optionals = { })
  _named_parameters optionals, false, true
end