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



132
133
134
135
136
137
138
# File 'lib/rails_edge_test/dsl/edge.rb', line 132

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_delete(parameters = {}) ⇒ Object



41
42
43
44
45
# File 'lib/rails_edge_test/dsl/edge.rb', line 41

def perform_delete(parameters={})
  request.instance_variable_set(:@method, "DELETE")
  request.env['REQUEST_METHOD'] = "DELETE"
  process(parameters)
end

#perform_get(parameters = {}) ⇒ Object



31
32
33
# File 'lib/rails_edge_test/dsl/edge.rb', line 31

def perform_get(parameters={})
  process(parameters)
end

#perform_post(parameters = {}) ⇒ Object



35
36
37
38
39
# File 'lib/rails_edge_test/dsl/edge.rb', line 35

def perform_post(parameters={})
  request.instance_variable_set(:@method, "POST")
  request.env['REQUEST_METHOD'] = "POST"
  process(parameters)
end

#process(parameters = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_edge_test/dsl/edge.rb', line 53

def process(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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rails_edge_test/dsl/edge.rb', line 70

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

#set_authenticity_tokenObject



47
48
49
50
51
# File 'lib/rails_edge_test/dsl/edge.rb', line 47

def set_authenticity_token
  r = request
  controller.instance_eval { @_request = r }
  request.headers['X-CSRF-Token'] = controller.send :form_authenticity_token
end