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.



6
7
8
9
# File 'lib/leadlight/tint_helper.rb', line 6

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

Instance Method Details

#add_header(name, value) ⇒ Object



60
61
62
# File 'lib/leadlight/tint_helper.rb', line 60

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

#exec_tint(&block) ⇒ Object



11
12
13
14
15
16
# File 'lib/leadlight/tint_helper.rb', line 11

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

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



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

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



18
19
20
21
22
# File 'lib/leadlight/tint_helper.rb', line 18

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



51
52
53
# File 'lib/leadlight/tint_helper.rb', line 51

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

#match_content_type(pattern) ⇒ Object



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

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



24
25
26
27
# File 'lib/leadlight/tint_helper.rb', line 24

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

#match_status(*patterns) ⇒ Object



55
56
57
58
# File 'lib/leadlight/tint_helper.rb', line 55

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

#match_template(path_template) ⇒ Object



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

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