Class: Device

Inherits:
Object
  • Object
show all
Defined in:
lib/peach/device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, device_type:, runtime:, udid: nil) ⇒ Device

Returns a new instance of Device.



6
7
8
9
10
11
# File 'lib/peach/device.rb', line 6

def initialize(name:, device_type:, runtime:, udid: nil)
  @name = name
  @device_type = device_type
  @runtime = runtime
  @udid = udid
end

Instance Attribute Details

#device_typeObject (readonly)

Returns the value of attribute device_type.



4
5
6
# File 'lib/peach/device.rb', line 4

def device_type
  @device_type
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/peach/device.rb', line 4

def name
  @name
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



4
5
6
# File 'lib/peach/device.rb', line 4

def runtime
  @runtime
end

#udidObject (readonly)

Returns the value of attribute udid.



4
5
6
# File 'lib/peach/device.rb', line 4

def udid
  @udid
end

Class Method Details

.current_devices(xc:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/peach/device.rb', line 48

def self.current_devices(xc:)
  devices = Array.new

  simctl_json = JSON.parse(load_devices)
  device_list = simctl_json['devices']

  device_list.each do |type, ds|
    runtime = xc.runtime_name(type)

    ds.each do |d|
      device_name = d["name"]
      device_udid = d["udid"]
      device_type = device_type(udid: device_udid, xc: xc)
      devices << Device.new(name: device_name, device_type: device_type, runtime: runtime, udid: device_udid)
    end
  end

  devices
end

.destroy_all(xc:) ⇒ Object

class functions



42
43
44
45
46
# File 'lib/peach/device.rb', line 42

def self.destroy_all(xc:)
  Device.current_devices(xc: xc).each do |d|
    d.destroy
  end
end

.device_type(udid:, xc:) ⇒ Object



73
74
75
# File 'lib/peach/device.rb', line 73

def self.device_type(udid:, xc:)
  return xc.device_name(device_type_identifier(udid: udid, xc: xc))
end

.device_type_identifier(udid:, xc:) ⇒ Object



68
69
70
71
# File 'lib/peach/device.rb', line 68

def self.device_type_identifier(udid:, xc:)
  plist = Plist.parse_xml("#{ENV['HOME']}/Library/Developer/CoreSimulator/Devices/#{udid}/device.plist")
  plist["deviceType"]
end

.load_devicesObject



77
78
79
# File 'lib/peach/device.rb', line 77

def self.load_devices
  `xcrun simctl list devices -j`
end

Instance Method Details

#==(o) ⇒ Object



33
34
35
36
37
38
# File 'lib/peach/device.rb', line 33

def ==(o)
  o.class == self.class &&
  o.name == @name &&
  o.device_type == @device_type &&
  o.runtime == runtime
end

#createObject



18
19
20
21
22
23
24
25
26
# File 'lib/peach/device.rb', line 18

def create
  xc = XC.new
  runtime_id = xc.runtimes.key(@runtime)
  `xcrun simctl create \"#{@name}\" \"#{@device_type}\" \"#{runtime_id}\"`

  # reload everything to get the device.
  newDevices = Device.current_devices(xc: xc).select { |d| d == self }
  @udid = newDevices.first.udid
end

#destroyObject



28
29
30
# File 'lib/peach/device.rb', line 28

def destroy
  `xcrun simctl delete #{@udid}`
end

#eql?Object



32
# File 'lib/peach/device.rb', line 32

alias_method :eql?, :==

#exists?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/peach/device.rb', line 13

def exists?
  #could pass XC in? Make this a class function?
  Device.current_devices(xc: XC.new).include?(self)
end