Class: Rack::Unpoly::Inspector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rack/unpoly/inspector.rb

Overview

Easily inspect the Unpoly environment of the current request. Inspectors are not normally instantiated by users, but accessed through env or one of the convenience wrappers for Roda and Sinatra.

Direct Known Subclasses

Sinatra::Unpoly::SinatraInspector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Inspector

:nodoc:



17
18
19
# File 'lib/rack/unpoly/inspector.rb', line 17

def initialize(request) # :nodoc:
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

:nodoc:



15
16
17
# File 'lib/rack/unpoly/inspector.rb', line 15

def request
  @request
end

Instance Method Details

#any_target?(tested_target) ⇒ Boolean

Determine if the provided target is the current target for a successful or failed request.

Returns:

  • (Boolean)


50
51
52
# File 'lib/rack/unpoly/inspector.rb', line 50

def any_target?(tested_target)
  target?(tested_target) || fail_target?(tested_target)
end

#fail_targetObject

The CSS selector for the fragment Unpoly will update if the request fails. Requires Unpoly >= 0.50



39
40
41
# File 'lib/rack/unpoly/inspector.rb', line 39

def fail_target
  get_header("HTTP_X_UP_FAIL_TARGET")
end

#fail_target?(tested_target) ⇒ Boolean

Determine if the provided target is the current target for a failed request. Requires Unpoly >= 0.50

Returns:

  • (Boolean)


45
46
47
# File 'lib/rack/unpoly/inspector.rb', line 45

def fail_target?(tested_target)
  query_target(fail_target, tested_target)
end

#query_target(actual_target, tested_target) ⇒ Object

:nodoc:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rack/unpoly/inspector.rb', line 70

def query_target(actual_target, tested_target) # :nodoc:
  if up?
    if actual_target == tested_target
      true
    elsif actual_target == "html"
      true
    elsif actual_target == "body"
      !%w(head title meta).include?(tested_target)
    else
      false
    end
  else
    true
  end
end

#set_title(response, new_title) ⇒ Object

Set the page title.



55
56
57
# File 'lib/rack/unpoly/inspector.rb', line 55

def set_title(response, new_title)
  response.headers["X-Up-Title"] = new_title
end

#targetObject

The actual target as requested by Unpoly.



33
34
35
# File 'lib/rack/unpoly/inspector.rb', line 33

def target
  get_header("HTTP_X_UP_TARGET")
end

#target?(tested_target) ⇒ Boolean

Identify if the tested_target will match the actual target requested.

Returns:

  • (Boolean)


28
29
30
# File 'lib/rack/unpoly/inspector.rb', line 28

def target?(tested_target)
  query_target(target, tested_target)
end

#unpoly?Boolean Also known as: up?

Determine if this is an Unpoly request.

Returns:

  • (Boolean)


22
23
24
# File 'lib/rack/unpoly/inspector.rb', line 22

def unpoly?
  target.to_s.strip != ""
end

#validate?Boolean

Determine if this is a validate request.

Returns:

  • (Boolean)


60
61
62
# File 'lib/rack/unpoly/inspector.rb', line 60

def validate?
  validate_name.to_s.strip != ""
end

#validate_nameObject

The name attribute of the form field that triggered the validation.



66
67
68
# File 'lib/rack/unpoly/inspector.rb', line 66

def validate_name
  get_header("HTTP_X_UP_VALIDATE")
end