Module: Innate::Node

Includes:
Traited
Included in:
Ramaze::Controller
Defined in:
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb

Overview

The nervous system of Innate, so you can relax.

Node may be included into any class to make it a valid responder to requests.

The major difference between this and the old Ramaze controller is that every Node acts as a standalone application with its own dispatcher.

What's also an important difference is the fact that Node is a module, so we don't have to spend a lot of time designing the perfect subclassing scheme.

This makes dispatching more fun, avoids a lot of processing that is done by Rack anyway and lets you tailor your application down to the last action exactly the way you want without worrying about side-effects to other Nodes.

Upon inclusion, it will also include Trinity and Helper to provide you with Request, Response, Session instances, and all the standard helper methods as well as the ability to simply add other helpers.

Please note that method_missing will not be considered when building an Action. There might be future demand for this, but for now you can simply use def index(*args); end to make a catch-all action.

Constant Summary

NODE_LIST =
Set.new

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Traited

#ancestral_trait, #ancestral_trait_values, #class_trait, #each_ancestral_trait, #trait

Instance Attribute Details

- (Object) layout_templates (readonly)

Returns the value of attribute layout_templates



31
32
33
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 31

def layout_templates
  @layout_templates
end

- (Object) method_arities (readonly)

Returns the value of attribute method_arities



31
32
33
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 31

def method_arities
  @method_arities
end

- (Object) view_templates (readonly)

Returns the value of attribute view_templates



31
32
33
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 31

def view_templates
  @view_templates
end

Class Method Details

+ (Object) generate_mapping(object_name = self.name)



97
98
99
100
101
102
103
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 97

def self.generate_mapping(object_name = self.name)
  return '/' if NODE_LIST.size == 1
  parts = object_name.split('::').map{|part|
    part.gsub(/^[A-Z]+/){|sub| sub.downcase }.gsub(/[A-Z]+[^A-Z]/, '_\&')
  }
  '/' << parts.join('/').downcase
end

+ (Object) included(into)

Upon inclusion we make ourselves comfortable.



70
71
72
73
74
75
76
77
78
79
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 70

def self.included(into)
  into.__send__(:include, Helper)
  into.extend(Trinity, self)

  NODE_LIST << into

  return if into.provide_set?
  into.provide(:html, :engine => :Etanni)
  into.trait(:provide_set => false)
end

+ (Object) setup

node mapping procedure

when Node is included into an object, it's added to NODE_LIST when object::map(location) is sent, it maps the object into DynaMap when Innate.start is issued, it calls Node::setup Node::setup iterates NODE_LIST and maps all objects not in DynaMap by using Node::generate_mapping(object.name) as location

when object::map(nil) is sent, the object will be skipped in Node::setup



91
92
93
94
95
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 91

def self.setup
  NODE_LIST.each{|node|
    node.map(generate_mapping(node.name)) unless node.trait[:skip_node_map]
  }
end

Instance Method Details

- (Innate::Response) action_found(action)

Executed once an Action has been found.

Reset the Response instance, catch :respond and :redirect. Action#call has to return a String.

Parameters:

Returns:

See Also:

Author:

  • manveru



328
329
330
331
332
333
334
335
336
337
338
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 328

def action_found(action)
  response = catch(:respond){ catch(:redirect){ action.call }}

  unless response.respond_to?(:finish)
    self.response.write(response)
    response = self.response
  end

  response['Content-Type'] ||= action.options[:content_type]
  response
end

- (Object) action_missing(path)

The default handler in case no action was found, kind of method_missing. Must modify the response in order to have any lasting effect.

Reasoning: * We are doing this is in order to avoid tons of special error handling code that would impact runtime and make the overall API more complicated. * This cannot be a normal action is that methods defined in Innate::Node will never be considered for actions.

To use a normal action with template do following:

Examples:


