Class: Betterdocs::Dsl::Controller::Action::Response

Inherits:
Object
  • Object
show all
Extended by:
Tins::DSLAccessor
Includes:
Betterdocs::Dsl::Common
Defined in:
lib/betterdocs/dsl/controller/action/response.rb

Instance Method Summary collapse

Methods included from Betterdocs::Dsl::Common

#set_context

Constructor Details

#initialize(name = :default, &block) ⇒ Response

Returns a new instance of Response.



5
6
7
8
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 5

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

#dataObject



16
17
18
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 16

def data
  @data ||= instance_eval(&@data_block)
end


24
25
26
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 24

def links
  representer.full? { |r| r.docs.nested_links }  || []
end

#paramsObject



12
13
14
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 12

def params
  -> name { param(name).full?(:value) }
end

#propertiesObject



20
21
22
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 20

def properties
  representer.full? { |r| r.docs.nested_properties }  || []
end

#representerObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 28

def representer
  if data
    data.ask_and_send(:representer) ||
      data.singleton_class.ancestors.find { |c|
      Betterdocs::ResultRepresenter >= c && c.respond_to?(:docs)
      # Actually it's more like
      #   Betterdocs::ResultRepresenter >= c && !c.singleton_class?
      # in newer rubies.
      # But singleton_class? is broken and private in ruby 2.1.x not
      # existant in <= ruby 2.0.x and finally works in ruby 2.2.x.
      # What a mess!
    }
  end
end

#to_json(*a) ⇒ Object



43
44
45
46
# File 'lib/betterdocs/dsl/controller/action/response.rb', line 43

def to_json(*a)
  my_data = data.ask_and_send(:to_hash) || data
  my_data.to_json(*a)
end