Module: Lunetas::Candy::Initialization::ClassMethods

Defined in:
lib/lunetas/candy/initialization.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lunetas_content_typeObject (readonly)

Holds the Content Type for this request.



40
41
42
# File 'lib/lunetas/candy/initialization.rb', line 40

def lunetas_content_type
  @lunetas_content_type
end

#lunetas_regexObject (readonly)

Holds the Regular Expression for this class.



42
43
44
# File 'lib/lunetas/candy/initialization.rb', line 42

def lunetas_regex
  @lunetas_regex
end

#lunetas_url_paramsObject (readonly)

Holds the URL params, this will be created as instance variables when the class is initialized.



38
39
40
# File 'lib/lunetas/candy/initialization.rb', line 38

def lunetas_url_params
  @lunetas_url_params
end

Instance Method Details

#matches(regex, *url_params) ⇒ Object

Registers a new regular expression. Will add it to the Lunetas::Bag. Will convert the regex to a Regular Expression, if passing a String. It also receives the instance variables for the matches of the regex.

Examples:

Regular expression as a Regexp

matches /\/test\/\w+/

Regular expression as a double quoted string

matches "\\/test\\/\\w+"

Regular expression as a single quoted string (my favorite)

matches '/test/\w+'

Passing instance variables that will be set for the captures

matches '/test/(\d+)\.(\w+)', :id, :format

Parameters:

  • regex (String, Regexp)

    the regular expression for this resource.

  • url_params (Array<Symbol, String>)

    the instance variables that will be set for the captures from the regex.



58
59
60
61
62
63
64
65
66
# File 'lib/lunetas/candy/initialization.rb', line 58

def matches(regex, *url_params)
  @lunetas_content_type = 'text/html'
  @lunetas_regex = regex
  unless Regexp === lunetas_regex
    @lunetas_regex = Regexp.new(regex)
  end
  @lunetas_url_params = url_params
  Lunetas::Bag.register(lunetas_regex, self)
end

#set_content_type(content_type) ⇒ Object

Sets the Content Type for this URL. Defaults to text/html.

Parameters:

  • content_type (String)

    the ContentType for the response.



70
71
72
# File 'lib/lunetas/candy/initialization.rb', line 70

def set_content_type(content_type)
  @lunetas_content_type = content_type
end