Module: Innate::SingletonMethods

Included in:
Ramaze
Defined in:
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/dynamap.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/route.rb,
/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/state.rb

Overview

script_name, path_info = env['SCRIPT_NAME'], env['PATH_INFO'] answer = app.call(env) env.merge!('SCRIPT_NAME' => script_name, 'PATH_INFO' => path_info) answer

Instance Method Summary (collapse)

Instance Method Details

- (Object) at(location)

Answer with object at +location+.

Examples:


class Hello
  include Innate::Node
  map '/'
end

Innate.at('/') # => Hello


78
79
80
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/dynamap.rb', line 78

def at(location)
  DynaMap.at(location)
end

- (Object) defer



18
19
20
21
22
23
24
25
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/state.rb', line 18

def defer
  outer = ::Thread.current
  ::Thread.new{
    inner = ::Thread.current
    outer.keys.each{|k| inner[k] = outer[k] }
    yield
  }
end

- (Object) map(location, object = nil, &block)

Maps the given +object+ or +block+ to +location+, +object+ must respond to

call in order to be of any use.

Examples:

with passed +object+


Innate.map('/', lambda{|env| [200, {}, "Hello, World"] })
Innate.at('/').call({}) # => [200, {}, "Hello, World"]

with passed +block+


Innate.map('/'){|env| [200, {}, ['Hello, World!']] }
Innate.at('/').call({})


64
65
66
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/dynamap.rb', line 64

def map(location, object = nil, &block)
  DynaMap.map(location, object || block)
end

- (Class, Module) node(location, node = nil)

Convenience method to include the Node module into +node+ and map to a +location+.

Parameters:

  • location (#to_s)

    where the node is mapped to

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

    the class that will be a node, will try to look it up if not given

Returns:

  • (Class, Module)

    the node argument or detected class will be returned

See Also:

  • SingletonMethods::node_from_backtrace

Author:

  • manveru



1076
1077
1078
1079
1080
1081
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 1076

def node(location, node = nil)
  node ||= node_from_backtrace(caller)
  node.__send__(:include, Node)
  node.map(location)
  node
end

- (Class, Module) node_from_backtrace(backtrace)

Cheap hack that works reasonably well to avoid passing self all the time to Innate::node We simply search the file that Innate::node was called in for the first class definition above the line that Innate::node was called and look up the constant. If there are any problems with this (filenames containing ':' or metaprogramming) just pass the node parameter explicitly to Innate::node

Parameters:

  • backtrace (Array<String>, #[])

Returns:

  • (Class, Module)

See Also:

  • SingletonMethods::node

Author:

  • manveru



1098
1099
1100
1101
1102
1103
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/node.rb', line 1098

def node_from_backtrace(backtrace)
  filename, lineno = backtrace[0].split(':', 2)
  regexp = /^\s*class\s+(\S+)/
  File.readlines(filename)[0..lineno.to_i].reverse.find{|ln| ln =~ regexp }
  const_get($1)
end

- (Object) Rewrite(key, value = nil, &block)



110
111
112
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/route.rb', line 110

def Rewrite(key, value = nil, &block)
  Rewrite[key] = value || block
end

- (Object) Route(key, value = nil, &block)



106
107
108
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/route.rb', line 106

def Route(key, value = nil, &block)
  Route[key] = value || block
end

- (Object) sync(&block)

Use this method to achieve thread-safety for sensitive operations.

This should be of most use when manipulating files to prevent other threads from doing the same, no other code will be scheduled during execution of this method.

Parameters:

  • block (Proc)

    the things you want to execute



14
15
16
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/state.rb', line 14

def sync(&block)
  SEMAPHORE.synchronize(&block)
end

- (Object) to(object)

Returns one of the paths the given +object+ is mapped to.

Examples:


class Hello
  include Innate::Node
  map '/'
end

Innate.to(Hello) # => '/'


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

def to(object)
  DynaMap.to(object)
end