Class: Sourcerer::Templating::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcerer/templating.rb

Overview

Holds contextual information for templating.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage: :load, strict: false, scopes: {}) ⇒ Context

Returns a new instance of Context.

Parameters:

  • stage (Symbol) (defaults to: :load)

    The rendering stage.

  • strict (Boolean) (defaults to: false)

    Whether to use strict rendering.

  • scopes (Hash) (defaults to: {})

    A hash of scopes.



124
125
126
127
128
# File 'lib/sourcerer/templating.rb', line 124

def initialize stage: :load, strict: false, scopes: {}
  @stage  = stage.to_sym
  @strict = strict
  @scopes = scopes.transform_keys(&:to_sym)
end

Instance Attribute Details

#scopesHash (readonly)

Returns A hash of scopes for rendering.

Returns:

  • (Hash)

    A hash of scopes for rendering.



119
120
121
# File 'lib/sourcerer/templating.rb', line 119

def scopes
  @scopes
end

#stageSymbol (readonly)

Returns The rendering stage (e.g., :load).

Returns:

  • (Symbol)

    The rendering stage (e.g., :load).



115
116
117
# File 'lib/sourcerer/templating.rb', line 115

def stage
  @stage
end

#strictBoolean (readonly)

Returns Whether to use strict rendering.

Returns:

  • (Boolean)

    Whether to use strict rendering.



117
118
119
# File 'lib/sourcerer/templating.rb', line 117

def strict
  @strict
end

Class Method Details

.from_schema(schema_fragment) ⇒ Context

Creates a new Context object from a schema fragment.

Parameters:

  • schema_fragment (Hash)

    The schema fragment containing templating info.

Returns:

  • (Context)

    The new Context object.



133
134
135
136
137
138
139
140
141
# File 'lib/sourcerer/templating.rb', line 133

def self.from_schema schema_fragment
  render_conf = schema_fragment['templating'] || {}

  stage  = (render_conf['stage'] || :load).to_sym
  strict = render_conf['strict'] == true
  scopes = (render_conf['scopes'] || {}).transform_keys(&:to_sym)

  new(stage: stage, strict: strict, scopes: scopes)
end

Instance Method Details

#merged_scopeHash

Merges all scopes into a single hash.

Returns:

  • (Hash)

    The merged scope.



145
146
147
# File 'lib/sourcerer/templating.rb', line 145

def merged_scope
  scopes.values.reduce({}) { |acc, s| acc.merge(s) }
end