Class: Hanami::SliceRegistrar Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/slice_registrar.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

Constant Summary collapse

VALID_SLICE_NAME_RE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

/^[a-z][a-z0-9_]+$/
SLICE_DELIMITER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

CONTAINER_KEY_DELIMITER

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ SliceRegistrar

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SliceRegistrar.

Since:

  • 0.1.0



14
15
16
17
# File 'lib/hanami/slice_registrar.rb', line 14

def initialize(parent)
  @parent = parent
  @slices = {}
end

Instance Method Details

#[](name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



37
38
39
40
41
# File 'lib/hanami/slice_registrar.rb', line 37

def [](name)
  slices.fetch(name) do
    raise SliceLoadError, "Slice '#{name}' not found"
  end
end

#each(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



66
67
68
# File 'lib/hanami/slice_registrar.rb', line 66

def each(&block)
  slices.each_value(&block)
end

#freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



43
44
45
46
# File 'lib/hanami/slice_registrar.rb', line 43

def freeze
  slices.freeze
  super
end

#keysObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



70
71
72
# File 'lib/hanami/slice_registrar.rb', line 70

def keys
  slices.keys
end

#load_slicesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hanami/slice_registrar.rb', line 48

def load_slices
  slice_configs = Dir[root.join(CONFIG_DIR, SLICES_DIR, "*#{RB_EXT}")]
    .map { |file| File.basename(file, RB_EXT) }

  slice_dirs = Dir[File.join(root, SLICES_DIR, "*")]
    .select { |path| File.directory?(path) }
    .map { |path| File.basename(path) }

  slice_names = (slice_dirs + slice_configs).uniq.sort
    .then { filter_slice_names(_1) }

  slice_names.each do |slice_name|
    load_slice(slice_name)
  end

  self
end

#register(name, slice_class = nil, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hanami/slice_registrar.rb', line 19

def register(name, slice_class = nil, &block)
  unless name.to_s =~ VALID_SLICE_NAME_RE
    raise ArgumentError, "slice name #{name.inspect} must be lowercase alphanumeric text and underscores only"
  end

  return unless filter_slice_names([name]).any?

  if slices.key?(name.to_sym)
    raise SliceLoadError, "Slice '#{name}' is already registered"
  end

  slice = slice_class || build_slice(name, &block)

  configure_slice(name, slice)

  slices[name.to_sym] = slice
end

#to_aObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



74
75
76
# File 'lib/hanami/slice_registrar.rb', line 74

def to_a
  slices.values
end

#with_nestedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



78
79
80
81
82
83
84
# File 'lib/hanami/slice_registrar.rb', line 78

def with_nested
  to_a.flat_map { |slice|
    # Return nested slices first so that their more specific namespaces may be picked up first
    # by SliceConfigurable#slice_for
    slice.slices.with_nested + [slice]
  }
end