Class: LcpRuby::Metadata::DisplayTemplateDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/metadata/display_template_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ DisplayTemplateDefinition

Returns a new instance of DisplayTemplateDefinition.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 7

def initialize(attrs = {})
  @name = attrs[:name].to_s
  @template = attrs[:template]
  @subtitle = attrs[:subtitle]
  @icon = attrs[:icon]
  @badge = attrs[:badge]
  @renderer = attrs[:renderer]
  @partial = attrs[:partial]
  @options = attrs[:options] || {}

  validate!
end

Instance Attribute Details

#badgeObject (readonly)

Returns the value of attribute badge.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def badge
  @badge
end

#iconObject (readonly)

Returns the value of attribute icon.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def icon
  @icon
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def options
  @options
end

#partialObject (readonly)

Returns the value of attribute partial.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def partial
  @partial
end

#rendererObject (readonly)

Returns the value of attribute renderer.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def renderer
  @renderer
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def subtitle
  @subtitle
end

#templateObject (readonly)

Returns the value of attribute template.



4
5
6
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 4

def template
  @template
end

Class Method Details

.from_hash(name, hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 20

def self.from_hash(name, hash)
  new(
    name: name,
    template: hash["template"],
    subtitle: hash["subtitle"],
    icon: hash["icon"],
    badge: hash["badge"],
    renderer: hash["renderer"],
    partial: hash["partial"],
    options: hash["options"] || {}
  )
end

Instance Method Details

#formObject



33
34
35
36
37
38
39
40
41
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 33

def form
  if @renderer
    :renderer
  elsif @partial
    :partial
  else
    :structured
  end
end

#partial?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 51

def partial?
  form == :partial
end

#referenced_fieldsObject

Extract field references from template, subtitle, badge strings. Returns an array of unique field path strings.



63
64
65
66
67
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 63

def referenced_fields
  @referenced_fields ||= string_slots.values.compact
                                     .flat_map { |s| s.scan(/\{([^}]+)\}/).flatten.map(&:strip) }
                                     .uniq
end

#renderer?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 47

def renderer?
  form == :renderer
end

#string_slotsObject

The string-valued slots that may carry ‘field` placeholders. Single source of truth for `referenced_fields` and validators.



57
58
59
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 57

def string_slots
  { "template" => @template, "subtitle" => @subtitle, "badge" => @badge }
end

#structured?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/lcp_ruby/metadata/display_template_definition.rb', line 43

def structured?
  form == :structured
end