Module: Renee::Core::URLGeneration

Included in:
ClassMethods
Defined in:
lib/renee_core/url_generation.rb

Overview

URL generator for creating paths and URLs within your application.

Instance Method Summary collapse

Instance Method Details

#path(name, *args) ⇒ Object

Generates a path for a given name.

Parameters:

  • name (Symbol)

    The name of the path

  • args (Object)

    The values used to generate the path. Can be named with using :name => "value" or supplied in the order for which the variables were decalared in #register.

See Also:



53
54
55
56
# File 'lib/renee_core/url_generation.rb', line 53

def path(name, *args)
  generator = url_generators[name]
  generator ? generator.path(*args) : raise("Generator for #{name} doesn't exist")
end

#prefix(prefix, defaults = nil, &blk) ⇒ Object

Allows the creation of generation contexts.

Examples:

renee.prefix("/prefix") {
  renee.register(:prefix_path, "/path") # would register /prefix/path
}

Parameters:

  • prefix (String)

    The prefix to add to subsequent calls to #register.

  • defaults (Hash, nil) (defaults to: nil)

    The defaults to add to subsequent calls to #register.

See Also:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/renee_core/url_generation.rb', line 30

def prefix(prefix, defaults = nil, &blk)
  generator = self
  subgenerator = Class.new {
    include URLGeneration
    define_method(:url_generators) { generator.send(:url_generators) }
  }.new
  subgenerator.instance_variable_set(:@generation_prefix, "#{@generation_prefix}#{prefix}")
  subgenerator.instance_variable_set(:@generation_defaults, defaults_for_generation(defaults))
  if block_given?
    old_prefix, old_defaults = @generation_prefix, @generation_defaults
    @generation_prefix, @generation_defaults = "#{@generation_prefix}#{prefix}", defaults_for_generation(defaults)
    subgenerator.instance_eval(&blk)
    @generation_prefix, @generation_defaults = old_prefix, old_defaults
  end
  subgenerator
end

#register(name, pattern, defaults = nil) ⇒ Object

Registers new paths for generation.

Examples:

renee.register(:path, "/my/:var/path")
renee.path(:path, 123) # => "/my/123/path"
renee.path(:path, :var => 'hey you') # => "/my/hey%20you/path"

Parameters:

  • name (Symbol)

    The name of the path

  • pattern (String)

    The pattern used for generation.

  • defaults (Hash, nil) (defaults to: nil)

    Any default values used for generation.



17
18
19
# File 'lib/renee_core/url_generation.rb', line 17

def register(name, pattern, defaults = nil)
  url_generators[name] = Generator.new("#{@generation_prefix}#{pattern}", defaults_for_generation(defaults))
end

#url(name, *args) ⇒ Object

Generates a url for a given name.

Parameters:

  • name (Symbol)

    The name of the path

  • args (Object)

    The values used to generate the path. Can be named with using :name => "value" or supplied in the order for which the variables were decalared in #register.

See Also:



61
62
63
64
# File 'lib/renee_core/url_generation.rb', line 61

def url(name, *args)
  generator = url_generators[name]
  generator ? generator.url(*args) : raise("Generator for #{name} doesn't exist")
end