Class: Leadlight::TintHelper

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/leadlight/tint_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(representation, tint) ⇒ TintHelper

Returns a new instance of TintHelper.



8
9
10
11
# File 'lib/leadlight/tint_helper.rb', line 8

def initialize(representation, tint)
  @tint = tint
  super(representation)
end

Instance Method Details

#add_header(name, value) ⇒ Object



62
63
64
# File 'lib/leadlight/tint_helper.rb', line 62

def add_header(name, value)
  __response__.env[:response_headers][name] = value
end

#exec_tint(&block) ⇒ Object



13
14
15
16
17
18
# File 'lib/leadlight/tint_helper.rb', line 13

def exec_tint(&block)
  catch(:halt_tint) do
    instance_eval(&block)
  end
  self
end

#extend(mod = nil, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/leadlight/tint_helper.rb', line 66

def extend(mod=nil, &block)
  if mod && block
    raise ArgumentError, 'Module or block, not both'
  end
  if mod
    __getobj__.extend(mod)
  else
    __getobj__.extend(Module.new(&block))
  end
end

#match(*matchers, &block_matcher) ⇒ Object



20
21
22
23
24
# File 'lib/leadlight/tint_helper.rb', line 20

def match(*matchers, &block_matcher)
  matchers << block_matcher if block_matcher
  matched = matchers.any?{|m| m === __getobj__}
  throw :halt_tint unless matched
end

#match_class(klass) ⇒ Object



53
54
55
# File 'lib/leadlight/tint_helper.rb', line 53

def match_class(klass)
  match{ klass === __getobj__}
end

#match_content_type(pattern) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/leadlight/tint_helper.rb', line 43

def match_content_type(pattern)
  content_type = __response__.env[:response_headers]['Content-Type'].to_s
  throw :halt_tint if content_type.empty?
  mimetype = MIME::Type.new(content_type)
  match{
    # Gotta get rid of the type params
    pattern === "#{mimetype.media_type}/#{mimetype.sub_type}"
  }
end

#match_path(pattern) ⇒ Object



26
27
28
29
# File 'lib/leadlight/tint_helper.rb', line 26

def match_path(pattern)
  matcher = path_matcher(pattern)
  match{ matcher.call(pattern, __location__.path, __captures__) }
end

#match_status(*patterns) ⇒ Object



57
58
59
60
# File 'lib/leadlight/tint_helper.rb', line 57

def match_status(*patterns)
  patterns = expand_status_patterns(*patterns)
  match{ patterns.any?{|p| p === __response__.status} }
end

#match_template(path_template) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/leadlight/tint_helper.rb', line 31

def match_template(path_template)
  path_url = Addressable::URI.parse(path_template)
  full_url = __location__ + path_url
  template = Addressable::Template.new(full_url.to_s)
  match {
    match_data = template.match(__location__)
    if match_data
      __captures__.merge!(match_data.mapping)
    end
  }
end