Class: Frameit::Device

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

Constant Summary collapse

REQUIRED_PRIORITY =
999

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, formatted_name, priority, resolutions, density_ppi, default_color, platform = Platform::IOS, deliver_screen_id = nil, priority_config_key = nil) ⇒ Device

Returns a new instance of Device.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'frameit/lib/frameit/device.rb', line 17

def initialize(id, formatted_name, priority, resolutions, density_ppi, default_color, platform = Platform::IOS, deliver_screen_id = nil, priority_config_key = nil)
  Raise("Priority mustn't be higher than #{REQUIRED_PRIORITY}") if priority > REQUIRED_PRIORITY
  @id = id
  @deliver_screen_id = deliver_screen_id
  @formatted_name = formatted_name
  @priority = priority
  @resolutions = resolutions
  @density_ppi = density_ppi
  @default_color = default_color
  @platform = platform
  @priority_config_key = priority_config_key
end

Instance Attribute Details

#default_colorObject (readonly)

Returns the value of attribute default_color.



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

def default_color
  @default_color
end

#deliver_screen_idObject (readonly)

Returns the value of attribute deliver_screen_id.



9
10
11
# File 'frameit/lib/frameit/device.rb', line 9

def deliver_screen_id
  @deliver_screen_id
end

#density_ppiObject (readonly)

Returns the value of attribute density_ppi.



12
13
14
# File 'frameit/lib/frameit/device.rb', line 12

def density_ppi
  @density_ppi
end

#formatted_nameObject (readonly)

Returns the value of attribute formatted_name.



10
11
12
# File 'frameit/lib/frameit/device.rb', line 10

def formatted_name
  @formatted_name
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'frameit/lib/frameit/device.rb', line 8

def id
  @id
end

#platformObject (readonly)

Returns the value of attribute platform.



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

def platform
  @platform
end

#priority_config_keyObject (readonly)

Returns the value of attribute priority_config_key.



15
16
17
# File 'frameit/lib/frameit/device.rb', line 15

def priority_config_key
  @priority_config_key
end

#resolutionsObject (readonly)

Returns the value of attribute resolutions.



11
12
13
# File 'frameit/lib/frameit/device.rb', line 11

def resolutions
  @resolutions
end

Class Method Details

.detect_device(path, platform) ⇒ Object



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
# File 'frameit/lib/frameit/device.rb', line 46

def self.detect_device(path, platform)
  size = FastImage.size(path)

  UI.user_error!("Could not find or parse file at path '#{path}'") if size.nil? || size.count == 0

  found_device = nil
  filename_device = nil
  filename = Pathname.new(path).basename.to_s
  Devices.constants.sort_by(&:length).reverse_each do |c|
    device = Devices.const_get(c)
    next unless device.resolutions.include?(size)
    # assign to filename_device if the filename contains the formatted name / id and its priority is higher than the current filename_device
    filename_device = device if (filename.include?(device.formatted_name_without_apple) || filename.include?(device.id)) && (filename_device.nil? || filename_device.priority < device.priority)
    next unless device.is_chosen_platform?(platform) && (found_device.nil? || device.priority > found_device.priority)
    found_device = device
  end

  # prefer filename
  return filename_device if filename_device

  # return found_device which was detected according to platform & priority & settings if found
  return found_device if found_device

  # no device detected - show error and return nil
  UI.user_error!("Unsupported screen size #{size} for path '#{path}'")
  return nil
end

.find_device_by_id_or_name(id) ⇒ Object

Previously ENV was matched to Deliver::AppScreenshot::ScreenSize constants. However, options.rb defined a few Apple devices with unspecified IDs, this option was never read from Frameit.config. Therefore this function matches both ScreenSize constants and formatted names to maintain backward compatibility.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'frameit/lib/frameit/device.rb', line 77

def self.find_device_by_id_or_name(id)
  return nil if id.nil?
  found_device = nil
  # multiple devices can be matched to the same deliver_screen_id constant -> we return the one with the highest priority
  Devices.constants.each do |c|
    device = Devices.const_get(c)
    if (device.id == id || device.deliver_screen_id == id || device.formatted_name_without_apple == id) && (found_device.nil? || device.priority > found_device.priority)
      found_device = device
    end
  end
  return found_device
end

Instance Method Details

#formatted_name_without_appleObject



42
43
44
# File 'frameit/lib/frameit/device.rb', line 42

def formatted_name_without_apple
  formatted_name.gsub("Apple", "").strip.to_s
end

#is_chosen_platform?(platform) ⇒ Boolean

Returns:



38
39
40
# File 'frameit/lib/frameit/device.rb', line 38

def is_chosen_platform?(platform)
  @platform == platform || platform == Platform::ANY
end

#priorityObject



30
31
32
33
34
35
36
# File 'frameit/lib/frameit/device.rb', line 30

def priority
  if !priority_config_key.nil? && Frameit.config[priority_config_key]
    REQUIRED_PRIORITY
  else
    @priority
  end
end