Class: Pressroom::Blocks::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/pressroom/blocks/definition.rb

Overview

One registered block type: how it is edited, rendered, and indexed.

The reader/writer methods double as the registration DSL, so b.partial "x" sets the value and definition.partial reads it.

Constant Summary collapse

FULL_SANITIZER =
Rails::HTML4::FullSanitizer.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Definition

Returns a new instance of Definition.



16
17
18
19
20
21
22
23
# File 'lib/pressroom/blocks/definition.rb', line 16

def initialize(type)
  @type = type.to_s
  @schema = Schema.build
  @tool = nil
  @tool_config = {}
  @partial = nil
  @searchable = nil
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/pressroom/blocks/definition.rb', line 14

def type
  @type
end

Instance Method Details

#partial(path = nil) ⇒ Object



44
45
46
47
# File 'lib/pressroom/blocks/definition.rb', line 44

def partial(path = nil)
  @partial = path.to_s if path
  @partial
end

#schema(&block) ⇒ Object



39
40
41
42
# File 'lib/pressroom/blocks/definition.rb', line 39

def schema(&block)
  @schema = Schema.build(&block) if block
  @schema
end

#searchable(&block) ⇒ Object



49
50
51
52
# File 'lib/pressroom/blocks/definition.rb', line 49

def searchable(&block)
  @searchable = block if block
  @searchable
end

#text_for(data) ⇒ Object

Plain text for the host application's search index.



55
56
57
58
59
# File 'lib/pressroom/blocks/definition.rb', line 55

def text_for(data)
  return @searchable.call(data).to_s if @searchable

  default_text_for(data)
end

#tool(name = nil, **config) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/pressroom/blocks/definition.rb', line 25

def tool(name = nil, **config)
  if name
    @tool = name.to_s
    @tool_config = config
  end

  @tool ||= default_tool_name
end

#tool_configObject



34
35
36
37
# File 'lib/pressroom/blocks/definition.rb', line 34

def tool_config
  tool # ensure the default is materialized
  @tool_config
end

#validate!Object

Raises:



61
62
63
64
65
# File 'lib/pressroom/blocks/definition.rb', line 61

def validate!
  return if @partial

  raise Pressroom::Error, "block #{type.inspect} must declare a partial"
end