Class: Lifer::Brain

Inherits:
Object
  • Object
show all
Defined in:
lib/lifer/brain.rb

Overview

The brain is the object that keeps track of all essential information about the current Lifer project. Usually this information will be consumed via the ‘Lifer` module methods.

Constant Summary collapse

DEFAULT_CONFIG_FILE_URI =

The default configuration file URI.

".config/lifer.yaml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



14
15
16
# File 'lib/lifer/brain.rb', line 14

def root
  @root
end

Class Method Details

.init(root: Dir.pwd, config_file: nil) ⇒ Lifer::Brain

The preferred initializer for the single ‘Lifer::Brain` object that represents the user’s Lifer project.

Parameters:

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

    The root Lifer project directory.

  • config_file (String) (defaults to: nil)

    A path to the correct Lifer config file. If left empty, the brain uses the one at the default path or the one bundled with the gem.

Returns:

  • (Lifer::Brain)

    The brain object for the current Lifer project.



25
# File 'lib/lifer/brain.rb', line 25

def init(root: Dir.pwd, config_file: nil) = new(root:, config_file:)

Instance Method Details

#asset_manifestSet<Lifer::Asset>

The asset manifest tracks the unique assets added to the project as they are added. The writer methods for this instanc evariable is used internally by Lifer when adding new assets.

Returns:



33
# File 'lib/lifer/brain.rb', line 33

def asset_manifest = (@asset_manifest ||= Set.new)

#author_manifestSet<Lifer::Author>

The author manifest tracks the unique authors added to the project as they are added. The writer method for this instance variable is used internally by Lifer when adding new authors.

Returns:



47
# File 'lib/lifer/brain.rb', line 47

def author_manifest = (@author_manifest ||= Set.new)

#authorsArray<Lifer::Author>

Given the author manifest, this returns an array of all authors for the current project. This method is preferrable for accessing and querying for authors.

Returns:



40
# File 'lib/lifer/brain.rb', line 40

def authors = author_manifest.to_a

#build!(environment: :build) ⇒ void

This method returns an undefined value.

Destroy any existing build output and then build the Lifer project with all configured ‘Lifer::Builder`s.

Parameters:

  • environment (Symbol) (defaults to: :build)

    The current Lifer environment.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lifer/brain.rb', line 55

def build!(environment: :build)
  brainwash!

  prebuild_steps =
    case setting(:global, :prebuild)
    when Array, NilClass then setting(:global, :prebuild)
    when Hash then setting(:global, :prebuild, environment)
    end

  builder_list =
    case setting(:global, :build)
    when Array, NilClass then setting(:global, :build)
    when Hash then setting(:global, :build, environment)
    end

  Lifer::Builder.prebuild!(*prebuild_steps, root:)
  Lifer::Builder.build!(*builder_list, root:)
end

#collectionsArray<Lifer::Collection>

Returns all collections and selections within the Lifer root.

Collections only exist if they’re explicitly configured in a configuration file and they match a subdirectory within the root.

Selections, on the other hand, reorganize entries from literal collections. For example, a user could collect all of their entries that were authored by Harry B. Cutler.

Every Lifer build contains at least one collection. (That collection is ‘:root`.)

Returns:



88
89
90
# File 'lib/lifer/brain.rb', line 88

def collections
  @collections ||= (generate_collections + generate_selections).to_a
end

#configLifer::Config

Returns the Lifer project’s configuration object.

Returns:



95
# File 'lib/lifer/brain.rb', line 95

def config = (@config ||= Lifer::Config.build file: config_file_location)

#entry_manifestSet<Lifer::Entry>

Allows for getting the entry manifest or shovelling new entries to the entry manifest.

Returns:



101
# File 'lib/lifer/brain.rb', line 101

def entry_manifest = (@entry_manifest ||= Set.new)

#output_directoryString

Returns the build directory for the Lifer project’s build output.

Returns:

  • (String)

    The Lifer build directory.



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lifer/brain.rb', line 106

def output_directory
  @output_directory ||=
    begin
      dir = "%s/%s" % [root, setting(:global, :output_directory)]

      return Pathname(dir) if Dir.exist? dir

      Dir.mkdir(dir)
      Pathname(dir)
    end
end

#require_user_provided_ruby_files!void

This method returns an undefined value.

The user can bring their own Ruby files to be read by Lifer. This ensures they are loaded before the build starts.

Note that the user’s Bundler path may be in scope, so we need to skip those Ruby files.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lifer/brain.rb', line 125

def require_user_provided_ruby_files!
  return if root.include? Lifer.gem_root

  rb_files = Dir.glob("#{root}/**/*.rb", File::FNM_DOTMATCH)

  if Bundler.bundle_path.to_s.include? root
    rb_files -=
      Dir.glob("#{Bundler.bundle_path}/**/*.rb", File::FNM_DOTMATCH)
  end

  rb_files.each do |rb_file|
    load rb_file
  end
end

#setting(path, ..., collection: nil, strict: false) ⇒ Array, String

Given the tree of a setting name, and the setting scope, returns the setting value. If the in-scope collection does not have a configured setting, this method will return fallback settings (unless ‘:strict` is `true`).

Examples:

Usage:

setting(:my, :great, :setting)

Returns The value of the requested setting.

Parameters:

  • name (Symbol)

    A key in the tree to a setting value.

  • ... (Symbol)

    Any additional keys in the tree.

  • collection (Lifer::Collection) (defaults to: nil)

    The collection to scope the result to.

  • strict (boolean) (defaults to: false)

    If true, do not return fallback setting values.

Returns:

  • (Array, String)

    The value of the requested setting.



154
155
156
# File 'lib/lifer/brain.rb', line 154

def setting(*name, collection: nil, strict: false)
  config.setting *name, collection_name: collection&.name, strict: strict
end

#tag_manifestSet<Lifer::Tag>

The tag manifest tracks the unique tags added to the project as they’re added. The writer method for this instance variable is used internally by Lifer when adding new tags.

Returns:



169
# File 'lib/lifer/brain.rb', line 169

def tag_manifest = (@tag_manifest ||= Set.new)

#tagsArray<Lifer::Tag>

Given the tag manifest, this returns an array of all tags for the current project. This method is preferrable for accessing and querying for tags.

Returns:



162
# File 'lib/lifer/brain.rb', line 162

def tags = tag_manifest.to_a