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

#initialize(tag = nil, label = nil, primary = nil, secondary = nil) ⇒ Definition

Creates a new 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

#labelString



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
# 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)
    matcher = self.send(type || :primary) rescue nil

    if matcher.is_a?(::Regexp) then
      matcher.match(args[1])
    elsif matcher.respond_to?(:call) then
      matcher.call(*args)
    elsif matcher
      args[1] == matcher ? args[1] : nil
    else
      nil
    end
  end
end

#primaryString|Symbol|Proc



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
# 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)
    matcher = self.send(type || :primary) rescue nil

    if matcher.is_a?(::Regexp) then
      matcher.match(args[1])
    elsif matcher.respond_to?(:call) then
      matcher.call(*args)
    elsif matcher
      args[1] == matcher ? args[1] : nil
    else
      nil
    end
  end
end

#secondaryString|Symbol|Proc



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
# 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)
    matcher = self.send(type || :primary) rescue nil

    if matcher.is_a?(::Regexp) then
      matcher.match(args[1])
    elsif matcher.respond_to?(:call) then
      matcher.call(*args)
    elsif matcher
      args[1] == matcher ? args[1] : nil
    else
      nil
    end
  end
end

#tagString|Symbol



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
# 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)
    matcher = self.send(type || :primary) rescue nil

    if matcher.is_a?(::Regexp) then
      matcher.match(args[1])
    elsif matcher.respond_to?(:call) then
      matcher.call(*args)
    elsif matcher
      args[1] == matcher ? args[1] : nil
    else
      nil
    end
  end
end

Instance Method Details

#match(type, *args) ⇒ Object|NilClass

Performs a match of this definition.



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

def match(type, *args)
  matcher = self.send(type || :primary) rescue nil

  if matcher.is_a?(::Regexp) then
    matcher.match(args[1])
  elsif matcher.respond_to?(:call) then
    matcher.call(*args)
  elsif matcher
    args[1] == matcher ? args[1] : nil
  else
    nil
  end
end