class DraftjsExporter::EntityState

Attributes

entity_decorators[R]
entity_map[R]
entity_stack[R]
root_element[R]

Public Class Methods

new(root_element, entity_decorators, entity_map) click to toggle source
# File lib/draftjs_exporter/entity_state.rb, line 11
def initialize(root_element, entity_decorators, entity_map)
  @entity_decorators = entity_decorators
  @entity_map = entity_map
  @entity_stack = [[Entities::Null.new.call(root_element, nil), nil]]
end

Public Instance Methods

apply(command) click to toggle source
# File lib/draftjs_exporter/entity_state.rb, line 17
def apply(command)
  case command.name
  when :start_entity
    start_command(command)
  when :stop_entity
    stop_command(command)
  end
end
current_parent() click to toggle source
# File lib/draftjs_exporter/entity_state.rb, line 26
def current_parent
  element, _data = entity_stack.last
  element
end

Private Instance Methods

entity_for(key) click to toggle source
# File lib/draftjs_exporter/entity_state.rb, line 53
def entity_for(key)
  entity_keys = [key.to_s, key.to_s.to_sym]
  entity_keys.map { |key| entity_map.fetch(key, nil) }.compact.first
end
start_command(command) click to toggle source
# File lib/draftjs_exporter/entity_state.rb, line 33
def start_command(command)
  entity_details = entity_for(command.data)

  decorator = entity_decorators.fetch(entity_details.fetch(:type))
  parent_element = entity_stack.last.first
  new_element = decorator.call(parent_element, entity_details)
  entity_stack.push([new_element, entity_details])
end
stop_command(command) click to toggle source
# File lib/draftjs_exporter/entity_state.rb, line 42
def stop_command(command)
  entity_details = entity_for(command.data)
  _element, expected_entity_details = entity_stack.last

  if expected_entity_details != entity_details
    raise InvalidEntity, "Expected #{expected_entity_details.inspect} got #{entity_details.inspect}"
  end

  entity_stack.pop
end