Class: Dato::Local::FieldType::StructuredText
- Inherits:
-
Object
- Object
- Dato::Local::FieldType::StructuredText
- Defined in:
- lib/dato/local/field_type/structured_text.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #blocks ⇒ Object
- #find_all_nodes(types) ⇒ Object
-
#initialize(value, repo) ⇒ StructuredText
constructor
A new instance of StructuredText.
- #links ⇒ Object
- #to_hash(max_depth = 3, current_depth = 0) ⇒ Object
- #visit(node, &block) ⇒ Object
Constructor Details
#initialize(value, repo) ⇒ StructuredText
Returns a new instance of StructuredText.
11 12 13 14 |
# File 'lib/dato/local/field_type/structured_text.rb', line 11 def initialize(value, repo) @value = value @repo = repo end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
16 17 18 |
# File 'lib/dato/local/field_type/structured_text.rb', line 16 def value @value end |
Class Method Details
.parse(value, repo) ⇒ Object
7 8 9 |
# File 'lib/dato/local/field_type/structured_text.rb', line 7 def self.parse(value, repo) new(value, repo) end |
Instance Method Details
#blocks ⇒ Object
18 19 20 21 22 |
# 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) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# 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 |
#links ⇒ Object
24 25 26 27 28 |
# File 'lib/dato/local/field_type/structured_text.rb', line 24 def links find_all_nodes(%w[inlineItem itemLink]).map do |node| @repo.find(node["item"]) end.uniq end |
#to_hash(max_depth = 3, current_depth = 0) ⇒ Object
53 54 55 56 57 58 59 |
# 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) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# 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 |