Class: Brauser::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/brauser/definition.rb

Overview

A class which hold a definition of a browser, a platform or a language This class represents a detection of the current user browser.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Definition) initialize(tag = nil, label = nil, primary = nil, secondary = nil)

Creates a new definition.

Parameters:

  • tag (String|Symbol) (defaults to: nil)

    An identifier for this definition.

  • label (String) (defaults to: nil)

    A human readable label for this definition.

  • primary (String|Symbol|Proc) (defaults to: nil)

    The primary matcher of this definition.

  • secondary (String|Symbol|Proc) (defaults to: nil)

    The secondary matcher of this definition.



28
29
30
31
32
33
# File 'lib/brauser/definition.rb', line 28

def initialize(tag = nil, label = nil, primary = nil, secondary = nil)
  self.tag = tag if tag
  self.label = label if label
  self.primary = primary if primary
  self.secondary = secondary if secondary
end

Instance Attribute Details

- (String) label

Returns A human readable label for this definition.

Returns:

  • (String)

    A human readable label for this definition.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brauser/definition.rb', line 19

class Definition
  attr_accessor :tag, :label, :primary, :secondary

  # Creates a new definition.
  #
  # @param tag [String|Symbol] An identifier for this definition.
  # @param label [String] A human readable label for this definition.
  # @param primary [String|Symbol|Proc] The primary matcher of this definition.
  # @param secondary [String|Symbol|Proc] The secondary matcher of this definition.
  def initialize(tag = nil, label = nil, primary = nil, secondary = nil)
    self.tag = tag if tag
    self.label = label if label
    self.primary = primary if primary
    self.secondary = secondary if secondary
  end

  # Performs a match of this definition.
  #
  # @param type [Symbol] The matcher to perform. Can be `:primary` (default) or `:secondary`.
  # @param args [Array] Arguments to pass to the matcher. The first is the definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def match(type, *args)
    begin
      matcher = send(type || :primary)
    rescue NoMethodError
      nil
    end

    perform_match(matcher, args[1], args)
  end

  private

  # Recognizes a browser disambiguating against another.
  #
  # @param agent [String] The agent to match.
  # @param positive_matcher [Regexp] The expression to match.
  # @param negative_matcher [Regexp] The expression NOT to match.
  # @return [Boolean] `true` if matching succeeded, `false otherwise`.
  def self.disambiguate_browser(agent, positive_matcher, negative_matcher)
    agent =~ positive_matcher && agent !~ negative_matcher
  end

  # Performs a match against a target.
  #
  # @param matcher [Object] The matcher to run, can be a `Regexp`, a `Proc` or a string.
  # @param target [String] The string to match.
  # @param args [Array] Arguments to pass to the matcher. The first is definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def perform_match(matcher, target, args)
    if matcher.is_a?(::Regexp)
      matcher.match(target)
    elsif matcher.respond_to?(:call)
      matcher.call(*args)
    elsif target == matcher
      target
    else
      nil
    end
  end
end

- (String|Symbol|Proc) primary

Returns The primary matcher of this definition. Used to match browser engine, platform name and language.

Returns:

  • (String|Symbol|Proc)

    The primary matcher of this definition. Used to match browser engine, platform name and language.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brauser/definition.rb', line 19

class Definition
  attr_accessor :tag, :label, :primary, :secondary

  # Creates a new definition.
  #
  # @param tag [String|Symbol] An identifier for this definition.
  # @param label [String] A human readable label for this definition.
  # @param primary [String|Symbol|Proc] The primary matcher of this definition.
  # @param secondary [String|Symbol|Proc] The secondary matcher of this definition.
  def initialize(tag = nil, label = nil, primary = nil, secondary = nil)
    self.tag = tag if tag
    self.label = label if label
    self.primary = primary if primary
    self.secondary = secondary if secondary
  end

  # Performs a match of this definition.
  #
  # @param type [Symbol] The matcher to perform. Can be `:primary` (default) or `:secondary`.
  # @param args [Array] Arguments to pass to the matcher. The first is the definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def match(type, *args)
    begin
      matcher = send(type || :primary)
    rescue NoMethodError
      nil
    end

    perform_match(matcher, args[1], args)
  end

  private

  # Recognizes a browser disambiguating against another.
  #
  # @param agent [String] The agent to match.
  # @param positive_matcher [Regexp] The expression to match.
  # @param negative_matcher [Regexp] The expression NOT to match.
  # @return [Boolean] `true` if matching succeeded, `false otherwise`.
  def self.disambiguate_browser(agent, positive_matcher, negative_matcher)
    agent =~ positive_matcher && agent !~ negative_matcher
  end

  # Performs a match against a target.
  #
  # @param matcher [Object] The matcher to run, can be a `Regexp`, a `Proc` or a string.
  # @param target [String] The string to match.
  # @param args [Array] Arguments to pass to the matcher. The first is definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def perform_match(matcher, target, args)
    if matcher.is_a?(::Regexp)
      matcher.match(target)
    elsif matcher.respond_to?(:call)
      matcher.call(*args)
    elsif target == matcher
      target
    else
      nil
    end
  end
