Class: XCRes::XCAssets::ResourceImage

Inherits:
Object
  • Object
show all
Defined in:
lib/xcres/model/xcassets/resource_image.rb

Overview

Represents a single image of a resource in an asset catalog

Constant Summary collapse

KNOWN_KEYS =

The known keys

[
    :filename,
    :scale,
    :orientation,
    :size,
    :idiom,
    :subtype,
    :extent,
    :minimum_system_version
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ ResourceImage

Initialize a new ResourceImage

Parameters:

  • the (Hash)

    initial attribute values



73
74
75
76
77
78
79
80
81
# File 'lib/xcres/model/xcassets/resource_image.rb', line 73

def initialize(attributes={})
  self.scale = attributes.delete('scale') unless attributes['scale'].nil?

  KNOWN_KEYS.each do |key|
    self.send "#{key}=".to_sym, attributes.delete(key)
  end

  self.attributes = attributes
end

Instance Attribute Details

#attributesHash{Symbol => String}

Returns further attributes, not mapped to a specific attribute.

Returns:

  • (Hash{Symbol => String})

    further attributes, not mapped to a specific attribute



55
56
57
# File 'lib/xcres/model/xcassets/resource_image.rb', line 55

def attributes
  @attributes
end

#extentSymbol

Returns the extent, e.g. ‘full-screen’.

Returns:

  • (Symbol)

    the extent, e.g. ‘full-screen’



47
48
49
# File 'lib/xcres/model/xcassets/resource_image.rb', line 47

def extent
  @extent
end

#filenamePathname

Returns file name.

Returns:

  • (Pathname)

    file name



23
24
25
# File 'lib/xcres/model/xcassets/resource_image.rb', line 23

def filename
  @filename
end

#idiomSymbol

Returns the idiom, e.g. ‘iphone’.

Returns:

  • (Symbol)

    the idiom, e.g. ‘iphone’



39
40
41
# File 'lib/xcres/model/xcassets/resource_image.rb', line 39

def idiom
  @idiom
end

#minimum_system_versionString

Returns the minimum system version, e.g. ‘7.0’.

Returns:

  • (String)

    the minimum system version, e.g. ‘7.0’



51
52
53
# File 'lib/xcres/model/xcassets/resource_image.rb', line 51

def minimum_system_version
  @minimum_system_version
end

#orientationSymbol

Returns the orientation, e.g. ‘portrait’.

Returns:

  • (Symbol)

    the orientation, e.g. ‘portrait’



31
32
33
# File 'lib/xcres/model/xcassets/resource_image.rb', line 31

def orientation
  @orientation
end

#scaleInteger

Returns scale of the image.

Returns:

  • (Integer)

    scale of the image



27
28
29
# File 'lib/xcres/model/xcassets/resource_image.rb', line 27

def scale
  @scale
end

#sizeString

Returns the size, e.g. ‘29x29’, ‘40x40’.

Returns:

  • (String)

    the size, e.g. ‘29x29’, ‘40x40’



35
36
37
# File 'lib/xcres/model/xcassets/resource_image.rb', line 35

def size
  @size
end

#subtypeSymbol

Returns the subtype, e.g. ‘retina4’.

Returns:

  • (Symbol)

    the subtype, e.g. ‘retina4’



43
44
45
# File 'lib/xcres/model/xcassets/resource_image.rb', line 43

def subtype
  @subtype
end

Class Method Details

.read(hash) ⇒ ResourceImage

Read from hash

Parameters:

  • the (Hash)

    hash to deserialize

Returns:



64
65
66
# File 'lib/xcres/model/xcassets/resource_image.rb', line 64

def self.read(hash)
  self.new.read(hash)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



125
126
127
128
# File 'lib/xcres/model/xcassets/resource_image.rb', line 125

def ==(other)
  return false unless other.respond_to?(:to_hash)
  self.to_hash == other.to_hash
end

#read(hash) ⇒ ResourceImage

Read from hash

Parameters:

  • the (Hash)

    hash to deserialize

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xcres/model/xcassets/resource_image.rb', line 90

def read(hash)
  self.scale = hash.delete('scale').sub(/x$/, '').to_i unless hash['scale'].nil?

  KNOWN_KEYS.each do |key|
    value = hash.delete(key.to_s.dasherize)
    next if value.nil?
    self.send "#{key}=".to_sym, value
  end

  self.attributes = hash

  return self
end

#to_hashHash{String => String}

Serialize to hash

Returns:

  • (Hash{String => String})


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xcres/model/xcassets/resource_image.rb', line 108

def to_hash
  hash = {}

  hash['scale'] = "#{scale}x" unless scale.nil?

  (KNOWN_KEYS - [:scale]).each do |key|
    value = self.send(key)
    hash[key.to_s.dasherize] = value.to_s unless value.nil?
  end

  attributes.each do |key, value|
    hash[key.to_s] = value
  end

  hash
end