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',
  # The default project to use.
  default_project: 'default'
}.freeze

Constants included from Support::Fileable

Support::Fileable::MIGRATION_VERSION_FILE_NAME

Instance Attribute Summary collapse

Attributes inherited from Crud::JsonFile

#file_path

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Presentable

#presenter

Methods included from Support::Fileable

#backup_folder_for, #config_file_name, #config_folder, #config_path, #current_project_file, #current_project_file_name, #dsu_folder, #entries_file_name, #entries_folder, #entries_path, #gem_dir, #migration_version_folder, #migration_version_path, #project_file_for, #project_folder_for, #projects_folder, #root_folder, #seed_data_dsu_configuration_for, #seed_data_dsu_folder_for, #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, #update_version!, #write, write, #write!, write!

Constructor Details

#initialize(options: {}) ⇒ Configuration

Returns a new instance of Configuration.



73
74
75
76
77
78
79
80
81
82
# File 'lib/dsu/models/configuration.rb', line 73

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.



61
62
63
# File 'lib/dsu/models/configuration.rb', line 61

def carry_over_entries_to_today
  @carry_over_entries_to_today
end

#default_projectObject

Returns the value of attribute default_project.



61
62
63
# File 'lib/dsu/models/configuration.rb', line 61

def default_project
  @default_project
end

#editorObject

Returns the value of attribute editor.



61
62
63
# File 'lib/dsu/models/configuration.rb', line 61

def editor
  @editor
end

#entries_display_orderObject

Returns the value of attribute entries_display_order.



61
62
63
# File 'lib/dsu/models/configuration.rb', line 61

def entries_display_order
  @entries_display_order
end

#include_allObject

Returns the value of attribute include_all.



61
62
63
# File 'lib/dsu/models/configuration.rb', line 61

def include_all
  @include_all
end

#optionsObject (readonly)

Returns the value of attribute options.



69
70
71
# File 'lib/dsu/models/configuration.rb', line 69

def options
  @options
end

#theme_nameObject

Returns the value of attribute theme_name.



61
62
63
# File 'lib/dsu/models/configuration.rb', line 61

def theme_name
  @theme_name
end

#versionObject

Returns the value of attribute version.



61
62
63
# File 'lib/dsu/models/configuration.rb', line 61

def version
  @version
end

Class Method Details

.exist?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/dsu/models/configuration.rb', line 85

def exist?
  File.exist?(Support::Fileable.config_path)
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.



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

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

  to_h == other.to_h
end

#carry_over_entries_to_today?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/dsu/models/configuration.rb', line 117

def carry_over_entries_to_today?
  carry_over_entries_to_today
end

#hashObject



143
144
145
146
147
# File 'lib/dsu/models/configuration.rb', line 143

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

#merge(hash) ⇒ Object



149
150
151
152
# File 'lib/dsu/models/configuration.rb', line 149

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.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dsu/models/configuration.rb', line 102

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)


92
93
94
95
96
97
98
99
# File 'lib/dsu/models/configuration.rb', line 92

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



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dsu/models/configuration.rb', line 121

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,
    default_project: default_project
  }
end