Class: Sqed::Boundaries

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sqed/boundaries.rb

Overview

An Sqed::Boundaries is a simple wrapper for a hash that contains the co-ordinates for each section of a layout.

Layouts are Hashes defined in EXTRACTION_PATTERNS[<layout>]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout = nil) ⇒ Boundaries

Returns a new instance of Boundaries.

Raises:



26
27
28
29
30
31
32
33
# File 'lib/sqed/boundaries.rb', line 26

def initialize(layout = nil)
  raise Sqed::Error, 'unrecognized layout' if layout && !SqedConfig::LAYOUTS.include?(layout)
  @complete = false

  @layout = layout
  @coordinates = {}
  initialize_coordinates if !@layout.nil?
end

Instance Attribute Details

#completeBoolean

executed to completion

Returns:

  • (Boolean)

    whether or not the last method to populate this object



24
25
26
# File 'lib/sqed/boundaries.rb', line 24

def complete
  @complete
end

#coordinatesObject (readonly)

stores a hash References the section by integer index! In the pattern integer => [x1,y1, width, height] (ImageMagick convention rectangle descriptors) e.g.

0 => [10,10,40,40]


16
17
18
# File 'lib/sqed/boundaries.rb', line 16

def coordinates
  @coordinates
end

#layoutObject

A symbol from Sqed::Config::LAYOUTS.keys

:right_t


20
21
22
# File 'lib/sqed/boundaries.rb', line 20

def layout
  @layout
end

Instance Method Details

#countObject

Overrides Enumerable



68
69
70
# File 'lib/sqed/boundaries.rb', line 68

def count
  @coordinates.length
end

#each(&block) ⇒ Object



61
62
63
64
65
# File 'lib/sqed/boundaries.rb', line 61

def each(&block)
  @coordinates.each do |section_index, coords|
    block.call([section_index, coords])
  end
end

#for(section) ⇒ Object



57
58
59
# File 'lib/sqed/boundaries.rb', line 57

def for(section)
  @coordinates[section]
end

#height_for(index) ⇒ Object



84
85
86
# File 'lib/sqed/boundaries.rb', line 84

def height_for(index)
  @coordinates[index][3]
end

#initialize_coordinatesObject



35
36
37
38
39
# File 'lib/sqed/boundaries.rb', line 35

def initialize_coordinates
  SqedConfig::LAYOUTS[@layout].each do |k|
    @coordinates.merge!(k => [nil, nil, nil, nil] )
  end
end

#offset(boundary) ⇒ Sqed::Boundaries instance

Returns the idea here is to create a deep copy of self, offsetting by boundary as we go.

Returns:

  • (Sqed::Boundaries instance)

    the idea here is to create a deep copy of self, offsetting by boundary as we go



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sqed/boundaries.rb', line 44

def offset(boundary)
  b = Sqed::Boundaries.new
  (0..coordinates.length - 1).each do |i|
    b.set(i,
          [(x_for(i) + boundary.x_for(0)),
           (y_for(i) + boundary.y_for(0)),
           width_for(i),
           height_for(i)])
  end
  b.complete = complete
  b
end

#populated?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
# File 'lib/sqed/boundaries.rb', line 92

def populated?
  each do |index, coords|
    coords.each do |c|
      return false if c.nil?
    end
  end
  true
end

#set(index, coordinates) ⇒ Object



88
89
90
# File 'lib/sqed/boundaries.rb', line 88

def set(index, coordinates)
  @coordinates[index] = coordinates
end

#width_for(index) ⇒ Object



80
81
82
# File 'lib/sqed/boundaries.rb', line 80

def width_for(index)
  @coordinates[index][2]
end

#x_for(index) ⇒ Object



72
73
74
# File 'lib/sqed/boundaries.rb', line 72

def x_for(index)
  @coordinates[index][0]
end

#y_for(index) ⇒ Object



76
77
78
# File 'lib/sqed/boundaries.rb', line 76

def y_for(index)
  @coordinates[index][1]
end

#zoom(width_factor, height_factor) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/sqed/boundaries.rb', line 101

def zoom(width_factor, height_factor)
  coordinates.keys.each do |i|
    set(i, [
      (x_for(i).to_f * width_factor).to_i,
      (y_for(i).to_f * height_factor).to_i,
      (width_for(i).to_f * width_factor).to_i,
      (height_for(i).to_f * height_factor).to_i
    ])
  end
end