Class: Scarpe::Components::SegmentedFileLoader

Inherits:
Object
  • Object
show all
Includes:
FileHelpers
Defined in:
lib/scarpe/components/segmented_file_loader.rb

Instance Method Summary collapse

Methods included from FileHelpers

#with_tempfile, #with_tempfiles

Instance Method Details

#add_segment_type(type, handler) ⇒ void

This method returns an undefined value.

Add a new segment type (e.g. “catscradle”) with a different file handler.

Parameters:

  • type (String)

    the new name for this segment type

  • handler (Object)

    an object that will be called as obj.call(filename) - often a proc



15
16
17
18
19
20
21
# File 'lib/scarpe/components/segmented_file_loader.rb', line 15

def add_segment_type(type, handler)
  if segment_type_hash.key?(type)
    raise "Segment type #{type.inspect} already exists!"
  end

  segment_type_hash[type] = handler
end

#after_load(&block) ⇒ Object

Segment type handlers can call this to perform an operation after the load has completed. This is important for ordering, and because loading a Shoes app often doesn’t return. So to have a later section (e.g. tests, additional data) do something that affects Shoes app loading (e.g. set an env var, affect the display service) it’s important that app loading take place later in the sequence.



53
54
55
56
# File 'lib/scarpe/components/segmented_file_loader.rb', line 53

def after_load(&block)
  @after_load ||= []
  @after_load << block
end

#call(path) ⇒ Boolean

Load a .sca file with an optional YAML frontmatter prefix and multiple file sections which can be treated differently.

The file loader acts like a proc, being called with .call() and returning true or false for whether it has handled the file load. This allows chaining loaders in order and the first loader to recognise a file will run it.

Parameters:

  • path (String)

    the file or directory to treat as a Scarpe app

Returns:

  • (Boolean)

    return true if the file is loaded as a segmented Scarpe app file



40
41
42
43
44
45
# File 'lib/scarpe/components/segmented_file_loader.rb', line 40

def call(path)
  return false unless path.end_with?(".scas")

  file_load(path)
  true
end

#segment_typesObject

Return an Array of segment type labels, such as “code” and “app_test”.



26
27
28
# File 'lib/scarpe/components/segmented_file_loader.rb', line 26

def segment_types
  segment_type_hash.keys
end