Class: JsResponder

Inherits:
Object
  • Object
show all
Defined in:
lib/js_responder.rb,
lib/js_responder/version.rb

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJsResponder

Returns a new instance of JsResponder.



6
7
8
# File 'lib/js_responder.rb', line 6

def initialize
  @instructions = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Introduce some flexibility by catching other commands



37
38
39
# File 'lib/js_responder.rb', line 37

def method_missing(method, *args)
  instructions << [[method.to_s], args].flatten!
end

Instance Attribute Details

#instructionsObject

Returns the value of attribute instructions.



4
5
6
# File 'lib/js_responder.rb', line 4

def instructions
  @instructions
end

Instance Method Details

#error(text) ⇒ Object



32
33
34
# File 'lib/js_responder.rb', line 32

def error(text)
  instructions << ['error', text]
end

#log(text) ⇒ Object



24
25
26
# File 'lib/js_responder.rb', line 24

def log(text)
  instructions << ['log', text]
end

#redirect(url) ⇒ Object

Default instruction set



20
21
22
# File 'lib/js_responder.rb', line 20

def redirect(url)
  instructions << ['redirect', url]
end

#success(text) ⇒ Object



28
29
30
# File 'lib/js_responder.rb', line 28

def success(text)
  instructions << ['success', text]
end

#transcode(context = nil, &block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/js_responder.rb', line 10

def transcode(context=nil, &block)
  if block_given?
    instance_exec(context, &block)
  else
    raise 'Expecting a block'
  end
  return instructions
end