class Beryl::Widget

Attributes

children[R]

Public Class Methods

new() click to toggle source
# File lib/beryl/widget.rb, line 7
def initialize
  @children = []
end

Public Instance Methods

build(type, *args, &block) click to toggle source
# File lib/beryl/widget.rb, line 11
def build(type, *args, &block)
  element = Widget.new
  element.instance_eval(&block)
  {
    type: type,
    props: args,
    children: element.children
  }
end
column(*args, &block) click to toggle source
# File lib/beryl/widget.rb, line 21
def column(*args, &block)
  @children << build(:column, *args, &block)
  @children
end
method_missing(name, *args, &block) click to toggle source
# File lib/beryl/widget.rb, line 26
def method_missing(name, *args, &block)
  constantized = Beryl::Utils.constantize(name.to_s)
  child = args.any? ? constantized.new.render(*args) : constantized.new.render
  raise SyntaxError.new("Widget #{name} should return only one element (use row or column)") if child.is_a?(Array) && child.size > 1
  @children += child
  child
rescue NoMethodError
  raise NameError.new("There is no such widget: #{name}")
end
row(*args, &block) click to toggle source
# File lib/beryl/widget.rb, line 36
def row(*args, &block)
  @children << build(:row, *args, &block)
  @children
end
text(string, *props) click to toggle source
# File lib/beryl/widget.rb, line 41
def text(string, *props)
  @children << {
    type: :text,
    value: string,
    props: props
  }
  @children
end