Class: Brauser::Definitions::Browser
- Defined in:
- lib/brauser/definitions/browser.rb
Overview
A definition of a platform.
Instance Attribute Summary collapse
-
#engine_matcher ⇒ Regexp|Proc
readonly
The pattern or the block to recognize the engine.
-
#name ⇒ String
readonly
The platform name.
-
#version_matcher ⇒ Regexp|Proc
readonly
The pattern or the block to recognize the version.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(id, name, engine_matcher, version_matcher, **_) ⇒ Browser
constructor
Creates a new definition.
-
#match(header) ⇒ Array|NilClass
Matches against an header.
Constructor Details
#initialize(id, name, engine_matcher, version_matcher, **_) ⇒ Browser
Creates a new definition.
25 26 27 28 29 30 |
# File 'lib/brauser/definitions/browser.rb', line 25 def initialize(id, name, engine_matcher, version_matcher, **_) @id = id @name = name @engine_matcher = engine_matcher @version_matcher = version_matcher end |
Instance Attribute Details
#engine_matcher ⇒ Regexp|Proc (readonly)
Returns The pattern or the block to recognize the engine.
16 17 18 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 |
# File 'lib/brauser/definitions/browser.rb', line 16 class Browser < Base attr_reader :name, :engine_matcher, :version_matcher # Creates a new definition. # # @param id [Symbol] The platform id. # @param name [String] The platform name. # @param engine_matcher [Regexp|Proc] The pattern or the block to recognize the engine. # @param version_matcher [Regexp|Proc] The pattern or the block to recognize the version. def initialize(id, name, engine_matcher, version_matcher, **_) @id = id @name = name @engine_matcher = engine_matcher @version_matcher = version_matcher end # Matches against an header. # # @param header [String] The header to match # @return [Array|NilClass] An array with the engine and the version if match succeeded, `false` or `nil` otherwise. def match(header) # First of all, match the engine engine = perform_match(@engine_matcher, header) ? @id : nil if engine version = extract_version(perform_match(@version_matcher, header)) platform = extract_platform(header, engine) [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)] else nil end end private # :nodoc: def perform_match(pattern, subject) method = pattern.is_a?(Regexp) ? :match : :call pattern.send(method, subject) end # :nodoc: # @private def extract_version(version) # Adjust version version = "0.0" if version.blank? version = version.to_a.last if version.is_a?(::MatchData) Versionomy.parse(version) rescue version end # :nodoc: def extract_platform(header, engine) catch(:result) { Brauser::Definitions.platforms.each do |platform, definition| throw(:result, platform) if definition.match(header, engine) end nil } end end |
#name ⇒ String (readonly)
Returns The platform name.
16 17 18 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 |
# File 'lib/brauser/definitions/browser.rb', line 16 class Browser < Base attr_reader :name, :engine_matcher, :version_matcher # Creates a new definition. # # @param id [Symbol] The platform id. # @param name [String] The platform name. # @param engine_matcher [Regexp|Proc] The pattern or the block to recognize the engine. # @param version_matcher [Regexp|Proc] The pattern or the block to recognize the version. def initialize(id, name, engine_matcher, version_matcher, **_) @id = id @name = name @engine_matcher = engine_matcher @version_matcher = version_matcher end # Matches against an header. # # @param header [String] The header to match # @return [Array|NilClass] An array with the engine and the version if match succeeded, `false` or `nil` otherwise. def match(header) # First of all, match the engine engine = perform_match(@engine_matcher, header) ? @id : nil if engine version = extract_version(perform_match(@version_matcher, header)) platform = extract_platform(header, engine) [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)] else nil end end private # :nodoc: def perform_match(pattern, subject) method = pattern.is_a?(Regexp) ? :match : :call pattern.send(method, subject) end # :nodoc: # @private def extract_version(version) # Adjust version version = "0.0" if version.blank? version = version.to_a.last if version.is_a?(::MatchData) Versionomy.parse(version) rescue version end # :nodoc: def extract_platform(header, engine) catch(:result) { Brauser::Definitions.platforms.each do |platform, definition| throw(:result, platform) if definition.match(header, engine) end nil } end end |
#version_matcher ⇒ Regexp|Proc (readonly)
Returns The pattern or the block to recognize the version.
16 17 18 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 |
# File 'lib/brauser/definitions/browser.rb', line 16 class Browser < Base attr_reader :name, :engine_matcher, :version_matcher # Creates a new definition. # # @param id [Symbol] The platform id. # @param name [String] The platform name. # @param engine_matcher [Regexp|Proc] The pattern or the block to recognize the engine. # @param version_matcher [Regexp|Proc] The pattern or the block to recognize the version. def initialize(id, name, engine_matcher, version_matcher, **_) @id = id @name = name @engine_matcher = engine_matcher @version_matcher = version_matcher end # Matches against an header. # # @param header [String] The header to match # @return [Array|NilClass] An array with the engine and the version if match succeeded, `false` or `nil` otherwise. def match(header) # First of all, match the engine engine = perform_match(@engine_matcher, header) ? @id : nil if engine version = extract_version(perform_match(@version_matcher, header)) platform = extract_platform(header, engine) [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)] else nil end end private # :nodoc: def perform_match(pattern, subject) method = pattern.is_a?(Regexp) ? :match : :call pattern.send(method, subject) end # :nodoc: # @private def extract_version(version) # Adjust version version = "0.0" if version.blank? version = version.to_a.last if version.is_a?(::MatchData) Versionomy.parse(version) rescue version end # :nodoc: def extract_platform(header, engine) catch(:result) { Brauser::Definitions.platforms.each do |platform, definition| throw(:result, platform) if definition.match(header, engine) end nil } end end |
Instance Method Details
#match(header) ⇒ Array|NilClass
Matches against an header.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/brauser/definitions/browser.rb', line 36 def match(header) # First of all, match the engine engine = perform_match(@engine_matcher, header) ? @id : nil if engine version = extract_version(perform_match(@version_matcher, header)) platform = extract_platform(header, engine) [Brauser::Value.new(engine), Brauser::Value.new(version), Brauser::Value.new(platform)] else nil end end |