Class: Sandals::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/sandals/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title:, &definition) ⇒ Application

Returns a new instance of Application.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/sandals/application.rb', line 7

def initialize(title:, &definition)
  @title = title
  @definition = definition
  @refresh_interval = nil
  @view_definition = nil
  @snapshots = SnapshotHistory.new
  instance_eval(&definition)
  raise ArgumentError, "Sandals.app requires a view block" unless @view_definition
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, **options, &block) ⇒ Object (private)



102
103
104
105
106
107
108
# File 'lib/sandals/application.rb', line 102

def method_missing(name, *arguments, **options, &block)
  if @current_view&.respond_to?(name)
    @current_view.public_send(name, *arguments, **options, &block)
  else
    super
  end
end

Instance Attribute Details

#refresh_intervalObject (readonly)

Returns the value of attribute refresh_interval.



5
6
7
# File 'lib/sandals/application.rb', line 5

def refresh_interval
  @refresh_interval
end

Instance Method Details

#dispatch(revision:, events:, oldest_pending_revision: revision) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sandals/application.rb', line 63

def dispatch(revision:, events:, oldest_pending_revision: revision)
  if oldest_pending_revision > revision
    raise ArgumentError,
      "oldest pending revision cannot be newer than event revision"
  end

  snapshot = @snapshots.fetch(revision)

  EventDispatcher.new(snapshot.controls).dispatch(events)

  next_snapshot = publish_view
  @snapshots.discard_before(oldest_pending_revision)
  [next_snapshot.revision, HTMLRenderer.new.render_fragment(next_snapshot.view)]
end

#new_sessionObject



17
18
19
# File 'lib/sandals/application.rb', line 17

def new_session
  self.class.new(title: @title, &@definition)
end

#refresh(every:) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/sandals/application.rb', line 26

def refresh(every:)
  interval = Float(every, exception: false)
  unless interval&.finite? && interval >= 1
    raise ArgumentError, "refresh interval must be at least one second"
  end

  @refresh_interval = interval
end

#render(development_reload: false) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/sandals/application.rb', line 53

def render(development_reload: false)
  snapshot = publish_view
  HTMLRenderer.new.render(
    self,
    snapshot.view,
    revision: snapshot.revision,
    development_reload:
  )
end

#render_viewObject



45
46
47
48
49
50
51
# File 'lib/sandals/application.rb', line 45

def render_view
  @current_view = View.new
  instance_eval(&view)
  @current_view
ensure
  @current_view = nil
end

#select(label, **options, &action) ⇒ Object



35
36
37
# File 'lib/sandals/application.rb', line 35

def select(label, **options, &action)
  @current_view.select(label, **options, &action)
end

#title(*arguments) ⇒ Object



39
40
41
42
43
# File 'lib/sandals/application.rb', line 39

def title(*arguments)
  return @title if arguments.empty?

  @current_view.title(*arguments)
end

#view(&block) ⇒ Object



21
22
23
24
# File 'lib/sandals/application.rb', line 21

def view(&block)
  @view_definition = block if block
  @view_definition
end