Class: Workplaces::Settings

Inherits:
Hash
  • Object
show all
Defined in:
lib/workplaces/settings.rb

Direct Known Subclasses

Contexts, Dirs

Defined Under Namespace

Classes: Item

Constant Summary collapse

DIR =
File.join(::Dir.home, '.config', 'workplaces')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, hash = {}) ⇒ Settings

Returns a new instance of Settings.



63
64
65
66
67
# File 'lib/workplaces/settings.rb', line 63

def initialize(name, hash={})
  @name = name
  @path = Settings.path name
  merge! hash
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/workplaces/settings.rb', line 61

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



61
62
63
# File 'lib/workplaces/settings.rb', line 61

def path
  @path
end

Class Method Details

.class_nameObject



21
22
23
24
# File 'lib/workplaces/settings.rb', line 21

def class_name
  index = name.reverse.index('::')
  index ? name[-index..-1] : name
end

.open(name, &block) ⇒ Object



16
17
18
19
# File 'lib/workplaces/settings.rb', line 16

def open(name, &block)
  settings = Settings.new name, load_data(name)
  eval_block settings, &block
end

.path(name) ⇒ Object



12
13
14
# File 'lib/workplaces/settings.rb', line 12

def path(name)
  File.join(DIR, "#{name}.yml")
end

.settings_subclassObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/workplaces/settings.rb', line 26

def settings_subclass
  self.class_eval do
    def self.open(&block)
      settings = self.new load_data(class_name.underscore)
      eval_block settings, &block
    end

    def initialize(hash={})
      super self.class.class_name.underscore, hash
      item_class = self.class.name.singularize.constantize
      self.default_proc = proc do |hash, key|
        hash[key] = item_class.new
      end
    end
  end
end

Instance Method Details

#saveObject



69
70
71
72
73
74
# File 'lib/workplaces/settings.rb', line 69

def save
  FileUtils.mkdir_p DIR unless ::Dir.exists?(DIR)
  File.open(@path, 'w') do |f|
    f.write({}.merge(self).to_yaml)
  end
end