class Nanoc::Spec::HelperContext

Attributes

dependency_tracker[R]

@return [Nanoc::Core::DependencyTracker]

erbout[R]

Public Class Methods

new(mod) click to toggle source

@param [Module] mod The helper module to create a context for

# File lib/nanoc/spec.rb, line 57
def initialize(mod)
  @mod = mod

  @erbout = +''
  @action_sequence = {}
  @config = Nanoc::Core::Configuration.new(dir: Dir.getwd).with_defaults
  @reps = Nanoc::Core::ItemRepRepo.new
  @items = Nanoc::Core::ItemCollection.new(@config)
  @layouts = Nanoc::Core::LayoutCollection.new(@config)
  @dependency_tracker = Nanoc::Core::DependencyTracker.new(Object.new)
  @compiled_content_store = Nanoc::Core::CompiledContentStore.new
  @action_provider = new_action_provider
end

Private Class Methods

for(_context) click to toggle source
# File lib/nanoc/spec.rb, line 189
def self.for(_context)
  raise NotImplementedError
end

Public Instance Methods

action_sequence_for(obj) click to toggle source
# File lib/nanoc/spec.rb, line 154
def action_sequence_for(obj)
  @action_sequence.fetch(obj, [])
end
compiled_content_store() click to toggle source
# File lib/nanoc/spec.rb, line 162
def compiled_content_store
  view_context.compiled_content_store
end
config() click to toggle source

@return [Nanoc::Core::MutableConfigView]

# File lib/nanoc/spec.rb, line 130
def config
  assigns[:config]
end
create_item(content, attributes, identifier) click to toggle source

Creates a new item and adds it to the site’s collection of items.

@param [String] content The uncompiled item content

@param [Hash] attributes A hash containing this item's attributes

@param [Nanoc::Core::Identifier, String] identifier This item's identifier

@return [Nanoc::Core::CompilationItemView] A view for the newly created item

# File lib/nanoc/spec.rb, line 80
def create_item(content, attributes, identifier)
  item = Nanoc::Core::Item.new(content, attributes, identifier)
  @items = @items.add(item)
  self
end
create_layout(content, attributes, identifier) click to toggle source

Creates a new layout and adds it to the site’s collection of layouts.

@param [String] content The raw layout content

@param [Hash] attributes A hash containing this layout's attributes

@param [Nanoc::Core::Identifier, String] identifier This layout's identifier

@return [Nanoc::Core::CompilationItemView] A view for the newly created layout

# File lib/nanoc/spec.rb, line 95
def create_layout(content, attributes, identifier)
  layout = Nanoc::Core::Layout.new(content, attributes, identifier)
  @layouts = @layouts.add(layout)
  self
end
create_rep(item, path, rep = :default) click to toggle source

Creates a new representation for the given item.

@param [Nanoc::Core::CompilationItemView] item The item to create a represetation for

@param [String] path The path of the `:last` snapshot of this item representation @param [Symbol] rep The rep name to create

# File lib/nanoc/spec.rb, line 107
def create_rep(item, path, rep = :default)
  rep = Nanoc::Core::ItemRep.new(item._unwrap, rep)
  rep.paths[:last] = [path]
  @reps << rep
  self
end
helper() click to toggle source

@return [Object] An object that includes the helper functions

# File lib/nanoc/spec.rb, line 115
def helper
  mod = @mod
  klass = Class.new(Nanoc::Core::Context) { include mod }
  klass.new(assigns)
end
item() click to toggle source

@return [Nanoc::Core::CompilationItemView, nil]

# File lib/nanoc/spec.rb, line 135
def item
  assigns[:item]
end
item=(item) click to toggle source
# File lib/nanoc/spec.rb, line 121
def item=(item)
  @item = item ? item._unwrap : nil
end
item_rep() click to toggle source

@return [Nanoc::Core::BasicItemRepView, nil]

# File lib/nanoc/spec.rb, line 140
def item_rep
  assigns[:item_rep]
end
item_rep=(item_rep) click to toggle source
# File lib/nanoc/spec.rb, line 125
def item_rep=(item_rep)
  @item_rep = item_rep ? item_rep._unwrap : nil
end
items() click to toggle source

@return [Nanoc::Core::ItemCollectionWithRepsView]

# File lib/nanoc/spec.rb, line 145
def items
  assigns[:items]
end
layouts() click to toggle source

@return [Nanoc::Core::LayoutCollectionView]

# File lib/nanoc/spec.rb, line 150
def layouts
  assigns[:layouts]
end
update_action_sequence(obj, memory) click to toggle source
# File lib/nanoc/spec.rb, line 158
def update_action_sequence(obj, memory)
  @action_sequence[obj] = memory
end

Private Instance Methods

assigns() click to toggle source
# File lib/nanoc/spec.rb, line 224
def assigns
  {
    config: Nanoc::Core::MutableConfigView.new(@config, view_context),
    item_rep: @item_rep ? Nanoc::Core::CompilationItemRepView.new(@item_rep, view_context) : nil,
    item: @item ? Nanoc::Core::CompilationItemView.new(@item, view_context) : nil,
    items: Nanoc::Core::ItemCollectionWithRepsView.new(@items, view_context),
    layouts: Nanoc::Core::LayoutCollectionView.new(@layouts, view_context),
    _erbout: @erbout,
  }
end
new_action_provider() click to toggle source
# File lib/nanoc/spec.rb, line 187
def new_action_provider
  Class.new(Nanoc::Core::ActionProvider) do
    def self.for(_context)
      raise NotImplementedError
    end

    def initialize(context)
      @context = context
    end

    def rep_names_for(_item)
      [:default]
    end

    def action_sequence_for(obj)
      @context.action_sequence_for(obj)
    end

    def snapshots_defs_for(_rep)
      [Nanoc::Core::SnapshotDef.new(:last, binary: false)]
    end
  end.new(self)
end
new_compiler_for(site) click to toggle source
# File lib/nanoc/spec.rb, line 211
def new_compiler_for(site)
  Nanoc::Core::CompilerLoader.new.load(site, action_provider: @action_provider)
end
rep_names_for(_item) click to toggle source
# File lib/nanoc/spec.rb, line 197
def rep_names_for(_item)
  [:default]
end
site() click to toggle source
# File lib/nanoc/spec.rb, line 215
def site
  @_site ||=
    Nanoc::Core::Site.new(
      config: @config,
      code_snippets: [],
      data_source: Nanoc::Core::InMemoryDataSource.new(@items, @layouts),
    )
end
snapshots_defs_for(_rep) click to toggle source
# File lib/nanoc/spec.rb, line 205
def snapshots_defs_for(_rep)
  [Nanoc::Core::SnapshotDef.new(:last, binary: false)]
end
view_context() click to toggle source
# File lib/nanoc/spec.rb, line 168
def view_context
  compilation_context =
    Nanoc::Core::CompilationContext.new(
      action_provider: @action_provider,
      reps: @reps,
      site: site,
      compiled_content_cache: Nanoc::Core::CompiledContentCache.new(config: @config),
      compiled_content_store: @compiled_content_store,
    )

  Nanoc::Core::ViewContextForCompilation.new(
    reps: @reps,
    items: @items,
    dependency_tracker: @dependency_tracker,
    compilation_context: compilation_context,
    compiled_content_store: @compiled_content_store,
  )
end