Class: Brandish::Configure

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/brandish/configure.rb,
lib/brandish/configure/dsl.rb,
lib/brandish/configure/form.rb,
lib/brandish/configure/dsl/form.rb

Overview

This provides a central location for all configuration options. For the DSL, see DSL.

Defined Under Namespace

Classes: DSL, Form

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = Dir.pwd) ⇒ Configure

Initializes the configure instance.

Parameters:

  • root (::String, ::Pathname) (defaults to: Dir.pwd)

    The root for the project. This is used to determine the correct paths for the output directory, the sources directory, and the templates directory.



82
83
84
85
86
87
# File 'lib/brandish/configure.rb', line 82

def initialize(root = Dir.pwd)
  root = ::Pathname.new(root)
  @options = { root: root, sources: PathSet.new, templates: PathSet.new }
  @forms = ::Set.new
  default_paths
end

Instance Attribute Details

#formsSet<Configure::Form> (readonly)

The forms that are defined on this configuration instance.

Returns:



29
30
31
# File 'lib/brandish/configure.rb', line 29

def forms
  @forms
end

#options{::Symbol => ::Object} (readonly)

The options. These can be anything; predefined options are :root, :source, :output, and :templates; for more information on those, see #root, #sources, #output, and #templates for more information. These options should not be accessed directly, but rather through their accessors. Other options must be accessed through this.

Returns:

  • ({::Symbol => ::Object})


24
25
26
# File 'lib/brandish/configure.rb', line 24

def options
  @options
end

Instance Method Details

#[](key) ⇒ ::Object

Sets a key on the options. This gets an option that is used for all processors on the options.

Parameters:

  • key (::Symbol, ::String)

    The key.

Returns:

  • (::Object)


75
# File 'lib/brandish/configure.rb', line 75

delegate [:[], :fetch] => :options

#[]=(key, value) ⇒ ::Object

Sets a key on the options. This sets an option that is used for all processors on the options.

Parameters:

  • key (::Symbol, ::String)

    The key.

  • value (::Object)

    The value.

Returns:

  • (::Object)


75
# File 'lib/brandish/configure.rb', line 75

delegate [:[], :fetch] => :options

#build(which = :all) {|build| ... } ⇒ void

This method returns an undefined value.

Given a set of forms to build, it yields blocks that can be called to build a form.

Parameters:

  • which (::Symbol, <::Symbol>) (defaults to: :all)

    If this is :all, all of the forms available are built; otherwise, it only builds the forms whose names are listed.

Yields:

  • (build)

    Yields for each form that can be built.

Yield Parameters:

  • build (::Proc<void>)

    A block that can be called to build a form.



132
133
134
135
# File 'lib/brandish/configure.rb', line 132

def build(which = :all)
  return to_enum(:build, which) unless block_given?
  select_forms(which).each { |f| yield proc { f.build(self) } }
end

#build!(which = :all) {|build| ... } ⇒ void

This method returns an undefined value.

Given a set of forms to build, it yields blocks that can be called to build a form.

This first clears the cache for file nodes.

Parameters:

  • which (::Symbol, <::Symbol>) (defaults to: :all)

    If this is :all, all of the forms available are built; otherwise, it only builds the forms whose names are listed.

Yields:

  • (build)

    Yields for each form that can be built.

Yield Parameters:

  • build (::Proc<void>)

    A block that can be called to build a form.



149
150
151
152
153
# File 'lib/brandish/configure.rb', line 149

def build!(which = :all)
  return to_enum(:build!, which) unless block_given?
  @_roots = nil
  select_forms(which).each { |f| yield proc { f.build(self) } }
end

#fetch(key) ⇒ ::Object

Fetches a value at the given key, or provides a default if the key doesn't exist. If both a block and a default argument are given, the block form takes precedence.

Attempts to retrieve a value at the given key. If there is no key-value pair at the given key, it raises an error.

Parameters:

  • key (::Symbol, ::String)

    The key.

Returns:

  • (::Object)

    The value.

Raises:

  • (KeyError)

    if the key isn't on the options.



75
# File 'lib/brandish/configure.rb', line 75

delegate [:[], :fetch] => :options

#output::Pathname

Retrieves the output path. This is where the outputs for all of the forms should be located.

Returns:

  • (::Pathname)


102
103
104
# File 'lib/brandish/configure.rb', line 102

def output
  fetch(:output) { root / "output" }
end

#parse_from(path, short = path.relative_path_from(root)) ⇒ Parser::Root

Parses a file. This bypasses the cache.

Parameters:

  • path (::Pathname)

    The path to the actual file. This should respond to #read. If this isn't a pathname, the short should be provided.

  • short (::String) (defaults to: path.relative_path_from(root))

    The short name of the file. This is used for location information.

Returns:

  • (Parser::Root)


174
175
176
# File 'lib/brandish/configure.rb', line 174

def parse_from(path, short = path.relative_path_from(root))
  Parser.new(Scanner.new(path.read, short, options).call).call
end

#root::Pathname

Retrieves the root path. This is where all of the other directories should be located, and where the configuration file should be located.

Returns:

  • (::Pathname)


94
95
96
# File 'lib/brandish/configure.rb', line 94

def root
  fetch(:root)
end

#roots{::Pathname => Parser::Root}

A cache for all of the root nodes. This is a regular hash; however, upon attempt to access an item that isn't already in the hash, it first parses the file at that item, and stores the result in the hash, returning the root node in the file. This is to cache files so that they do not get reparsed multiple times.

Returns:

  • ({::Pathname => Parser::Root})


162
163
164
# File 'lib/brandish/configure.rb', line 162

def roots
  @_roots ||= ::Hash.new { |h, k| h[k] = parse_from(k) }
end

#sourcesPathSet

Retrieves the source path. This is where the sources for all of the documents in the Brandish project are located.

Returns:



110
111
112
# File 'lib/brandish/configure.rb', line 110

def sources
  fetch(:sources)
end

#templatesPathSet

Retrieves the templates path. This is where all of the templates for all of the forms should be located.

Returns:



118
119
120
# File 'lib/brandish/configure.rb', line 118

def templates
  fetch(:templates)
end