class Dato::Local::FieldType::StructuredText

Attributes

value[R]

Public Class Methods

new(value, repo) click to toggle source
# File lib/dato/local/field_type/structured_text.rb, line 11
def initialize(value, repo)
  @value = value
  @repo = repo
end
parse(value, repo) click to toggle source
# File lib/dato/local/field_type/structured_text.rb, line 7
def self.parse(value, repo)
  new(value, repo)
end

Public Instance Methods

blocks() click to toggle source
# File lib/dato/local/field_type/structured_text.rb, line 18
def blocks
  find_all_nodes("block").map do |node|
    @repo.find(node["item"])
  end.uniq
end
find_all_nodes(types) click to toggle source
# File lib/dato/local/field_type/structured_text.rb, line 30
def find_all_nodes(types)
  return [] if value.nil?

  types = Array(types)
  result = []

  visit(value["document"]) do |node|
    result << node if node.is_a?(Hash) && types.include?(node["type"])
  end

  result
end
to_hash(max_depth = 3, current_depth = 0) click to toggle source
# File lib/dato/local/field_type/structured_text.rb, line 53
def to_hash(max_depth = 3, current_depth = 0)
  {
    value: value,
    links: links.map { |item| item.to_hash(max_depth, current_depth) },
    blocks: blocks.map { |item| item.to_hash(max_depth, current_depth) },
  }
end
visit(node, &block) click to toggle source
# File lib/dato/local/field_type/structured_text.rb, line 43
def visit(node, &block)
  if node.is_a?(Hash) && node["children"].is_a?(Array)
    node["children"].each do |child|
      visit(child, &block)
    end
  end

  block.call(node)
end