class Browserino::Client

Public Class Methods

new(props = {}, like = nil) click to toggle source
# File lib/browserino/client.rb, line 5
def initialize(props = {}, like = nil)
  @property_names = props.keys
  @like           = like

  # properties are added as methods that will each be defined in a specific
  # order below. First, seperate static value methods from procs,
  # procs will be able to call methods in this instances' context
  # therefore we need to define static methods before procs
  generate_preset_methods! props

  # for each of #name, #engine, #platform and #device use their results as
  # methods names, this will create a method #firefox? for the output
  # of a #name # => :firefox for example.
  # NOTE: labels do not have to be added, they will be extracted
  # and inserted by this method, this method will also add aliasses
  # of all the names as methods.
  generate_result_methods! props, :name, :engine, :platform, :device
  generate_proc_methods! props
end

Public Instance Methods

==(other) click to toggle source
# File lib/browserino/client.rb, line 72
def ==(other)
  self === other
end
===(other) click to toggle source
# File lib/browserino/client.rb, line 61
def ===(other)
  return invertable false unless name

  invertable case other
             when Regexp         then other        =~ name
             when String         then other.to_sym == name
             when Symbol, Client then other        == name
             else false
             end
end
=~(other) click to toggle source
# File lib/browserino/client.rb, line 76
def =~(other)
  self === other
end
arm?() click to toggle source
# File lib/browserino/client.rb, line 47
def arm?
  invertable(@arm ||= architecture == :arm)
end
is?(sym, opt = {}) click to toggle source
# File lib/browserino/client.rb, line 51
def is?(sym, opt = {})
  return invertable send("#{sym}?", opt[:version]) if opt && opt[:version]

  invertable send("#{sym}?")
end
label_or_version_name(prop) click to toggle source
# File lib/browserino/client.rb, line 90
def label_or_version_name(prop)
  ver = version_for prop
  label_for(prop) || ver && [properties[prop].to_s.strip,
                             (ver.major if ver > '0.0.0')].join.strip
end
like() click to toggle source
# File lib/browserino/client.rb, line 31
def like
  @like ||= self
end
like?(sym, opts = {}) click to toggle source
# File lib/browserino/client.rb, line 35
def like?(sym, opts = {})
  invertable like.is?(sym, opts)
end
method_missing(*) click to toggle source

scary, I know, but a falsy value is all we need to return if some property isn't known as any property can be defined on the Client

# File lib/browserino/client.rb, line 117
def method_missing(*)
  invertable nil
end
not() click to toggle source

if you wish for a method to respond to the not method, you'll have to return the result of a function using the `invertable` method as seen in `method_missing` - this will apply the state of the instance variable and invert the state as well as the result if set, otherwise it will just return the value without touching it

# File lib/browserino/client.rb, line 131
def not
  @not = true && self
end
not?(sym, opts = {}) click to toggle source
# File lib/browserino/client.rb, line 57
def not?(sym, opts = {})
  !is? sym, opts
end
properties() click to toggle source
# File lib/browserino/client.rb, line 25
def properties
  @properties ||= @property_names.each_with_object({}) do |prop, result|
    result[prop] = send prop
  end
end
respond_to_missing?(*) click to toggle source

always respond to missing, read method_missing comment

# File lib/browserino/client.rb, line 122
def respond_to_missing?(*)
  true
end
to_a() click to toggle source
# File lib/browserino/client.rb, line 111
def to_a
  properties.to_a
end
to_h() click to toggle source
# File lib/browserino/client.rb, line 107
def to_h
  properties
end
to_hash() click to toggle source
# File lib/browserino/client.rb, line 103
def to_hash
  properties
end
to_json(*args) click to toggle source
# File lib/browserino/client.rb, line 84
def to_json(*args)
  @to_json ||= properties.each_with_object({}) do |(prop, val), hsh|
    hsh[prop] = val.is_a?(Version) && val.full || val
  end.to_json(*args)
end
to_s() click to toggle source
# File lib/browserino/client.rb, line 96
def to_s
  @to_s ||= %i[name engine platform device].each_with_object([]) do |pr, a|
    a << properties[pr].to_s.strip
    a << label_or_version_name(pr)
  end.compact.reject(&:empty?).uniq.join ' '
end
to_str() click to toggle source
# File lib/browserino/client.rb, line 80
def to_str
  to_s
end
x32?() click to toggle source
# File lib/browserino/client.rb, line 43
def x32?
  invertable(@x32 ||= architecture == :x32)
end
x64?() click to toggle source
# File lib/browserino/client.rb, line 39
def x64?
  invertable(@x64 ||= architecture == :x64)
end

Private Instance Methods

create_question!(result, opts = {}) click to toggle source
# File lib/browserino/client.rb, line 193
def create_question!(result, opts = {})
  methods = [result]
  methods += Browserino.config.aliasses[result] if opts[:aliasses]

  methods.each do |mtd|
    define_singleton_method("#{mtd}?") do |val = nil|
      invertable(val ? opts[:version] == val : true)
    end
  end
end
generate_preset_methods!(props) click to toggle source
# File lib/browserino/client.rb, line 159
def generate_preset_methods!(props)
  props.each do |name, value|
    methods = [name, *Browserino.config.aliasses[name]]
    methods.each do |mtd|
      define_singleton_method(mtd) { value }
      define_singleton_method("#{mtd}?") do |val = nil, opts = {}|
        if val && opts.key?(:version)
          value == val && version_for(name) == opts[:version]
        elsif val
          value == val
        else value && true
        end
      end
    end
  end
end
generate_proc_methods!(props) click to toggle source
# File lib/browserino/client.rb, line 176
def generate_proc_methods!(props)
  props.select { |_, val| val.respond_to? :call }.each do |name, value|
    result = instance_eval(&value)
    define_singleton_method(name)       { invertable result }
    define_singleton_method("#{name}?") { invertable result && true }
  end
end
generate_result_methods!(info, *property_names) click to toggle source
# File lib/browserino/client.rb, line 184
def generate_result_methods!(info, *property_names)
  property_names.each do |prop|
    ver_res = version_for prop, info

    create_question! info[prop],            version: ver_res, aliasses: true
    create_question! label_for(prop, info), version: ver_res, aliasses: true
  end
end
invertable(result) click to toggle source
# File lib/browserino/client.rb, line 137
def invertable(result)
  @not ? @not = false || !result : result
end
label_for(sym, from = properties) click to toggle source
# File lib/browserino/client.rb, line 141
def label_for(sym, from = properties)
  return from[:label] if %i[version label name].include? sym

  from["#{sym}_label".to_sym]
end
name_for(sym, from = properties) click to toggle source
# File lib/browserino/client.rb, line 147
def name_for(sym, from = properties)
  return from[:name] if %i[version label name].include? sym

  from["#{sym}_name".to_sym]
end
version_for(sym, from = properties) click to toggle source
# File lib/browserino/client.rb, line 153
def version_for(sym, from = properties)
  return from[:version] if %i[version label name].include? sym

  from["#{sym}_version".to_sym]
end