Class: RoadForest::Application::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/application/parameters.rb

Overview

Parameters extracted from a URL, which a interface object can use to identify the resource being discussed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Parameters

Returns a new instance of Parameters.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
11
# File 'lib/roadforest/application/parameters.rb', line 6

def initialize
  @path_info = {}
  @query_params = {}
  @path_tokens = []
  yield self if block_given?
end

Instance Attribute Details

#path_infoObject

Returns the value of attribute path_info.



12
13
14
# File 'lib/roadforest/application/parameters.rb', line 12

def path_info
  @path_info
end

#path_tokensObject

Returns the value of attribute path_tokens.



12
13
14
# File 'lib/roadforest/application/parameters.rb', line 12

def path_tokens
  @path_tokens
end

#query_paramsObject

Returns the value of attribute query_params.



12
13
14
# File 'lib/roadforest/application/parameters.rb', line 12

def query_params
  @query_params
end

Instance Method Details

#[](field_name) ⇒ Object



14
15
16
17
18
# File 'lib/roadforest/application/parameters.rb', line 14

def [](field_name)
  fetch(field_name)
rescue KeyError
  nil
end

#fetch(field_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roadforest/application/parameters.rb', line 20

def fetch(field_name)
  return path_tokens if field_name == '*'
  @path_info.fetch(field_name) do
    if @query_params.respond_to?(:fetch)
      @query_params.fetch(field_name) do
        @query_params.fetch(field_name.to_s)
      end
    else
      raise KeyError, "No parameter: #{field_name}"
    end
  end
end

#remainderObject



39
40
41
# File 'lib/roadforest/application/parameters.rb', line 39

def remainder
  @remainder = @path_tokens.join("/")
end

#slice(*fields) ⇒ Object



33
34
35
36
37
# File 'lib/roadforest/application/parameters.rb', line 33

def slice(*fields)
  fields.each_with_object({}) do |name, hash|
    hash[name] = self[name]
  end
end

#to_hashObject



43
44
45
# File 'lib/roadforest/application/parameters.rb', line 43

def to_hash
  (query_params||{}).merge(path_info||{}).merge('*' => path_tokens)
end