Class: Dsu::Models::Configuration

Inherits:
Crud::JsonFile show all
Includes:
Support::Fileable, Support::Presentable
Defined in:
lib/dsu/models/configuration.rb

Overview

This class represents the dsu configuration.

Constant Summary collapse

VERSION =
Migration::VERSION
DEFAULT_CONFIGURATION =
{
  version: VERSION,
  # The default editor to use when editing entry groups if the EDITOR
  # environment variable on your system is not set. On nix systmes,
  # the default editor is`nano`. You need to change this default on
  # Windows systems.
  editor: 'nano',
  # The order by which entries should be displayed by default:
  # :asc or :desc, ascending or descending, respectively.
  entries_display_order: :desc,
  carry_over_entries_to_today: false,
  # If true, when using dsu commands that list date ranges (e.g.
  # `dsu list dates`), the displayed list will include dates that
  # have no dsu entries. If false, the displayed list will only
  # include dates that have dsu entries.
  # For all other `dsu list` commands, if true, this option will
  # behave in the aforementioned manner. If false, the displayed
  # list will unconditionally display the first and last dates
  # regardless of whether or not the DSU date has entries or not;
  # all other dates will not be displayed if the DSU date has no
  # entries.
  include_all: false,
  # Themes
  # The currently selected color theme. Should be equal to
  # Models::ColorTheme::DEFAULT_THEME_NAME or the name of a custom
  # theme (with the same file name) that resides in the themes_folder.
  theme_name: 'default'
}.freeze

Constants included from Support::Fileable

Support::Fileable::MIGRATION_VERSION_FILE_NAME

Instance Attribute Summary collapse

Attributes inherited from Crud::JsonFile

#file_path

Instance Method Summary collapse

Methods included from Support::Presentable

#presenter

Methods included from Support::Fileable

#backup_folder, #config_file_name, #config_folder, #config_path, #dsu_folder, #entries_file_name, #entries_folder, #entries_path, #gem_dir, #migration_version_folder, #migration_version_path, #root_folder, #seed_data_folder, #temp_folder, #theme_file_name, #themes_folder, #themes_path

Methods inherited from Crud::JsonFile

#delete, delete, #delete!, delete!, file_does_not_exist_message, #file_exist?, file_exist?, parse, #persisted?, read, read!, #save, #save!, #to_model, #write, write, #write!, write!

Constructor Details

#initialize(options: {}) ⇒ Configuration

Returns a new instance of Configuration.



68
69
70
71
72
73
74
75
76
77
# File 'lib/dsu/models/configuration.rb', line 68

def initialize(options: {})
  super(config_path)

  FileUtils.mkdir_p config_folder

  @options = options || {}
  reload

  write! unless exist?
end

Instance Attribute Details

#carry_over_entries_to_todayObject

Returns the value of attribute carry_over_entries_to_today.



57
58
59
# File 'lib/dsu/models/configuration.rb', line 57

def carry_over_entries_to_today
  @carry_over_entries_to_today
end

#editorObject

Returns the value of attribute editor.



57
58
59
# File 'lib/dsu/models/configuration.rb', line 57

def editor
  @editor
end

#entries_display_orderObject

Returns the value of attribute entries_display_order.



57
58
59
# File 'lib/dsu/models/configuration.rb', line 57

def entries_display_order
  @entries_display_order
end

#include_allObject

Returns the value of attribute include_all.



57
58
59
# File 'lib/dsu/models/configuration.rb', line 57

def include_all
  @include_all
end

#optionsObject (readonly)

Returns the value of attribute options.



64
65
66
# File 'lib/dsu/models/configuration.rb', line 64

def options
  @options
end

#theme_nameObject

Returns the value of attribute theme_name.



57
58
59
# File 'lib/dsu/models/configuration.rb', line 57

def theme_name
  @theme_name
end

#versionObject

Returns the value of attribute version.



57
58
59
# File 'lib/dsu/models/configuration.rb', line 57

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Override == and hash so that we can compare objects based on attributes alone. This is also useful for comparing objects in an array, for example.



124
125
126
127
128
# File 'lib/dsu/models/configuration.rb', line 124

def ==(other)
  return false unless other.is_a?(Configuration)

  to_h == other.to_h
end

#carry_over_entries_to_today?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/dsu/models/configuration.rb', line 106

def carry_over_entries_to_today?
  carry_over_entries_to_today
end

#hashObject



131
132
133
134
135
# File 'lib/dsu/models/configuration.rb', line 131

def hash
  DEFAULT_CONFIGURATION.each_key.map do |key|
    public_send(key)
  end.hash
end

#merge(hash) ⇒ Object



137
138
139
140
# File 'lib/dsu/models/configuration.rb', line 137

def merge(hash)
  hash.transform_keys!(&:to_sym)
  replace!(config_hash: to_h.merge(hash))
end

#reloadObject

Restores the configuration to its original state from disk.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dsu/models/configuration.rb', line 91

def reload
  file_hash = if exist?
    read do |config_hash|
      hydrated_hash = Services::Configuration::HydratorService.new(config_hash: config_hash).call
      config_hash.merge!(hydrated_hash)
    end
  else
    DEFAULT_CONFIGURATION.dup
  end

  assign_attributes_from file_hash

  self
end

#replace!(config_hash: {}) ⇒ Object

Temporarily sets the configuration to the given config_hash. To reset the configuration to its original state, call #reload

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
# File 'lib/dsu/models/configuration.rb', line 81

def replace!(config_hash: {})
  raise ArgumentError, 'config_hash is nil.' if config_hash.nil?
  raise ArgumentError, "config_hash must be a Hash: \"#{config_hash}\"." unless config_hash.is_a?(Hash)

  assign_attributes_from config_hash.dup

  self
end

#to_hObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/dsu/models/configuration.rb', line 110

def to_h
  {
    version: version,
    editor: editor,
    entries_display_order: entries_display_order,
    carry_over_entries_to_today: carry_over_entries_to_today,
    include_all: include_all,
    theme_name: theme_name
  }
end