Class: Brauser::Definition
- Inherits:
-
Object
- Object
- Brauser::Definition
- 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
-
#label ⇒ String
A human readable label for this definition.
-
#primary ⇒ String|Symbol|Proc
The primary matcher of this definition.
-
#secondary ⇒ String|Symbol|Proc
The secondary matcher of this definition.
-
#tag ⇒ String|Symbol
An identifier for this definition.
Class Method Summary collapse
-
.disambiguate_browser(agent, positive_matcher, negative_matcher) ⇒ Boolean
Recognizes a browser disambiguating against another.
Instance Method Summary collapse
-
#initialize(tag = nil, label = nil, primary = nil, secondary = nil) ⇒ Definition
constructor
Creates a new definition.
-
#match(type, *args) ⇒ Object|NilClass
Performs a match of this definition.
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
#label ⇒ String
Returns 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 |
#primary ⇒ String|Symbol|Proc
Returns 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 |
#secondary ⇒ String|Symbol|Proc
Returns 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 |
#tag ⇒ String|Symbol
Returns 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 |
Class Method Details
.disambiguate_browser(agent, positive_matcher, negative_matcher) ⇒ Boolean
Recognizes a browser disambiguating against another.
58 59 60 |
# File 'lib/brauser/definition.rb', line 58 def self.disambiguate_browser(agent, positive_matcher, negative_matcher) agent =~ positive_matcher && agent !~ negative_matcher end |
Instance Method Details
#match(type, *args) ⇒ Object|NilClass
Performs a match of this definition.
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 |