Class: Lifer::Brain
- Inherits:
-
Object
- Object
- Lifer::Brain
- 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
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#build!(environment: :build) ⇒ void
Destroy any existing build output and then build the Lifer project with all configured ‘Lifer::Builder`s.
-
#collections ⇒ Array<Lifer::Collection>
Returns all collections and selections within the Lifer root.
-
#config ⇒ Lifer::Config
Returns the Lifer project’s configuration object.
-
#entry_manifest ⇒ Set<Lifer::Entry>
Returns all entries that have been added to the manifest.
-
#manifest ⇒ Set<Lifer::Entry>
A manifest of all Lifer project entries.
-
#output_directory ⇒ String
Returns the build directory for the Lifer project’s build output.
-
#require_user_provided_ruby_files! ⇒ void
The user can bring their own Ruby files to be read by Lifer.
-
#setting(path, ..., collection: nil, strict: false) ⇒ Array, String
Given the tree of a setting name, and the setting scope, returns the setting value.
-
#tag_manifest ⇒ Set<Lifer::Tag>
The tag manifest tracks the unique tags added to the project as they’re added.
-
#tags ⇒ Array<Lifer::Tag>
Given the tag manifest, this returns an array of all tags for the current project.
Instance Attribute Details
#root ⇒ Object (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.
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.
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 |
#collections ⇒ Array<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`.)
67 68 69 |
# File 'lib/lifer/brain.rb', line 67 def collections @collections ||= (generate_collections + generate_selections).to_a end |
#config ⇒ Lifer::Config
Returns the Lifer project’s configuration object.
74 |
# File 'lib/lifer/brain.rb', line 74 def config = (@config ||= Lifer::Config.build file: config_file_location) |
#entry_manifest ⇒ Set<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.
80 |
# File 'lib/lifer/brain.rb', line 80 def entry_manifest = (@entry_manifest ||= Set.new) |
#manifest ⇒ Set<Lifer::Entry>
A manifest of all Lifer project entries.
85 |
# File 'lib/lifer/brain.rb', line 85 def manifest = (@manifest ||= Set.new) |
#output_directory ⇒ String
Returns the build directory for the Lifer project’s build output.
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`).
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_manifest ⇒ Set<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.
153 |
# File 'lib/lifer/brain.rb', line 153 def tag_manifest = (@tag_manifest ||= Set.new) |
#tags ⇒ Array<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.
146 |
# File 'lib/lifer/brain.rb', line 146 def = tag_manifest.to_a |