Class: Exclaim::Ui

Inherits:
Object
  • Object
show all
Defined in:
lib/exclaim/ui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(implementation_map: Exclaim::Implementations.example_implementation_map, should_escape_html: true) ⇒ Ui

Returns a new instance of Ui.



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

def initialize(implementation_map: Exclaim::Implementations.example_implementation_map, should_escape_html: true)
  @implementation_map = Exclaim::ImplementationMap.parse!(implementation_map)
  @should_escape_html = should_escape_html
rescue Exclaim::Error
  raise
rescue StandardError => e
  e.extend(Exclaim::InternalError)
  raise
end

Instance Attribute Details

#implementation_mapObject (readonly)

Returns the value of attribute implementation_map.



5
6
7
# File 'lib/exclaim/ui.rb', line 5

def implementation_map
  @implementation_map
end

#parsed_uiObject

Returns the value of attribute parsed_ui.



5
6
7
# File 'lib/exclaim/ui.rb', line 5

def parsed_ui
  @parsed_ui
end

#rendererObject (readonly)

Returns the value of attribute renderer.



5
6
7
# File 'lib/exclaim/ui.rb', line 5

def renderer
  @renderer
end

Instance Method Details

#each_element(element_names = :ALL_ELEMENTS, &blk) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/exclaim/ui.rb', line 50

def each_element(element_names = :ALL_ELEMENTS, &blk)
  if parsed_ui.nil?
    error_message = 'Cannot compute each_element without UI configured, ' \
                    'must call Exclaim::Ui#parse_ui(ui_config) first'
    raise UiConfigurationError.new(error_message)
  end
  normalized_element_names = parse_element_names(element_names)

  if block_given?
    top_level_component = parsed_ui
    recurse_json_declarations(top_level_component, normalized_element_names, &blk)
  else
    to_enum(__callee__)
  end
end

#parse_ui!(ui_config) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/exclaim/ui.rb', line 17

def parse_ui!(ui_config)
  self.parsed_ui = Exclaim::UiConfiguration.parse!(@implementation_map, ui_config)
rescue Exclaim::Error
  raise
rescue StandardError => e
  e.extend(Exclaim::InternalError)
  raise
end

#render(env: {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/exclaim/ui.rb', line 26

def render(env: {})
  if parsed_ui.nil?
    error_message = 'Cannot render without UI configured, must call Exclaim::Ui#parse_ui(ui_config) first'
    raise RenderingError.new(error_message)
  end

  renderer.call(env: env)
rescue Exclaim::Error
  raise
rescue StandardError => e
  e.extend(Exclaim::InternalError)
  raise
end

#unique_bind_pathsObject



40
41
42
43
44
45
46
47
48
# File 'lib/exclaim/ui.rb', line 40

def unique_bind_paths
  if parsed_ui.nil?
    error_message = 'Cannot compute unique_bind_paths without UI configured, ' \
                    'must call Exclaim::Ui#parse_ui(ui_config) first'
    raise UiConfigurationError.new(error_message)
  end

  parsed_ui.config.reduce([]) { |all_paths, config_value| bind_paths(config_value, all_paths) }.uniq!
end