Class: Props::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/props_template/extensions/fragment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, fragments = []) ⇒ Fragment

Returns a new instance of Fragment.



5
6
7
8
# File 'lib/props_template/extensions/fragment.rb', line 5

def initialize(base, fragments = [])
  @base = base
  @fragments = fragments
end

Instance Attribute Details

#fragmentsObject (readonly)

Returns the value of attribute fragments.



3
4
5
# File 'lib/props_template/extensions/fragment.rb', line 3

def fragments
  @fragments
end

Class Method Details

.fragment_name_from_options(options, item = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/props_template/extensions/fragment.rb', line 10

def self.fragment_name_from_options(options, item = nil)
  return if !options[:partial]

  _, partial_opts = [*options[:partial]]
  return unless partial_opts

  fragment = partial_opts[:fragment]

  if item && ::Proc === fragment
    fragment = fragment.call(item)
  end

  if String === fragment || Symbol === fragment
    fragment.to_s
  end
end

Instance Method Details

#handle(options, item_context = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/props_template/extensions/fragment.rb', line 27

def handle(options, item_context = nil)
  fragment_name = self.class.fragment_name_from_options(options, item_context)
  path = @base.traveled_path
    .map { |item| item.is_a?(Array) ? item[0] : item }
    .join(".")

  if fragment_name
    @fragments.push(
      {id: fragment_name, path: path}
    )
  end
end