end

- (String|Symbol|Proc) secondary

Returns The secondary matcher of this definition. Used to match browser version.

Returns:

  • (String|Symbol|Proc)

    The secondary matcher of this definition. Used to match browser version.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brauser/definition.rb', line 19

class Definition
  attr_accessor :tag, :label, :primary, :secondary

  # Creates a new definition.
  #
  # @param tag [String|Symbol] An identifier for this definition.
  # @param label [String] A human readable label for this definition.
  # @param primary [String|Symbol|Proc] The primary matcher of this definition.
  # @param secondary [String|Symbol|Proc] The secondary matcher of this definition.
  def initialize(tag = nil, label = nil, primary = nil, secondary = nil)
    self.tag = tag if tag
    self.label = label if label
    self.primary = primary if primary
    self.secondary = secondary if secondary
  end

  # Performs a match of this definition.
  #
  # @param type [Symbol] The matcher to perform. Can be `:primary` (default) or `:secondary`.
  # @param args [Array] Arguments to pass to the matcher. The first is the definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def match(type, *args)
    begin
      matcher = send(type || :primary)
    rescue NoMethodError
      nil
    end

    perform_match(matcher, args[1], args)
  end

  private

  # Recognizes a browser disambiguating against another.
  #
  # @param agent [String] The agent to match.
  # @param positive_matcher [Regexp] The expression to match.
  # @param negative_matcher [Regexp] The expression NOT to match.
  # @return [Boolean] `true` if matching succeeded, `false otherwise`.
  def self.disambiguate_browser(agent, positive_matcher, negative_matcher)
    agent =~ positive_matcher && agent !~ negative_matcher
  end

  # Performs a match against a target.
  #
  # @param matcher [Object] The matcher to run, can be a `Regexp`, a `Proc` or a string.
  # @param target [String] The string to match.
  # @param args [Array] Arguments to pass to the matcher. The first is definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def perform_match(matcher, target, args)
    if matcher.is_a?(::Regexp)
      matcher.match(target)
    elsif matcher.respond_to?(:call)
      matcher.call(*args)
    elsif target == matcher
      target
    else
      nil
    end
  end
end

- (String|Symbol) tag

Returns An identifier for this definition.

Returns:

  • (String|Symbol)

    An identifier for this definition.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brauser/definition.rb', line 19

class Definition
  attr_accessor :tag, :label, :primary, :secondary

  # Creates a new definition.
  #
  # @param tag [String|Symbol] An identifier for this definition.
  # @param label [String] A human readable label for this definition.
  # @param primary [String|Symbol|Proc] The primary matcher of this definition.
  # @param secondary [String|Symbol|Proc] The secondary matcher of this definition.
  def initialize(tag = nil, label = nil, primary = nil, secondary = nil)
    self.tag = tag if tag
    self.label = label if label
    self.primary = primary if primary
    self.secondary = secondary if secondary
  end

  # Performs a match of this definition.
  #
  # @param type [Symbol] The matcher to perform. Can be `:primary` (default) or `:secondary`.
  # @param args [Array] Arguments to pass to the matcher. The first is the definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def match(type, *args)
    begin
      matcher = send(type || :primary)
    rescue NoMethodError
      nil
    end

    perform_match(matcher, args[1], args)
  end

  private

  # Recognizes a browser disambiguating against another.
  #
  # @param agent [String] The agent to match.
  # @param positive_matcher [Regexp] The expression to match.
  # @param negative_matcher [Regexp] The expression NOT to match.
  # @return [Boolean] `true` if matching succeeded, `false otherwise`.
  def self.disambiguate_browser(agent, positive_matcher, negative_matcher)
    agent =~ positive_matcher && agent !~ negative_matcher
  end

  # Performs a match against a target.
  #
  # @param matcher [Object] The matcher to run, can be a `Regexp`, a `Proc` or a string.
  # @param target [String] The string to match.
  # @param args [Array] Arguments to pass to the matcher. The first is definition itself.
  # @return [Object|NilClass] A match if matcher succeeded, `nil` otherwise.
  def perform_match(matcher, target, args)
    if matcher.is_a?(::Regexp)
      matcher.match(target)
    elsif matcher.respond_to?(:call)
      matcher.call(*args)
    elsif target == matcher
      target
    else
      nil
    end
  end
end

Instance Method Details

- (Object|NilClass) match(type, *args)

Performs a match of this definition.

Parameters:

  • type (Symbol)

    The matcher to perform. Can be :primary (default) or :secondary.

  • args (Array)

    Arguments to pass to the matcher. The first is the definition itself.

Returns:

  • (Object|NilClass)

    A match if matcher succeeded, nil otherwise.



40
41
42
43
44
45
46
47
48
# File 'lib/brauser/definition.rb', line 40

def match(type, *args)
  begin
    matcher = send(type || :primary)
  rescue NoMethodError
    nil
  end

  perform_match(matcher, args[1], args)
end