Class: Betterdocs::Dsl::Controller::Action::Response
- Inherits:
-
Object
- Object
- Betterdocs::Dsl::Controller::Action::Response
show all
- Extended by:
- Tins::DSLAccessor
- Includes:
- Betterdocs::Dsl::Common
- Defined in:
- lib/betterdocs/dsl/controller/action/response.rb
Defined Under Namespace
Classes: Error
Instance Method Summary
collapse
#set_context
Constructor Details
#initialize(name = :default, &block) ⇒ Response
Returns a new instance of Response.
7
8
9
10
|
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 7
def initialize(name = :default, &block)
@name = name.to_sym
@data_block = block || proc {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Betterdocs::Dsl::Common
Instance Method Details
#data ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 18
def data
@data ||= instance_eval(&@data_block)
@data.is_a? Betterdocs::Responding or
raise Error, "result #{@data.class} is not Betterdocs::Responding"
hash = @data.to_hash
if hash.key?(:data) && hash[:data].empty?
raise Error, "result data for action #{controller}##{action} is empty"
elsif hash.empty?
raise Error, "result for action #{controller}##{action} is empty"
end
@data
rescue => e
error = e.is_a?(Error) ? e : Error.new("#{e.class}: #{e}")
error.set_backtrace e.backtrace.grep(/^#{Betterdocs.rails.root}/)
raise error
end
|
#links ⇒ Object
39
40
41
|
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 39
def links
representer.full? { |r| r.docs.nested_links } || []
end
|
#params ⇒ Object
14
15
16
|
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 14
def params
-> name { param(name).full?(:value) }
end
|
#properties ⇒ Object
35
36
37
|
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 35
def properties
representer.full? { |r| r.docs.nested_properties } || []
end
|
#representer ⇒ Object
43
44
45
46
|
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 43
def representer
data or return
data.ask_and_send(:representer) || inherited_representer(data)
end
|
#to_json(*a) ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 55
def to_json(*a)
data.nil? and raise Error, "result for action #{controller}##{action} was nil"
unless data.is_a?(Betterdocs::Responding)
infobar.puts "#{data.class}".red
infobar.puts "Result of type #{data.class} for action #{controller}##{action} is not hash / Betterdocs::Representer"
end
my_data = data.ask_and_send(:to_hash) || data
data.to_json(*a)
end
|