Class: Layouter::Leaf::Custom
- Defined in:
- lib/layouter/leaf/custom.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#max_height ⇒ Object
readonly
Returns the value of attribute max_height.
-
#max_width ⇒ Object
readonly
Returns the value of attribute max_width.
-
#min_height ⇒ Object
readonly
Returns the value of attribute min_height.
-
#min_width ⇒ Object
readonly
Returns the value of attribute min_width.
Attributes inherited from Base
Attributes inherited from Element
#calculated_height, #calculated_width
Instance Method Summary collapse
-
#initialize(width: (0..INF), height: (0..INF), importance: 1, &block) ⇒ Custom
constructor
A new instance of Custom.
- #render ⇒ Object
Methods inherited from Base
Methods inherited from Element
Constructor Details
#initialize(width: (0..INF), height: (0..INF), importance: 1, &block) ⇒ Custom
Returns a new instance of Custom.
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_height ⇒ Object (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_width ⇒ Object (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_height ⇒ Object (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_width ⇒ Object (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
#render ⇒ Object
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 |