Class: Nanoc::Spec::HelperContext

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ HelperContext

Returns a new instance of HelperContext.

Parameters:

  • mod (Module)

    The helper module to create a context for



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nanoc/spec.rb', line 54

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)
  @compiled_content_repo = Nanoc::Core::CompiledContentRepo.new
  @action_provider = new_action_provider
end

Instance Attribute Details

#erboutObject (readonly)

Returns the value of attribute erbout.



51
52
53
# File 'lib/nanoc/spec.rb', line 51

def erbout
  @erbout
end

Instance Method Details

#action_sequence_for(obj) ⇒ Object



150
151
152
# File 'lib/nanoc/spec.rb', line 150

def action_sequence_for(obj)
  @action_sequence.fetch(obj, [])
end

#assignsObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/nanoc/spec.rb', line 162

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

#compiled_content_repoObject



158
159
160
# File 'lib/nanoc/spec.rb', line 158

def compiled_content_repo
  view_context.compiled_content_repo
end

#configNanoc::Core::MutableConfigView

Returns:

  • (Nanoc::Core::MutableConfigView)


126
127
128
# File 'lib/nanoc/spec.rb', line 126

def config
  assigns[:config]
end

#create_item(content, attributes, identifier) ⇒ Nanoc::Core::CompilationItemView

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

Parameters:

  • content (String)

    The uncompiled item content

  • attributes (Hash)

    A hash containing this item's attributes

  • identifier (Nanoc::Core::Identifier, String)

    This item's identifier

Returns:

  • (Nanoc::Core::CompilationItemView)

    A view for the newly created item



76
77
78
79
80
# File 'lib/nanoc/spec.rb', line 76

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) ⇒ Nanoc::Core::CompilationItemView

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

Parameters:

  • content (String)

    The raw layout content

  • attributes (Hash)

    A hash containing this layout's attributes

  • identifier (Nanoc::Core::Identifier, String)

    This layout's identifier

Returns:

  • (Nanoc::Core::CompilationItemView)

    A view for the newly created layout



91
92
93
94
95
# File 'lib/nanoc/spec.rb', line 91

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) ⇒ Object

Creates a new representation for the given item.

Parameters:

  • item (Nanoc::Core::CompilationItemView)

    The item to create a represetation for

  • path (String)

    The path of the :last snapshot of this item representation

  • rep (Symbol) (defaults to: :default)

    The rep name to create



103
104
105
106
107
108
# File 'lib/nanoc/spec.rb', line 103

def create_rep(item, path, rep = :default)
  rep = Nanoc::Core::ItemRep.new(item._unwrap, rep)
  rep.paths[:last] = [path]
  @reps << rep
  self
end

#dependency_storeObject



173
174
175
# File 'lib/nanoc/spec.rb', line 173

def dependency_store
  @_dependency_store ||= Nanoc::Core::DependencyStore.new(@items, @layouts, @config)
end

#dependency_trackerObject



177
178
179
180
181
182
# File 'lib/nanoc/spec.rb', line 177

def dependency_tracker
  @_dependency_tracker ||= Nanoc::Core::DependencyTracker.new(
    dependency_store,
    root: Nanoc::Core::Item.new('root', {}, '/root.md'),
  )
end

#helperObject

Returns An object that includes the helper functions.

Returns:

  • (Object)

    An object that includes the helper functions



111
112
113
114
115
# File 'lib/nanoc/spec.rb', line 111

def helper
  mod = @mod
  klass = Class.new(Nanoc::Core::Context) { include mod }
  klass.new(assigns)
end

#itemNanoc::Core::CompilationItemView?

Returns:

  • (Nanoc::Core::CompilationItemView, nil)


131
132
133
# File 'lib/nanoc/spec.rb', line 131

def item
  assigns[:item]
end

#item=(item) ⇒ Object



117
118
119
# File 'lib/nanoc/spec.rb', line 117

def item=(item)
  @item = item&._unwrap
end

#item_repNanoc::Core::BasicItemRepView?

Returns:

  • (Nanoc::Core::BasicItemRepView, nil)


136
137
138
# File 'lib/nanoc/spec.rb', line 136

def item_rep
  assigns[:item_rep]
end

#item_rep=(item_rep) ⇒ Object



121
122
123
# File 'lib/nanoc/spec.rb', line 121

def item_rep=(item_rep)
  @item_rep = item_rep&._unwrap
end

#itemsNanoc::Core::ItemCollectionWithRepsView

Returns:

  • (Nanoc::Core::ItemCollectionWithRepsView)


141
142
143
# File 'lib/nanoc/spec.rb', line 141

def items
  assigns[:items]
end

#layoutsNanoc::Core::LayoutCollectionView

Returns:

  • (Nanoc::Core::LayoutCollectionView)


146
147
148
# File 'lib/nanoc/spec.rb', line 146

def layouts
  assigns[:layouts]
end

#update_action_sequence(obj, memory) ⇒ Object



154
155
156
# File 'lib/nanoc/spec.rb', line 154

def update_action_sequence(obj, memory)
  @action_sequence[obj] = memory
end