Class: Reight::ChipList

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/reight/chip.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ ChipList

Returns a new instance of ChipList.



118
119
120
121
# File 'lib/reight/chip.rb', line 118

def initialize(image)
  @image = image
  @next_id, @id2chip, @frame2chip = 1, {}, {}
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



123
124
125
# File 'lib/reight/chip.rb', line 123

def image
  @image
end

Class Method Details

.restore(hash, image) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/reight/chip.rb', line 152

def self.restore(hash, image)
  next_id, chips = hash.values_at :next_id, :chips
  #hash => {next_id:, chips:}
  new(image).tap do |obj|
    obj.instance_eval do
      @next_id    = next_id
      @id2chip    = chips
        .map {|hash| Reight::Chip.restore hash, image}
        .map {|chip| [chip.id, chip]}
        .to_h
      @frame2chip = @id2chip.each_value
        .with_object({}) {|chip, hash| hash[chip.frame] = chip}
    end
  end
end

Instance Method Details

#[](id) ⇒ Object



137
138
139
# File 'lib/reight/chip.rb', line 137

def [](id)
  @id2chip[id]
end

#at(x, y, w, h) ⇒ Object



125
126
127
# File 'lib/reight/chip.rb', line 125

def at(x, y, w, h)
  @frame2chip[[x, y, w, h]] ||= create_chip x, y, w, h
end

#cmp__(o) ⇒ Object



146
147
148
149
150
# File 'lib/reight/chip.rb', line 146

def cmp__(o)
  a =                  [@image, @next_id, @id2chip, @frame2chip]
  b = o.instance_eval {[@image, @next_id, @id2chip, @frame2chip]}
  a <=> b
end

#each(&block) ⇒ Object



129
130
131
# File 'lib/reight/chip.rb', line 129

def each(&block)
  @id2chip.values.each(&block)
end

#inspectObject



141
142
143
# File 'lib/reight/chip.rb', line 141

def inspect()
  "#<#{self.class.name}:0x#{object_id}>"
end

#to_hashObject



133
134
135
# File 'lib/reight/chip.rb', line 133

def to_hash()
  {next_id: @next_id, chips: @id2chip.values.map {_1.to_hash}}
end