Class: Milktea::Container

Inherits:
Model
  • Object
show all
Defined in:
lib/milktea/container.rb

Overview

Container model with layout capabilities using Flexbox

Direct Known Subclasses

Text

Instance Attribute Summary collapse

Attributes inherited from Model

#children, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

children, #children_views, #screen_height, #screen_size, #screen_width, #update, #with

Constructor Details

#initialize(state = {}) ⇒ Container

Returns a new instance of Container.



35
36
37
38
39
# File 'lib/milktea/container.rb', line 35

def initialize(state = {})
  @bounds = extract_bounds(state)
  # Remove bounds keys from state before passing to parent
  super(state.except(:width, :height, :x, :y))
end

Instance Attribute Details

#boundsObject (readonly)

Returns the value of attribute bounds.



6
7
8
# File 'lib/milktea/container.rb', line 6

def bounds
  @bounds
end

Class Method Details

.child(klass, mapper = nil, flex: 1) ⇒ Object

Define a child model with optional state mapping and flex properties

Parameters:

  • klass (Class, Symbol)

    The child model class or method name

  • mapper (Proc) (defaults to: nil)

    Lambda to map parent state to child state

  • flex (Integer) (defaults to: 1)

    Flex grow factor for layout



13
14
15
16
17
18
19
20
# File 'lib/milktea/container.rb', line 13

def child(klass, mapper = nil, flex: 1)
  @children ||= []
  @children << {
    class: klass,
    mapper: mapper || ->(_state) { {} },
    flex: flex
  }
end

.direction(dir) ⇒ Object

Set the flex direction for the container

Parameters:

  • dir (Symbol)

    The direction (:column or :row)



24
25
26
# File 'lib/milktea/container.rb', line 24

def direction(dir)
  @direction = dir
end

.flex_directionSymbol

Get the flex direction (defaults to :column)

Returns:

  • (Symbol)

    The flex direction



30
31
32
# File 'lib/milktea/container.rb', line 30

def flex_direction
  @direction || :column
end

Instance Method Details

#viewObject



41
# File 'lib/milktea/container.rb', line 41

def view = children_views