Sha256: fd0c57f4237ee3a97937ff11dd8d318ff5347af861e846860896abf69b11cd13

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

require 'beryl/utils'

module Beryl
  class Widget
    attr_reader :children

    def initialize
      @children = []
    end

    def build(type, *args, &block)
      element = Widget.new
      element.instance_eval(&block)
      {
        type: type,
        props: args,
        children: element.children
      }
    end

    def column(*args, &block)
      @children << build(:column, *args, &block)
      @children
    end

    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

    def row(*args, &block)
      @children << build(:row, *args, &block)
      @children
    end

    def text(string, *props)
      @children << {
        type: :text,
        value: string,
        props: props
      }
      @children
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
beryl-0.3.1 lib/beryl/widget.rb
beryl-0.3.0 lib/beryl/widget.rb
beryl-0.2.5 lib/beryl/widget.rb
beryl-0.2.4 lib/beryl/widget.rb
beryl-0.2.3 lib/beryl/widget.rb
beryl-0.2.2 lib/beryl/widget.rb
beryl-0.2.1 lib/beryl/widget.rb
beryl-0.2.0 lib/beryl/widget.rb