Class: Hokusai::Node

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
ui/src/hokusai/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast, portal = nil) ⇒ Node

Returns a new instance of Node.



30
31
32
33
34
35
# File 'ui/src/hokusai/node.rb', line 30

def initialize(ast, portal = nil)
  @ast = ast
  @portal = portal
  @uuid = Random.hex(6).freeze
  @meta = Meta.new
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



11
12
13
# File 'ui/src/hokusai/node.rb', line 11

def ast
  @ast
end

#metaObject (readonly)

Returns the value of attribute meta.



11
12
13
# File 'ui/src/hokusai/node.rb', line 11

def meta
  @meta
end

#nodeObject (readonly)

Returns the value of attribute node.



11
12
13
# File 'ui/src/hokusai/node.rb', line 11

def node
  @node
end

#portalObject (readonly)

Returns the value of attribute portal.



11
12
13
# File 'ui/src/hokusai/node.rb', line 11

def portal
  @portal
end

#uuidObject (readonly)

Returns the value of attribute uuid.



11
12
13
# File 'ui/src/hokusai/node.rb', line 11

def uuid
  @uuid
end

Class Method Details

.parse(template, name = "root", parent = nil) ⇒ Object



24
25
26
27
28
# File 'ui/src/hokusai/node.rb', line 24

def self.parse(template, name = "root", parent = nil)
  ast = Ast.parse(template, name)

  new(ast, parent)
end

.provide(node, key, value) ⇒ Object



19
20
21
22
# File 'ui/src/hokusai/node.rb', line 19

def self.provide(node, key, value)
  provides[node.uuid] ||= {}
  provides[node.uuid][key] = value
end

.providesObject



15
16
17
# File 'ui/src/hokusai/node.rb', line 15

def self.provides
  @provides ||= {}
end

Instance Method Details

#add_props_from_block(parent, context: nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'ui/src/hokusai/node.rb', line 73

def add_props_from_block(parent, context: nil)
  if local_portal = portal
    if block = parent
      local_portal.ast.props.each do |_, prop|
        method = prop.value.method

        case prop.computed?
        when true
          if prop.value.args.size > 0 && context
            value = context.send_target(block, prop.value)
          elsif context&.table&.[](method)
            value = context.table[method]
          else
            value = block.instance_eval(method)
          end
        else
          value = method
        end

        meta.set_prop(prop.name.to_sym, value)
      end
    end
  end
end

#add_styles(klass) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ui/src/hokusai/node.rb', line 59

def add_styles(klass)
  return if portal.nil?

  portal.ast.style_list.each do |style_name|
    style = klass.styles_get[style_name]

    raise Hokusai::Error.new("Style #{style_name} doesn't exist in the styles for this block #{klass} - #{klass.styles_get.keys}") if style.nil?

    style.each do |key, value|
      meta.set_prop(key.to_sym, value)
    end
  end
end

#destroyObject



41
42
43
44
45
46
47
# File 'ui/src/hokusai/node.rb', line 41

def destroy
  # meta.children?&.each do |child|
  #   child.node.destroy
  # end
  #
  # ast.destroy
end

#emit(name, **args) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'ui/src/hokusai/node.rb', line 49

def emit(name, **args)
  if node = portal
    if event = node.event(name)
      meta.publisher.notify(event.value.name, **args)
    else
      raise Hokusai::Error.new("Invocation failed: @#{name} doesn't exist on #{node.type}")
    end
  end
end

#mount(klass) ⇒ Object



37
38
39
# File 'ui/src/hokusai/node.rb', line 37

def mount(klass)
  NodeMounter.new(self, klass).mount
end