Class: Primrose::Rose

Inherits:
Object
  • Object
show all
Defined in:
lib/primrose/rose.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRose

Returns a new instance of Rose.



5
6
7
8
9
10
# File 'lib/primrose/rose.rb', line 5

def initialize
  @state = Observable.new({})
  @event_handlers = {}
  @children = []
  lifecycle(:initialize)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/primrose/rose.rb', line 3

def children
  @children
end

#event_handlersObject (readonly)

Returns the value of attribute event_handlers.



3
4
5
# File 'lib/primrose/rose.rb', line 3

def event_handlers
  @event_handlers
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/primrose/rose.rb', line 3

def state
  @state
end

Instance Method Details

#add_child(child) ⇒ Object



38
39
40
# File 'lib/primrose/rose.rb', line 38

def add_child(child)
  @children << child
end

#get_bindingObject



52
53
54
# File 'lib/primrose/rose.rb', line 52

def get_binding
  binding
end

#handle_error(error) ⇒ Object

Error Handling



47
48
49
50
# File 'lib/primrose/rose.rb', line 47

def handle_error(error)
  puts "An error occurred: #{error.message}"
  error.backtrace.each { |line| puts line }
end

#lifecycle(method) ⇒ Object



12
13
14
# File 'lib/primrose/rose.rb', line 12

def lifecycle(method)
  send(method) if respond_to?(method)
end

#on(event, &block) ⇒ Object



25
26
27
28
# File 'lib/primrose/rose.rb', line 25

def on(event, &block)
  @event_handlers[event] ||= []
  @event_handlers[event] << block
end

#renderObject



16
17
18
19
20
21
22
23
# File 'lib/primrose/rose.rb', line 16

def render
  begin
    lifecycle(:before_render)
    lifecycle(:after_render)
  rescue StandardError => e
    handle_error(e)
  end
end

#render_childrenObject



42
43
44
# File 'lib/primrose/rose.rb', line 42

def render_children
  @children.map { |child| child.render }.join("\n")
end

#trigger(event, *args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/primrose/rose.rb', line 30

def trigger(event, *args)
  return unless @event_handlers[event]

  @event_handlers[event].each do |handler|
    handler.call(*args)
  end
end