Class: Pomodoro::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/pomodoro/config.rb

Defined Under Namespace

Classes: ConfigError, ConfigFileMissingError, MissingKeyError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = CONFIG_PATH) ⇒ Config


35
36
37
# File 'lib/pomodoro/config.rb', line 35

def initialize(path = CONFIG_PATH)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Use Pomodoro.config instead of instantiating a new Config object.


34
35
36
# File 'lib/pomodoro/config.rb', line 34

def path
  @path
end

Instance Method Details

#calendarObject


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pomodoro/config.rb', line 84

def calendar
  (self.data['calendar'] || Hash.new).reduce(Hash.new) do |buffer, (event_name, date)|
    if date.is_a?(Integer)
      today = Date.today
      p_date = Date.new(today.year, today.month, date)
      date = today.day > date ? p_date.next_month : p_date
    else
      date = Date.strptime(date, '%d/%m')
      date = (date < Date.today) ? date.next_year : date
    end
    buffer.merge(event_name => date)
  end
end

#dataObject


39
40
41
42
43
# File 'lib/pomodoro/config.rb', line 39

def data
  @data ||= YAML.load_file(@path)
rescue Errno::ENOENT
  raise ConfigFileMissingError, @path
end

#data_root_path(*chunks) ⇒ Object


49
50
51
52
53
54
55
56
# File 'lib/pomodoro/config.rb', line 49

def data_root_path(*chunks)
  data_root_path = File.expand_path(self.data.fetch('data_root_path'))
  if File.directory?(data_root_path)
    [data_root_path, *chunks].join('/')
  else
    raise ConfigError, "data_root_path was supposed to be #{data_root_path}, but such path doesn't exist."
  end
end

#inspectObject


45
46
47
# File 'lib/pomodoro/config.rb', line 45

def inspect
  self.data && super
end