Class: Hanami::View::Exposures Private

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/hanami/view/exposures.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:

  • 2.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exposures = {}) ⇒ Exposures

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 Exposures.

Since:

  • 2.1.0



20
21
22
# File 'lib/hanami/view/exposures.rb', line 20

def initialize(exposures = {})
  @exposures = exposures
end

Instance Attribute Details

#exposuresObject (readonly)

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:

  • 2.1.0



16
17
18
# File 'lib/hanami/view/exposures.rb', line 16

def exposures
  @exposures
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:

  • 2.1.0



32
33
34
# File 'lib/hanami/view/exposures.rb', line 32

def [](name)
  exposures[name]
end

#add(name, proc = nil, **options) ⇒ 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:

  • 2.1.0



44
45
46
# File 'lib/hanami/view/exposures.rb', line 44

def add(name, proc = nil, **options)
  exposures[name] = Exposure.new(name, proc, **options)
end

#bind(obj) ⇒ 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:

  • 2.1.0



56
57
58
59
60
61
62
# File 'lib/hanami/view/exposures.rb', line 56

def bind(obj)
  bound_exposures = exposures.each_with_object({}) { |(name, exposure), memo|
    memo[name] = exposure.bind(obj)
  }

  self.class.new(bound_exposures)
end

#call(input) ⇒ 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:

  • 2.1.0



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hanami/view/exposures.rb', line 66

def call(input)
  # Avoid performance cost of tsorting when we don't need it
  names =
    if exposures.values.any?(&:dependencies?) # TODO: this sholud be cachable at time of `#add`
      tsort
    else
      exposures.keys
    end

  names
    .each_with_object({}) { |name, memo|
      next unless (exposure = self[name])

      value = exposure.(input, memo)
      value = yield(value, exposure) if block_given?

      memo[name] = value
    }
    .tap { |hsh|
      names.each do |key|
        hsh.delete(key) if self[key].private?
      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:

  • 2.1.0



38
39
40
# File 'lib/hanami/view/exposures.rb', line 38

def each(&block)
  exposures.each(&block)
end

#import(name, exposure) ⇒ 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:

  • 2.1.0



50
51
52
# File 'lib/hanami/view/exposures.rb', line 50

def import(name, exposure)
  exposures[name] = exposure.dup
end

#key?(name) ⇒ Boolean

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:

  • (Boolean)

Since:

  • 2.1.0



26
27
28
# File 'lib/hanami/view/exposures.rb', line 26

def key?(name)
  exposures.key?(name)
end