class Hi
  include Innate::Node
  map '/'

  def self.action_missing(path)
    return if path == '/not_found'
    # No normal action, runs on bare metal
    try_resolve('/not_found')
  end

  def not_found
    # Normal action
    "Sorry, I do not exist"
  end
end

Parameters:

  • path (String)

See Also:

Author:

  • manveru



375
376
377
378
379
380
381
382
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 375

def action_missing(path)
  response = Current.response
  response.status = 404
  response['Content-Type'] = 'text/plain'
  response.write("No action found at: %p" % path)

  response
end

- (Object) alias_view(to, from, node = nil)

Aliasing one view from another. The aliases are inherited, and the optional third +node+ parameter indicates the Node to take the view from.

The argument order is identical with alias and alias_method, which quite honestly confuses me, but at least we stay consistent.

Note that the parameters have been simplified in comparision with Ramaze::Controller::template where the second parameter may be a Controller or the name of the template. We take that now as an optional third parameter.

Examples:

class Foo
  include Innate::Node

  # Use the 'foo' view when calling 'bar'
  alias_view 'bar', 'foo'

  # Use the 'foo' view from FooBar node when calling 'bar'
  alias_view 'bar', 'foo', FooBar
end

Parameters:

  • to (#to_s)

    view that should be replaced

  • from (#to_s)

    view to use or Node.

  • node (#nil?, Node) (defaults to: nil)

    optionally obtain view from this Node

See Also:

  • Node::find_aliased_view

Author:

  • manveru



658
659
660
661
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 658

def alias_view(to, from, node = nil)
  trait[:alias_view] || trait(:alias_view => {})
  trait[:alias_view][to.to_s] = node ? [from.to_s, node] : from.to_s
end

- (Binding) binding

For compatibility with new Kernel#binding behaviour in 1.9

Returns:

  • (Binding)

    binding of the instance being rendered.

See Also:

Author:

  • manveru



940
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 940

def binding; super end

- (Array) call(env)

This makes the Node a valid application for Rack. +env+ is the environment hash passed from the Rack::Handler

We rely on correct PATH_INFO.

As defined by the Rack spec, PATH_INFO may be empty if it wants the root of the application, so we insert '/' to make our dispatcher simple.

Innate will not rescue any errors for you or do any error handling, this should be done by an underlying middleware.

We do however log errors at some vital points in order to provide you with feedback in your logs.

A lot of functionality in here relies on the fact that call is executed within Current#call which populates the variables used by Trinity. So if you use the Node directly as a middleware make sure that you #use Innate::Current as a middleware before it.

Parameters:

  • env (Hash)

Returns:

  • (Array)

See Also:

Author:

  • manveru



293
294
295
296
297
298
299
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 293

def call(env)
  path = env['PATH_INFO']
  path << '/' if path.empty?

  response.reset
  try_resolve(path).finish
end

- (Action?) fill_action(action, given_name)

Now we're talking Action, we try to find a matching template and method, if we can't find either we go to the next pattern, otherwise we answer with an Action with everything we know so far about the demands of the client.

Parameters:

  • given_name (String)

    the name extracted from REQUEST_PATH

Returns:

See Also:

Author:

  • manveru



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 457

def fill_action(action, given_name)
  needs_method = action.options[:needs_method]
  wish = action.wish

  patterns_for(given_name) do |name, params|
    method = find_method(name, params)

    next unless method if needs_method
    next unless method if params.any?
    next unless (view = find_view(name, wish)) || method

    params.map!{|param| Rack::Utils.unescape(param) }

    action.merge!(:method => method, :view => view, :params => params,
                  :layout => find_layout(name, wish))
  end
end

- (nil, String) find_aliased_view(action_name, wish)

Resolve one level of aliasing for the given +action_name+ and +wish+.

Parameters:

  • action_name (String)
  • wish (String)

Returns:

  • (nil, String)

    the absolute path to the aliased template or nil

See Also:

  • Node::find_view

Author:

  • manveru



673
674
675
676
677
678
679
680
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 673

def find_aliased_view(action_name, wish)
  aliased_name, aliased_node = ancestral_trait[:alias_view][action_name]
  return unless aliased_name

  aliased_node ||= self
  aliased_node.update_view_mappings
  aliased_node.find_view(aliased_name, wish)
end

- (Array?) find_layout(name, wish)

TODO:

allow layouts combined of method and view... hairy :)

Try to find a suitable value for the layout. This may be a template or the name of a method.

If a layout could be found, an Array with two elements is returned, the first indicating the kind of layout (:layout|:view|:method), the second the found value, which may be a String or Symbol.

Parameters:

  • name (String)
  • wish (String)

Returns:

  • (Array, nil)

See Also:

Author:

  • manveru



492
493
494
495
496
497
498
499
500
501
502
503
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 492

def find_layout(name, wish)
  return unless layout = ancestral_trait[:layout]
  return unless layout = layout.call(name, wish) if layout.respond_to?(:call)

  if found = to_layout(layout, wish)
    [:layout, found]
  elsif found = find_view(layout, wish)
    [:view, found]
  elsif found = find_method(layout, [])
    [:method, found]
  end
end

- (String, Symbol) find_method(name, params)

TODO:

Once 1.9 is mainstream we can use Method#parameters to do accurate prediction

We check arity if possible, but will happily dispatch to any method that has default parameters. If you don't want your method to be responsible for messing up a request you should think twice about the arguments you specify due to limitations in Ruby.

So if you want your method to take only one parameter which may have a default value following will work fine:

def index(foo = "bar", *rest)

But following will respond to /arg1/arg2 and then fail due to ArgumentError:

def index(foo = "bar")

Here a glance at how parameters are expressed in arity:

def index(a) # => 1 def index(a = :a) # => -1 def index(a, r) # => -2 def index(a = :a, r) # => -1

def index(a, b) # => 2 def index(a, b, r) # => -3 def index(a, b = :b) # => -2 def index(a, b = :b, r) # => -2

def index(a = :a, b = :b) # => -1 def index(a = :a, b = :b, *r) # => -1

Parameters:

  • name (String, Symbol)
  • params (Array)

Returns:

  • (String, Symbol)

See Also:

Author:

  • manveru



546
547
548
549
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 546

def find_method(name, params)
  return unless arity = method_arities[name.to_s]
  name if arity == params.size || arity < 0
end

- (Array) find_provide(path)

Resolve possible provides for the given +path+ from #provides.

Parameters:

  • path (String)

Returns:

  • (Array)

    with name, wish, engine

See Also:

  • Node::provides

Author:

  • manveru



431
432
433
434
435
436
437
438
439
440
441
442
443
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 431

def find_provide(path)
  pr = provides

  name, wish, engine = path, 'html', pr['html_handler']

  pr.find do |key, value|
    key = key[/(.*)_handler$/, 1]
    next unless path =~ /^(.+)\.#{key}$/i
    name, wish, engine = $1, key, value
  end

  return name, wish, engine
end

- (String?) find_view(action_name, wish)

Try to find the best template for the given basename and wish and respect aliased views.

Parameters:

  • action_name (#to_s)
  • wish (#to_s)

Returns:

  • (String, nil)

    depending whether a template could be found

See Also:

Author:

  • manveru



602
603
604
605
606
607
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 602

def find_view(action_name, wish)
  aliased = find_aliased_view(action_name, wish)
  return aliased if aliased

  to_view(action_name, wish)
end

- (Proc, String) layout(layout_name = nil, &block)

Define a layout to use on this Node.

A Node can only have one layout, although the template being chosen can depend on #provides.

Examples:

layout :foo
layout do |name, wish|
  name == 'foo' ? 'dark' : 'bright'
end
layout :foo do |name, wish|
  wish == 'html'
end

Parameters:

  • layout_name (String, #to_s) (defaults to: nil)

    basename without extension of the layout to use

  • block (Proc, #call)

    called on every dispatch if no name given

Returns:

  • (Proc, String)

    The assigned name or block

See Also:

Author:

  • manveru



725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 725

def layout(layout_name = nil, &block)
  if layout_name and block
    # default name, but still check with block
    trait(:layout => lambda{|name, wish| layout_name.to_s if block.call(name, wish) })
  elsif layout_name
    # name of a method or template
    trait(:layout => layout_name.to_s)
  elsif block
    # call block every request with name and wish, returned value is name
    # of layout template or method
    trait(:layout => block)
  else
    # remove layout for this node
    trait(:layout => nil)
  end

  return ancestral_trait[:layout]
end

- (Array<String>+) layout_mappings

Combine Innate.options.layouts with either the ancestral_trait[:layouts] or the #mapping if the trait yields an empty Array.

Returns:

  • (Array<String>, Array<Array<String>>)

See Also:

  • Innate::Node.{Node{Node#map_layouts}

Author:

  • manveru



1009
1010
1011
1012
1013
1014
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 1009

def layout_mappings
  paths = [*ancestral_trait[:layouts]]
  paths = ['/'] if paths.empty?

  [[*options.layouts].flatten, [*paths].flatten]
end

- (Object) map(location)

Shortcut to map or remap this Node.

Examples:

Usage for explicit mapping:


class FooBar
  include Innate::Node
  map '/foo_bar'
end

Innate.to(FooBar) # => '/foo_bar'

Usage for automatic mapping:


class FooBar
  include Innate::Node
  map mapping
end

Innate.to(FooBar) # => '/foo_bar'

Parameters:

  • location (#to_s)

See Also:

  • SingletonMethods::map

Author:

  • manveru



150
151
152
153
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 150

def map(location)
  trait :skip_node_map => true
  Innate.map(location, self)
end

- (Node) map_layouts(*locations)

Set the paths for lookup below the Innate.options.layouts paths.

Parameters:

  • locations (String, Array<String>)

    Any number of strings indicating the paths where layout templates may be located, relative to Innate.options.roots/Innate.options.layouts

Returns:

See Also:

  • Innate::Node.{Node{Node#layout_mappings}

Author:

  • manveru



996
997
998
999
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 996

def map_layouts(*locations)
  trait :layouts => locations.flatten.uniq
  self
end

- (Node) map_views(*locations)

Set the paths for lookup below the Innate.options.views paths.

Parameters:

  • locations (String, Array<String>)

    Any number of strings indicating the paths where view templates may be located, relative to Innate.options.roots/Innate.options.views

Returns:

See Also:

  • Innate::Node.{Node{Node#view_mappings}

Author:

  • manveru



965
966
967
968
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 965

def map_views(*locations)
  trait :views => locations.flatten.uniq
  self
end

- (String) mapping

Tries to find the relative url that this Innate::Node is mapped to. If it cannot find one it will instead generate one based on the snake_cased name of itself.

Examples:

Usage:


class FooBar
  include Innate::Node
end
FooBar.mapping # => '/foo_bar'

Returns:

  • (String)

    the relative path to the node

See Also:

Author:

  • manveru



121
122
123
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 121

def mapping
  Innate.to(self)
end

- (true, false) needs_method?

Whether an Action can be built without a method.

The default is to allow actions that use only a view template, but you might want to turn this on, for example if you have partials in your view directories.

Examples:

turning needs_method? on


class Foo
  Innate.node('/')
end

Foo.needs_method? # => true
Foo.trait :needs_method => false
Foo.needs_method? # => false

Returns:

  • (true, false)

    (false)

See Also:

  • Innate::Node.{Node{Node#fill_action}

Author:

  • manveru



1041
1042
1043
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 1041

def needs_method?
  ancestral_trait[:needs_method]
end

- (Object) options



1016
1017
1018
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 1016

def options
  Innate.options
end

- (Action) patterns_for(path)

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



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 782

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

- (Array) possible_exts_for(wish)

Answer with an array of possible extensions in order of significance for the given +wish+.

Parameters:

  • wish (#to_s)

    the extension (no leading '.')

Returns:

  • (Array)

    list of exts valid for this +wish+

See Also:

Author:

  • manveru



927
928
929
930
931
932
933
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 927

def possible_exts_for(wish)
  pr = provides
  return unless engine = pr["#{wish}_handler"]
  View.exts_of(engine).map{|e_ext|
    [[*wish].map{|w_ext| /#{w_ext}\.#{e_ext}$/ }, /#{e_ext}$/]
  }.flatten
end

- (Array) possible_paths_for(mappings)

Answer with an array of possible paths in order of significance for template lookup of the given +mappings+.

Parameters:

  • mappings (#map)

    An array two Arrays of inner and outer directories.

Returns:

  • (Array)

See Also:

  • update_layout_mappings update_template_mappings

Author:

  • manveru



910
911
912
913
914
915
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 910

def possible_paths_for(mappings)
  root_mappings.map{|root|
    mappings.first.map{|inner|
      mappings.last.map{|outer|
        ::File.join(root, inner, outer, '/') }}}.flatten
end

- (Object) provide(format, param = {}, &block)

TODO:

The comment of this method may be too short for the effects it has on the rest of Innate, if you feel something is missing please let me know.

Note:

If you specify a block when calling this method you'll have to take care of rendering views and the like yourself. If you merely want to set a extension and content type you can omit the block.

Specify which way contents are provided and processed.

Use this to set a templating engine, custom Content-Type, or pass a block to take over the processing of the Action and template yourself.

Provides set via this method will be inherited into subclasses.

The +format+ is extracted from the PATH_INFO, it simply represents the last extension name in the path.

The provide also has influence on the chosen templates for the Action.

Given a request to /list.rss the template lookup first tries to find list.rss.erb, if that fails it falls back to list.erb. If neither of these are available it will try to use the return value of the method in the Action as template.

A request to /list.yaml would match the format 'yaml'

Examples:

providing RSS with ERB templating


provide :rss, :engine => :ERB

providing a yaml version of actions


class Articles
  include Innate::Node
  map '/article'

  provide(:yaml, :type => 'text/yaml'){|action, value| value.to_yaml }

  def list
    @articles = Article.list
  end
end

providing plain text inspect version


class Articles
  include Innate::Node
  map '/article'

  provide(:txt, :type => 'text/plain'){|action, value| value.inspect }

  def list
    @articles = Article.list
  end
end

Parameters:

  • block (Proc)

    upon calling the action, [action, value] will be passed to it and its return value becomes the response body.

  • param (Hash) (defaults to: {})

    a customizable set of options

Options Hash (param):

  • :engine (Symbol String)

    Name of an engine for View::get

  • :type (String)

    default Content-Type if none was set in Response

Raises:

  • (ArgumentError)

    if neither a block nor an engine was given

See Also:

  • Node#provides

Author:

  • manveru



229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 229

def provide(format, param = {}, &block)
  if param.respond_to?(:to_hash)
    param = param.to_hash
    handler = block || View.get(param[:engine])
    content_type = param[:type]
  else
    handler = View.get(param)
  end

  raise(ArgumentError, "Need an engine or block") unless handler

  trait("#{format}_handler"      => handler, :provide_set => true)
  trait("#{format}_content_type" => content_type) if content_type
end

- (Hash) provide_handlers

Returns:

  • (Hash)

See Also:



262
263
264
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 262

def provide_handlers
  ancestral_trait.reject { |key, value| key !~ /_handler$/ }
end

- (true, false) provide_set?

This will return true if the only provides set are by included.

The reasoning behind this is to determine whether the user has touched the provides at all, in which case we will not override the provides in subclasses.

Returns:

  • (true, false)

    (false)

See Also:

  • Innate::Node.{Node{Node::included}

Author:

  • manveru



1057
1058
1059
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 1057

def provide_set?
  ancestral_trait[:provide_set]
end

- (Hash) provides

Returns the list of provide handlers. This list is cached after the first call to this method.

Returns:

  • (Hash)


250
251
252
253
254
255
256
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 250

def provides
  if ancestral_trait[:cache_provides]
    return ancestral_trait[:provides_cache][self] ||= provide_handlers
  else
    return provide_handlers
  end
end

- (nil, Action) resolve(path, options = {})

Let's get down to business, first check if we got any wishes regarding the representation from the client, otherwise we will assume he wants html.

Parameters:

  • path (String)
  • options (Hash) (defaults to: {})

Returns:

See Also:

  • Node::update_method_arities Node::find_action

Author:

  • manveru



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 396

def resolve(path, options = {})
  name, wish, engine = find_provide(path)
  node = (respond_to?(:ancestors) && respond_to?(:new)) ? self : self.class

  action = Action.create(
    :node    => node,
    :wish    => wish,
    :engine  => engine,
    :path    => path,
    :options => options
  )

  if !action.options.key?(:needs_method)
    action.options[:needs_method] = node.needs_method?
  end

  if content_type = node.ancestral_trait["#{wish}_content_type"]
    action.options[:content_type] = content_type
  end

  node.update_method_arities
  node.update_template_mappings
  node.fill_action(action, name)
end

- (Array) root_mappings

make sure this is an Array and a new instance so modification on the wrapping array doesn't affect the original option. [*arr].object_id == arr.object_id if arr is an Array

Returns:

  • (Array)

    list of root directories

Author:

  • manveru



950
951
952
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 950

def root_mappings
  [*options.roots].flatten
end

- (nil, String) to_layout(action_name, wish)

Find the best matching action_name for the layout, if any.

This is mostly an abstract method that you might find handy if you want to do vastly different layout lookup.

Parameters:

  • action_name (String)
  • wish (String)

Returns:

  • (nil, String)

    the absolute path to the template or nil

See Also:

  • {Node#root_mappings} {Node#layout_mappings}

Author:

  • manveru



695
696
697
698
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 695

def to_layout(action_name, wish)
  return unless files = layout_templates[wish.to_s]
  files[action_name.to_s]
end

- (nil, String) to_template(path, wish)

Try to find a template at the given +path+ for +wish+.

Since Innate supports multiple paths to templates the +path+ has to be an Array that may be nested one level.

Examples:

Usage to find available templates


# This assumes following files:
# view/foo.erb
# view/bar.erb
# view/bar.rss.erb
# view/bar.yaml.erb

class FooBar
  Innate.node('/')
end

FooBar.to_template(['.', 'view', '/', 'foo'], 'html')
# => "./view/foo.erb"
FooBar.to_template(['.', 'view', '/', 'foo'], 'yaml')
# => "./view/foo.erb"
FooBar.to_template(['.', 'view', '/', 'foo'], 'rss')
# => "./view/foo.erb"

FooBar.to_template(['.', 'view', '/', 'bar'], 'html')
# => "./view/bar.erb"
FooBar.to_template(['.', 'view', '/', 'bar'], 'yaml')
# => "./view/bar.yaml.erb"
FooBar.to_template(['.', 'view', '/', 'bar'], 'rss')
# => "./view/bar.rss.erb"

Parameters:

  • path (Array<Array<String>>, Array<String>)

    array containing strings and nested (1 level) arrays containing strings

  • wish (String)

Returns:

  • (nil, String)

    relative path to the first template found

See Also:

Author:

  • manveru



845
846
847
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 845

def to_template(path, wish)
  to_view(path, wish) || to_layout(path, wish)
end

- (String?) to_view(action_name, wish)

Try to find the best template for the given basename and wish.

This method is mostly here for symetry with #to_layout and to allow you overriding the template lookup easily.

Parameters:

  • action_name (#to_s)
  • wish (#to_s)

Returns:

  • (String, nil)

    depending whether a template could be found

See Also:

  • {Node#to_template} {Node#root_mappings} {Node#view_mappings} {Node#to_template}

Author:

  • manveru



623
624
625
626
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 623

def to_view(action_name, wish)
  return unless files = view_templates[wish.to_s]
  files[action_name.to_s]
end

- (Response) try_resolve(path)

Let's try to find some valid action for given +path+. Otherwise we dispatch to #action_missing.

Parameters:

  • path (String)

    from env['PATH_INFO']

Returns:

See Also:

Author:

  • manveru



311
312
313
314
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 311

def try_resolve(path)
  action = resolve(path)
  action ? action_found(action) : action_missing(path)
end

- (Object) update_layout_mappings



863
864
865
866
867
868
869
870
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 863

def update_layout_mappings
  if ancestral_trait[:fast_mappings]
    return @layout_templates if @layout_templates
  end

  paths = possible_paths_for(layout_mappings)
  @layout_templates = update_mapping_shared(paths)
end

- (Object) update_mapping_shared(paths)



872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 872

def update_mapping_shared(paths)
  mapping = {}
  paths.reject!{|path| !File.directory?(path) }

  provides.each do |wish_key, engine|
    wish = wish_key[/(.*)_handler/, 1]
    exts = possible_exts_for(wish)

    paths.reverse_each do |path|
      Find.find(path) do |file|
        exts.each do |ext|
          next unless file =~ ext

          case file.sub(path, '').gsub('/', '__')
          when /^(.*)\.(.*)\.(.*)$/
            action_name, wish_ext, engine_ext = $1, $2, $3
          when /^(.*)\.(.*)$/
            action_name, wish_ext, engine_ext = $1, wish, $2
          end

          mapping[wish_ext] ||= {}
          mapping[wish_ext][action_name] = file
        end
      end
    end
  end

  return mapping
end

- (Hash) update_method_arities

Answer with a hash, keys are method names, values are method arities.

Note that this will be executed once for every request, once we have settled things down a bit more we can switch to update based on Reloader hooks and update once on startup. However, that may cause problems with dynamically created methods, so let's play it safe for now.

Examples:


Hi.update_method_arities
# => {'index' => 0, 'foo' => -1, 'bar' => 2}

Returns:

  • (Hash)

    mapping the name of the methods to their arity

See Also:



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 567

def update_method_arities
  if ancestral_trait[:cache_method_arities] \
  and ancestral_trait[:method_arity_cache][self]
    return ancestral_trait[:method_arity_cache][self]
  end

  @method_arities = {}

  exposed = ancestors & Helper::EXPOSE.to_a
  higher = ancestors.select{|ancestor| ancestor < Innate::Node }

  (higher + exposed).reverse_each do |ancestor|
    ancestor.public_instance_methods(false).each do |im|
      @method_arities[im.to_s] = ancestor.instance_method(im).arity
    end
  end

  if ancestral_trait[:cache_method_arities]
    ancestral_trait[:method_arity_cache][self] = @method_arities
  end

  @method_arities
end

- (Object) update_template_mappings



849
850
851
852
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 849

def update_template_mappings
  update_view_mappings
  update_layout_mappings
end

- (Object) update_view_mappings



854
855
856
857
858
859
860
861
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 854

def update_view_mappings
  if ancestral_trait[:fast_mappings]
    return @view_templates if @view_templates
  end

  paths = possible_paths_for(view_mappings)
  @view_templates = update_mapping_shared(paths)
end

- (Array<String>+) view_mappings

Combine Innate.options.views with either the ancestral_trait[:views] or the #mapping if the trait yields an empty Array.

Returns:

  • (Array<String>, Array<Array<String>>)

See Also:

  • Innate::Node.{Node{Node#map_views}

Author:

  • manveru



978
979
980
981
982
983
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 978

def view_mappings
  paths = [*ancestral_trait[:views]]
  paths = [mapping] if paths.empty?

  [[*options.views].flatten, [*paths].flatten]
end