Class: Belajar::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/belajar/configuration.rb

Constant Summary collapse

LOCAL_DIR =
'.belajar'.freeze
COURSES_DIR =
'courses'.freeze
SOLUTIONS_DIR =
'solutions'.freeze
STORAGE_FILE =
'belajar.db.yml'.freeze
BELAJAR_INITIAL_COURSE =
'wong-bejo/Belajar_Ruby'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



17
18
19
20
21
22
23
24
25
26
# File 'lib/belajar/configuration.rb', line 17

def initialize
  @courses_path = local_path_to(COURSES_DIR)
  @storage_file = local_path_to(STORAGE_FILE)

  QuickStore.configure do |config|
    config.file_path = @storage_file
  end

  yield if block_given?
end

Instance Attribute Details

#courses_pathObject

Returns the value of attribute courses_path.



14
15
16
# File 'lib/belajar/configuration.rb', line 14

def courses_path
  @courses_path
end

#storage_fileObject (readonly)

Returns the value of attribute storage_file.



15
16
17
# File 'lib/belajar/configuration.rb', line 15

def storage_file
  @storage_file
end

Instance Method Details

#importObject



57
58
59
60
61
62
# File 'lib/belajar/configuration.rb', line 57

def import
  store           = QuickStore.store
  @courses_path   = store.courses_path || @courses_path
  @solutions_path = store.solutions_path || @solutions_path
  self
end

#initial_courseObject



75
76
77
# File 'lib/belajar/configuration.rb', line 75

def initial_course
  BELAJAR_INITIAL_COURSE
end

#saveObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/belajar/configuration.rb', line 46

def save
  settings = instance_variables
  settings.delete(:@storage_file)

  settings.each do |variable|
    key   = variable.to_s.delete('@')
    value = instance_variable_get(variable.to_sym)
    QuickStore.store.set(key, value)
  end
end

#solutions_pathObject



28
29
30
31
# File 'lib/belajar/configuration.rb', line 28

def solutions_path
  @solutions_path ||
    raise(Belajar::ConfigurationError, 'Solutions path is not set.')
end

#solutions_path=(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/belajar/configuration.rb', line 33

def solutions_path=(path)
  full_path = File.expand_path(path, Dir.pwd)

  unless Dir.exist?(full_path)
    raise(
      Belajar::ConfigurationError,
      "Solutions path \"#{path}\" isn’t an existing directory."
    )
  end

  @solutions_path = full_path
end

#summaryObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/belajar/configuration.rb', line 64

def summary
  settings = instance_variables
  settings.delete(:@storage_file)

  settings.reduce('') do |lines, variable|
    key   = variable.to_s.delete('@').tr('_', ' ')
    value = instance_variable_get(variable.to_sym)
    lines + "* #{key}: #{value}\n"
  end
end