Class: Layouter::Leaf::Custom

Inherits:
Base show all
Defined in:
lib/layouter/leaf/custom.rb

Constant Summary

Constants inherited from Base

Base::EPS, Base::INF

Instance Attribute Summary collapse

Attributes inherited from Base

#importance

Attributes inherited from Element

#calculated_height, #calculated_width

Instance Method Summary collapse

Methods inherited from Base

#layout

Methods inherited from Element

#layout?

Constructor Details

#initialize(width: (0..INF), height: (0..INF), importance: 1, &block) ⇒ Custom

Returns a new instance of Custom.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/layouter/leaf/custom.rb', line 7

def initialize(width: (0..INF), height: (0..INF), importance: 1, &block)
  super(importance: importance)
  [:width, :height].each do |dim|
    eval <<-CODE, binding, __FILE__ , __LINE__ + 1
      if #{dim}.is_a?(Integer)
        @min_#{dim} = @max_#{dim} = #{dim}
      elsif #{dim}.is_a?(Range) && #{dim}.first.is_a?(Integer)
        @min_#{dim} = #{dim}.first
        @max_#{dim} = #{dim}.exclude_end? ? #{dim}.last - 1 : #{dim}.last
      else
        raise(ArgumentError.new("Invalid #{dim}"))
      end
      if @min_#{dim} > @max_#{dim}
        raise(ArgumentError.new("Inconsistent minimum and maximum #{dim}"))
      end
    CODE
  end
  raise(ArgumentError.new("Must pass block")) unless block_given?
  @block = block
end

Instance Attribute Details

#max_heightObject (readonly)

Returns the value of attribute max_height.



5
6
7
# File 'lib/layouter/leaf/custom.rb', line 5

def max_height
  @max_height
end

#max_widthObject (readonly)

Returns the value of attribute max_width.



5
6
7
# File 'lib/layouter/leaf/custom.rb', line 5

def max_width
  @max_width
end

#min_heightObject (readonly)

Returns the value of attribute min_height.



5
6
7
# File 'lib/layouter/leaf/custom.rb', line 5

def min_height
  @min_height
end

#min_widthObject (readonly)

Returns the value of attribute min_width.



5
6
7
# File 'lib/layouter/leaf/custom.rb', line 5

def min_width
  @min_width
end

Instance Method Details

#renderObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/layouter/leaf/custom.rb', line 28

def render
  layout!
  w, h = @calculated_width, @calculated_height
  res = @block.call(w, h)
  lines = res.split("\n")
  if lines.length != h || !lines.all? { |l| l.length == w }
    raise(ArgumentError.new("Custom render has incorrect dimensions"))
  end
  res
end