Class: LexxyVariables::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/lexxy_variables/configuration.rb

Overview

Host-supplied policy. The gem ships working defaults, so the smallest integration is just config.catalog = [...]. Everything else is optional.

LexxyVariables.configure do |c|
c.catalog = [ { key: "company", name: "Company", value: "Acme" } ]
end

catalog: the insertable items for the editor prompt. A list, a zero-arg callable, or a ->(context) callable. Items respond to #key, #name, optional #value (used by the default assigns), and #attachable_sgid. assigns: ->(context, used_keys) or ->(used_keys) returning a Hash of key => value for a render. Omit to read #value off catalog items. renderer: Renderers::Substitution (default, no Liquid) or Renderers::Liquid. max_fragment_depth: how deep :html chips (e.g. snippets) expand. The default of 1 resolves a snippet's inner variables and drops nested snippets. content_layout: the ActionText content layout wrapper for rendered output. sort: how the catalog is ordered in the prompt and dropdown. Defaults to :name (case-insensitive alphabetical). Use :key to sort by key, false to preserve the catalog's given order, or a callable: a ->(item) sort key, or a ->(a, b) comparator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



27
28
29
30
31
32
33
34
35
# File 'lib/lexxy_variables/configuration.rb', line 27

def initialize
  @registry = Registry.new
  @renderer = Renderers::Substitution.new
  @content_layout = "layouts/action_text/contents/content"
  @max_fragment_depth = 1
  @catalog = []
  @sort = :name
  register_default_variable_type
end

Instance Attribute Details

#assignsObject

Returns the value of attribute assigns.



23
24
25
# File 'lib/lexxy_variables/configuration.rb', line 23

def assigns
  @assigns
end

#catalog=(value) ⇒ Object (writeonly)

Sets the attribute catalog

Parameters:

  • value

    the value to set the attribute catalog to.



25
26
27
# File 'lib/lexxy_variables/configuration.rb', line 25

def catalog=(value)
  @catalog = value
end

#content_layoutObject

Returns the value of attribute content_layout.



23
24
25
# File 'lib/lexxy_variables/configuration.rb', line 23

def content_layout
  @content_layout
end

#max_fragment_depthObject

Returns the value of attribute max_fragment_depth.



23
24
25
# File 'lib/lexxy_variables/configuration.rb', line 23

def max_fragment_depth
  @max_fragment_depth
end

#registryObject (readonly)

Returns the value of attribute registry.



24
25
26
# File 'lib/lexxy_variables/configuration.rb', line 24

def registry
  @registry
end

#rendererObject

Returns the value of attribute renderer.



23
24
25
# File 'lib/lexxy_variables/configuration.rb', line 23

def renderer
  @renderer
end

#sortObject

Returns the value of attribute sort.



23
24
25
# File 'lib/lexxy_variables/configuration.rb', line 23

def sort
  @sort
end

Instance Method Details

#register_attachment(content_type:, resolve:, renders_as: :text, label: nil) ⇒ Object

Adds or overrides an attachment type. Variables are pre-registered. A host calls this to add types like snippets, or to override the variable resolver. renders_as is :text (default, resolver returns a key whose value is substituted HTML-escaped) or :html (resolver returns rich content spliced in pre-sanitize, e.g. snippets). label is an optional badge name shown in the prompt when types are mixed.



43
44
45
46
47
48
49
# File 'lib/lexxy_variables/configuration.rb', line 43

def register_attachment(content_type:, resolve:, renders_as: :text, label: nil)
  unless %i[text html].include?(renders_as)
    raise ArgumentError, "renders_as must be :text or :html, got #{renders_as.inspect}"
  end

  registry.register(AttachmentType.new(content_type:, renders_as:, resolve:, label:))
end

#resolve_assigns(context, keys) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/lexxy_variables/configuration.rb', line 55

def resolve_assigns(context, keys)
  return default_assigns(context, keys) unless @assigns

  if @assigns.arity == 1
    @assigns.call(keys)
  else
    @assigns.call(context, keys)
  end
end

#resolve_catalog(context) ⇒ Object



51
52
53
# File 'lib/lexxy_variables/configuration.rb', line 51

def resolve_catalog(context)
  sort_catalog(Array(call_with_context(@catalog, context)))
end