Class: Miron::Mironfile

Inherits:
Object
  • Object
show all
Defined in:
lib/miron/mironfile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mironfile_path) ⇒ Mironfile

Returns a new instance of Mironfile


36
37
38
39
# File 'lib/miron/mironfile.rb', line 36

def initialize(mironfile_path)
  @mironfile_path = mironfile_path
  @middleware = []
end

Instance Attribute Details

#appMiron::Mironfile, Nil (readonly)

Returns the contents of the Mironfile in the given dir, if any exists.

Parameters:

  • dir (Pathname)

    The directory where to look for the Mironfile.

Returns:


11
12
13
# File 'lib/miron/mironfile.rb', line 11

def app
  @app
end

#middlewareMiron::Mironfile, Nil (readonly)

Returns the contents of the Mironfile in the given dir, if any exists.

Parameters:

  • dir (Pathname)

    The directory where to look for the Mironfile.

Returns:


11
12
13
# File 'lib/miron/mironfile.rb', line 11

def middleware
  @middleware
end

Class Method Details

.from_dir(dir) ⇒ Object


13
14
15
16
17
18
19
# File 'lib/miron/mironfile.rb', line 13

def self.from_dir(dir)
  path = dir + 'Mironfile.rb'
  return nil unless path && File.exist?(path)
  mironfile = Mironfile.new(path)
  mironfile.evaluate
  mironfile
end

.from_file(path) ⇒ Miron::Mironfile, Nil

Returns the contents of the Mironfile in the given path.

Parameters:

  • path (Pathname)

    The path for the Mironfile.

Returns:


29
30
31
32
33
34
# File 'lib/miron/mironfile.rb', line 29

def self.from_file(path)
  return nil unless path && File.exist?(path)
  mironfile = Mironfile.new(path)
  mironfile.evaluate
  mironfile
end

Instance Method Details

#evaluateObject

Evaluates the Mironfile provided.


132
133
134
# File 'lib/miron/mironfile.rb', line 132

def evaluate
  instance_eval(@mironfile_path.read)
end

#run(app_constant, *args) ⇒ Object

Takes an argument that is an object that responds to #call and returns a Response.

class Heartbeat def self.call(request, response) response.http_status = 200 response.headers = { "Content-Type" => "text/plain" } response.body = "OK" end end

run Heartbeat

If you want to provide parameters besides for request and response to your app, you can do this too! Important: if you are initializing your app, you MUST change th call method to be on the instance of the class, not as a class method. See the below example for more information.

class Heartbeat def initialize(hi) @hi = hi end

def call(request, response)
  response.http_status = 200
  response.headers = { "Content-Type" => "text/plain" }
  response.body = @hi
end

end

run Heartbeat, 'hi'


71
72
73
74
75
76
77
# File 'lib/miron/mironfile.rb', line 71

def run(app_constant, *args)
  if args.empty?
    @app = app_constant
  else
    @app = app_constant.new(args)
  end
end

#use(middleware_constant, *args) ⇒ Object

Takes an argument that is an object that responds to #call and returns a Response.

class Middle def self.call(request, response) puts "hello from middle" end end

class Heartbeat def self.call(request, response) response.http_status = 200 response.headers = { "Content-Type" => "text/plain" } response.body = "OK" end end

use Middle run Heartbeat

If you want to provide parameters besides for request and response to your middleware, you can do this too! Important: if you are initializing your middleware, you MUST change th call method to be on the instance of the class, not as a class method. See the below example for more information.

class Middle def initialize(hi) @hi = hi end

def call(request, response)
  puts @hi
end

end

class Heartbeat def self.call(request, response) response.http_status = 200 response.headers = { "Content-Type" => "text/plain" } response.body = @hi end end

use Middle, 'hi' run Heartbeat


123
124
125
126
127
128
129
# File 'lib/miron/mironfile.rb', line 123

def use(middleware_constant, *args)
  if args.empty?
    @middleware << middleware_constant
  else
    @middleware << middleware_constant.new(args)
  end
end