Class: Browserino::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/browserino/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(props = {}, like = nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

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



117
118
119
# File 'lib/browserino/client.rb', line 117

def method_missing(*)
  invertable nil
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
# File 'lib/browserino/client.rb', line 72

def ==(other)
  self === other
end

#===(other) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



76
77
78
# File 'lib/browserino/client.rb', line 76

def =~(other)
  self === other
end

#arm?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/browserino/client.rb', line 47

def arm?
  invertable(@arm ||= architecture == :arm)
end

#is?(sym, opt = {}) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# 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) ⇒ Object



90
91
92
93
94
# 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

#likeObject



31
32
33
# File 'lib/browserino/client.rb', line 31

def like
  @like ||= self
end

#like?(sym, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/browserino/client.rb', line 35

def like?(sym, opts = {})
  invertable like.is?(sym, opts)
end

#notObject

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



131
132
133
# File 'lib/browserino/client.rb', line 131

def not
  @not = true && self
end

#not?(sym, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/browserino/client.rb', line 57

def not?(sym, opts = {})
  !is? sym, opts
end

#propertiesObject



25
26
27
28
29
# 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?Boolean

always respond to missing, read method_missing comment

Returns:

  • (Boolean)


122
123
124
# File 'lib/browserino/client.rb', line 122

def respond_to_missing?(*)
  true
end

#to_aObject



111
112
113
# File 'lib/browserino/client.rb', line 111

def to_a
  properties.to_a
end

#to_hObject



107
108
109
# File 'lib/browserino/client.rb', line 107

def to_h
  properties
end

#to_hashObject



103
104
105
# File 'lib/browserino/client.rb', line 103

def to_hash
  properties
end

#to_json(*args) ⇒ Object



84
85
86
87
88
# 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_sObject



96
97
98
99
100
101
# 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_strObject



80
81
82
# File 'lib/browserino/client.rb', line 80

def to_str
  to_s
end

#x32?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/browserino/client.rb', line 43

def x32?
  invertable(@x32 ||= architecture == :x32)
end

#x64?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/browserino/client.rb', line 39

def x64?
  invertable(@x64 ||= architecture == :x64)
end