Class: RestfulSpec::Specification::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/restful_spec/specification/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, route, controller) ⇒ Action

Returns a new instance of Action.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/restful_spec/specification/action.rb', line 26

def initialize(verb, route, controller)
  @route = route
  @controller = controller

  @name = route.name
  @path = route.path.respond_to?(:spec) ? route.path.spec.to_s : route.path
  @path.gsub!(/\(\.:format\)$/,'')
  @method = verb

  self.accepts_parameters = Set.new
  self.rejects_parameters = Set.new
  self.responds_with = nil
end

Instance Attribute Details

#accepts_parametersObject

Returns the value of attribute accepts_parameters.



3
4
5
# File 'lib/restful_spec/specification/action.rb', line 3

def accepts_parameters
  @accepts_parameters
end

#rejects_parametersObject

Returns the value of attribute rejects_parameters.



3
4
5
# File 'lib/restful_spec/specification/action.rb', line 3

def rejects_parameters
  @rejects_parameters
end

#responds_withObject

Returns the value of attribute responds_with.



3
4
5
# File 'lib/restful_spec/specification/action.rb', line 3

def responds_with
  @responds_with
end

#routeObject (readonly)

Returns the value of attribute route.



4
5
6
# File 'lib/restful_spec/specification/action.rb', line 4

def route
  @route
end

Class Method Details

.from_routes(controller) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/restful_spec/specification/action.rb', line 6

def self.from_routes(controller)
  Rails.application.routes.routes.reduce([]) do |actions, route|
    verbs = if route.verb.is_a?(Regexp)
              array = route.verb.source.split('|')
              array.map { |verb| verb.gsub(/[^a-z]/i, '') }
            elsif route.verb.present?
              Array.wrap(route.verb)
            else
              %w(GET POST PUT DELETE)
            end

    verbs.each do |verb|
      action = new(verb, route, controller)
      actions << action if action.exists?
    end

    actions
  end
end

Instance Method Details

#as_json(*args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/restful_spec/specification/action.rb', line 44

def as_json(*args)
  hash = {}
  hash[:method] = @method
  hash[:path] = @path
  hash[:name] = @name
  hash[:accepts_parameters] = accepts_parameters
  hash[:rejects_parameters] = rejects_parameters
  hash[:responds_with] = responds_with
  hash.as_json(*args)
end

#exists?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/restful_spec/specification/action.rb', line 40

def exists?
  route.defaults[:controller] == @controller.controller_path && @controller.method_defined?(route.defaults[:action])
end

#match?(string) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/restful_spec/specification/action.rb', line 55

def match?(string)
  to_s == string || @method == string || @name == string || @path == string
end

#to_sObject



59
60
61
# File 'lib/restful_spec/specification/action.rb', line 59

def to_s
  [@method, @path].join(' ')
end