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



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

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/leadlight/tint_helper.rb', line 58

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



45
46
47
# File 'lib/leadlight/tint_helper.rb', line 45

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

#match_content_type(pattern) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/leadlight/tint_helper.rb', line 35

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
# File 'lib/leadlight/tint_helper.rb', line 24

def match_path(pattern)
  match{ pattern === __location__.path }
end

#match_status(*patterns) ⇒ Object



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

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

#match_template(path_template) ⇒ Object



28
29
30
31
32
33
# File 'lib/leadlight/tint_helper.rb', line 28

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 { template.match(__location__) }
end