Class: RailsEdgeTest::Dsl::Edge

Inherits:
Struct
  • Object
show all
Defined in:
lib/rails_edge_test/dsl/edge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Edge

Returns a new instance of Edge.



8
9
10
11
# File 'lib/rails_edge_test/dsl/edge.rb', line 8

def initialize(*args)
  super
  @let_cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object (private)

support calling methods defined in action



110
111
112
113
114
115
116
# File 'lib/rails_edge_test/dsl/edge.rb', line 110

def method_missing(method_name, *arguments, &block)
  if action.respond_to?(method_name)
    action.public_send(method_name, *arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action

Returns:

  • (Object)

    the current value of action



6
7
8
# File 'lib/rails_edge_test/dsl/edge.rb', line 6

def action
  @action
end

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



6
7
8
# File 'lib/rails_edge_test/dsl/edge.rb', line 6

def description
  @description
end

Instance Method Details

#controllerObject

In the context of the edge, we want ‘controller` to be the rails controller instead of our own RailsEdgeTest::Dsl::Controller. In this way the user can directly access the rails controller within their edge.



23
24
25
# File 'lib/rails_edge_test/dsl/edge.rb', line 23

def controller
  @controller ||= controller_class.new
end

#perform_get(parameters = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_edge_test/dsl/edge.rb', line 31

def perform_get(parameters = {})
  request.assign_parameters(
    ::Rails.application.routes,
    controller_class.controller_path,
    action.name.to_s,
    parameters.stringify_keys!,
    '',
    ''
  )

  response = ActionDispatch::Response.new.tap do |res|
    res.request = request
  end

  @response = controller.dispatch(action.name, request, response)
end

#produce_elm_file(module_name, ivar: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rails_edge_test/dsl/edge.rb', line 48

def produce_elm_file(module_name, ivar: nil)
  json = produce_json(ivar: ivar)
  json = json.gsub("\\", "\\\\\\\\") # unbelievably, this replaces \ with \\

  filepath = File.join(
    RailsEdgeTest.configuration.elm_path,
    'Edge',
    controller_class.name.gsub('::','/')
  )

  full_module_name =
    "#{controller_class.name.gsub('::','.')}.#{module_name}"

  data = <<~ELM
    module Edge.#{full_module_name} exposing (json)


    json : String
    json =
        """
    #{json}
        """
  ELM

  write_file(filepath, module_name+'.elm', data)
end

#requestObject



16
17
18
# File 'lib/rails_edge_test/dsl/edge.rb', line 16

def request
  @request ||= ActionController::TestRequest.create(controller_class)
end

#responseObject



27
28
29
# File 'lib/rails_edge_test/dsl/edge.rb', line 27

def response
  @response
end