Class: RVGP::Application::Config

Inherits:
Object
  • Object
show all
Includes:
Pta::AvailabilityHelper
Defined in:
lib/rvgp/application/config.rb

Overview

This class provides the app configuration options accessors and parsing logic.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pta::AvailabilityHelper

#hledger, #ledger, #pta

Constructor Details

#initialize(project_path) ⇒ Config

Given the provided project path, this object will parse and store the config/rvgp.yaml, as well as provide default values for otherwise unspecified attributes in this file.

Parameters:

  • project_path (String)

    The path, to an RVGP project directory.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rvgp/application/config.rb', line 16

def initialize(project_path)
  @project_path = project_path
  @build_path = format('%s/build', project_path)

  config_path = project_path 'config/rvgp.yml'
  @yaml = RVGP::Utilities::Yaml.new config_path, project_path if File.exist? config_path

  RVGP::Pta.pta_adapter = @yaml[:pta_adapter].to_sym if @yaml.key? :pta_adapter

  @prices_path = @yaml.key?(:prices_path) ? @yaml[:prices_path] : project_path('journals/prices.db')

  if @yaml.key?(:project_journal_path)
    @project_journal_path = project_path @yaml[:project_journal_path]
  else
    journals_in_project_path = Dir.glob format('%s/*.journal', @project_path)
    if journals_in_project_path.length != 1
      raise StandardError,
            format(
              'Unable to automatically determine the project journal. Probably you want one of these ' \
              'files: %<files>s. Set the project_journal_path parameter in ' \
              'your config file, to the relative pathname, of the project journal.',
              files: journals_in_project_path.join(', ')
            )
    end
    @project_journal_path = journals_in_project_path.first
  end

  # I'm not crazy about this default.. Mabe we should raise an error if
  # this value isn't set...
  @grid_starting_at = @yaml[:grid_starting_at] if @yaml.key? :grid_starting_at
  @grid_starting_at ||= default_grid_starting_at

  # NOTE: pta_adapter.newest_transaction_date.year works in lieu of Date.today,
  #       but that query takes time. (and it requires that we've already
  #       performed a build step at the time it's called) so, we use
  #       Date.today instead.
  @grid_ending_at = @yaml[:grid_ending_at] if @yaml.key? :grid_ending_at
  @grid_ending_at ||= default_grid_ending_at
end

Instance Attribute Details

#prices_pathObject (readonly)

Returns the value of attribute prices_path.



10
11
12
# File 'lib/rvgp/application/config.rb', line 10

def prices_path
  @prices_path
end

#project_journal_pathObject (readonly)

Returns the value of attribute project_journal_path.



10
11
12
# File 'lib/rvgp/application/config.rb', line 10

def project_journal_path
  @project_journal_path
end

Instance Method Details

#[](attr) ⇒ Object

Return the contents of the provided attr, from the project’s config/rvgp.yaml

Returns:

  • (Object)

    the value corresponding to the provided attr



58
59
60
# File 'lib/rvgp/application/config.rb', line 58

def [](attr)
  @yaml[attr]
end

#build_path(relpath = nil) ⇒ String

Returns the full path, to a file or directory, in the project’s build/ directory. If no relpath was provided, this method returns the full path to the project build directory.

Parameters:

  • relpath (optional, String) (defaults to: nil)

    The relative path, to a filesystem object in the current project

Returns:

  • (String)

    The full path to the requested resource



99
100
101
# File 'lib/rvgp/application/config.rb', line 99

def build_path(relpath = nil)
  relpath ? [@build_path, relpath].join('/') : @build_path
end

#grid_ending_atDate

Returns the ending date, for all grids that will be generated in this project.

Returns:

  • (Date)

    when to finish grid building



77
78
79
# File 'lib/rvgp/application/config.rb', line 77

def grid_ending_at
  call_or_return_date @grid_ending_at
end

#grid_starting_atDate

Returns the starting date, for all grids that will be generated in this project.

Returns:

  • (Date)

    when to commence grid building



71
72
73
# File 'lib/rvgp/application/config.rb', line 71

def grid_starting_at
  call_or_return_date @grid_starting_at
end

#grid_yearsArray<Integer>

The years, for which we will be building grids

Returns:

  • (Array<Integer>)

    What years to expect in our build/grids directory (and their downstream targets)



83
84
85
# File 'lib/rvgp/application/config.rb', line 83

def grid_years
  grid_starting_at.year.upto(grid_ending_at.year)
end

#key?(attr) ⇒ TrueClass, FalseClass

Returns a boolean indicating whether a value for the provided attr was specified in the project’s config/rvgp.yaml

Returns:

  • (TrueClass, FalseClass)

    whether the key was specified



65
66
67
# File 'lib/rvgp/application/config.rb', line 65

def key?(attr)
  @yaml.key? attr
end

#project_path(relpath = nil) ⇒ String

Returns the full path, to a file or directory, in the project. If no relpath was provided, this method returns the full path to the project directory.

Parameters:

  • relpath (optional, String) (defaults to: nil)

    The relative path, to a filesystem object in the current project

Returns:

  • (String)

    The full path to the requested resource



91
92
93
# File 'lib/rvgp/application/config.rb', line 91

def project_path(relpath = nil)
  relpath ? [@project_path, relpath].join('/') : @project_path
end