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

#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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lifer/brain.rb', line 34

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:



67
68
69
# File 'lib/lifer/brain.rb', line 67

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

#configLifer::Config

Returns the Lifer project’s configuration object.

Returns:



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

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

#entry_manifestSet<Lifer::Entry>

Returns all entries that have been added to the manifest. If all is working as intended, this should be every entry ever generated.

Returns:



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

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

#manifestSet<Lifer::Entry>

A manifest of all Lifer project entries.

Returns:



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

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

#output_directoryString

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

Returns:

  • (String)

    The Lifer build directory.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lifer/brain.rb', line 90

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.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lifer/brain.rb', line 109

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.



138
139
140
# File 'lib/lifer/brain.rb', line 138

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:



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

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:



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

def tags = tag_manifest.to_a