Class: Wants::MatchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/wants/match_result.rb

Instance Method Summary collapse

Constructor Details

#initialize(accept_header, acceptable) ⇒ MatchResult

Returns a new instance of MatchResult.



6
7
8
9
10
# File 'lib/wants/match_result.rb', line 6

def initialize(accept_header, acceptable)
  @accept     = accept_header || ''
  @acceptable = acceptable.map { |mime| parse_mime(mime) }
  @best_match = MIMEParse.best_match(@acceptable, @accept)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wants/match_result.rb', line 38

def method_missing(method, *args, &block)
  if mime = mime_abbreviation_from_method(method)
    if args.length > 0
      raise ArgumentError, "wrong number of arguments (#{args.length} for 0)"
    end
    if method =~ /\?$/
      self[mime]
    elsif self[mime]
      @response_value ||= block.call
    else
      @response_value
    end
  else
    super
  end
end

Instance Method Details

#[](mime) ⇒ Object



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

def [](mime)
  @best_match == parse_mime(mime)
end

#any(&block) ⇒ Object



28
29
30
31
# File 'lib/wants/match_result.rb', line 28

def any(&block)
  @response_value ||= block.call if present?
  @response_value
end

#blank?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/wants/match_result.rb', line 16

def blank?
  not_acceptable?
end

#not_acceptable(&block) ⇒ Object



33
34
35
36
# File 'lib/wants/match_result.rb', line 33

def not_acceptable(&block)
  @response_value ||= block.call if blank?
  @response_value
end

#not_acceptable?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/wants/match_result.rb', line 12

def not_acceptable?
  @best_match.nil?
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  !blank?
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/wants/match_result.rb', line 55

def respond_to?(method)
  return true if mime_abbreviation_from_method(method)
  super
end