Method: Innate::Node#patterns_for

Defined in:
lib/innate/node.rb

#patterns_for(path) ⇒ Action

The innate beauty in Nitro, Ramaze, and Innate.

Will yield the name of the action and parameter for the action method in order of significance.

def foo__bar # responds to /foo/bar
def foo(bar) # also responds to /foo/bar

But foo__bar takes precedence because it’s more explicit.

The last fallback will always be the index action with all of the path turned into parameters.

Examples:

yielding possible combinations of action names and params


class Foo; include Innate::Node; map '/'; end

Foo.patterns_for('/'){|action, params| p action => params }
# => {"index"=>[]}

Foo.patterns_for('/foo/bar'){|action, params| p action => params }
# => {"foo__bar"=>[]}
# => {"foo"=>["bar"]}
# => {"index"=>["foo", "bar"]}

Foo.patterns_for('/foo/bar/baz'){|action, params| p action => params }
# => {"foo__bar__baz"=>[]}
# => {"foo__bar"=>["baz"]}
# => {"foo"=>["bar", "baz"]}
# => {"index"=>["foo", "bar", "baz"]}

Parameters:

  • path (String, #split)

    usually the PATH_INFO

Returns:

  • (Action)

    it actually returns the first non-nil/false result of yield

See Also:

Author:

  • manveru



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/innate/node.rb', line 739

def patterns_for(path)
  default_action_name = ancestral_trait[:default_action_name]
  separate_default_action = ancestral_trait[:separate_default_action]

  atoms = path.split('/')
  atoms.delete('')
  result = nil
  atoms.size.downto(0) do |len|
    action_name = atoms[0...len].join('__')

    next if separate_default_action && action_name == default_action_name

    params = atoms[len..-1]

    action_name = default_action_name if action_name.empty? &&
      (separate_default_action || params != [default_action_name])

    return result if result = yield(action_name, params)
  end

  return nil
end