Class: InstantApi::Controller::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/instant_api/controller/parameters.rb

Constant Summary collapse

INTERNAL_PARAMS =
%w(controller action format _method only_path)
RAILS_METHODS =
%i(new edit)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, request_path) ⇒ Parameters

params is the rails params “controller”=>“countries”, “id” => 3 request: /users/2/addresses/3/edit



10
11
12
13
# File 'lib/instant_api/controller/parameters.rb', line 10

def initialize(params, request_path)
  @params = params.except(*INTERNAL_PARAMS)
  @request_path = request_path
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/instant_api/controller/parameters.rb', line 6

def params
  @params
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



6
7
8
# File 'lib/instant_api/controller/parameters.rb', line 6

def request_path
  @request_path
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/instant_api/controller/parameters.rb', line 15

def [](key)
  params[key]
end

#resourcesObject

‘/users/2/addresses/3/edit’ -> [:users, :addresses]



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/instant_api/controller/parameters.rb', line 24

def resources
  @resources ||= begin
    resources = request_path.strip.
                             split('/').
                             compact.
                             map(&:strip).
                             each_with_index.
                             select { |_, index| index % 2 == 1 }.
                             map { |a, _| a.to_sym }

    *rest, tail = *resources
    if RAILS_METHODS.index(tail)
      rest
    else
      resources
    end
  end
end

#select(&block) ⇒ Object



19
20
21
# File 'lib/instant_api/controller/parameters.rb', line 19

def select(&block)
  params.select(&block)
end