Class: AmoebaDeployTools::Config

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/amoeba_deploy_tools/config.rb

Direct Known Subclasses

DataBagItem

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(filename, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/amoeba_deploy_tools/config.rb', line 15

def self.create(filename, opts={})
  opts.merge!({
    filename: File.expand_path(filename),
    create: true
  })

  Config.new.tap do |c|
    c.options(opts)
  end
end

.load(filename, opts = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/amoeba_deploy_tools/config.rb', line 8

def self.load(filename, opts={})
  opts[:filename] = File.expand_path filename
  Config.new.tap do |c|
    c.restore(opts)
  end
end

Instance Method Details

#[](k) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/amoeba_deploy_tools/config.rb', line 67

def [](k)
  chain = k.to_s.split('.')
  cur = self

  return super if chain.count <= 1

  for c in chain[0..-2]
    if cur and cur.key? c
      cur = cur.regular_reader(c)
    else
      return
    end
  end

  cur[chain[-1]]
end

#[]=(k, v) ⇒ Object



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

def []=(k, v)
  chain = k.to_s.split('.')
  cur = self

  return super if chain.count <= 1

  for c in chain[0..-2]
    cur = cur.initializing_reader(c)
  end

  cur[chain[-1]] = v
end

#flattenObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/amoeba_deploy_tools/config.rb', line 97

def flatten
  flat = {}

  each do |k1, v1|
    if v1.class == self.class
      v1.flatten.each do |k2, v2|
        flat["#{k1}.#{k2}"] = v2
      end
    else
      flat[k1] = v1
    end
  end

  flat
end

#new_file?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/amoeba_deploy_tools/config.rb', line 49

def new_file?
  !!@new_file
end

#options(opts = nil) ⇒ Object



26
27
28
29
30
# File 'lib/amoeba_deploy_tools/config.rb', line 26

def options(opts=nil)
  @opts ||= { format: :yaml }
  @opts.merge! opts if opts
  @opts
end

#reload!Object



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

def reload!
  restore(filename: options[:filename])
end

#restore(opts = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/amoeba_deploy_tools/config.rb', line 32

def restore(opts=nil)
  options(opts)

  return unless filename = options[:filename]

  self.clear.deep_merge! deserialize(File.read(filename))

  self
rescue Errno::ENOENT
  @new_file = true
  FileUtils.touch(filename) and retry if options[:create]
end

#save(opts = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/amoeba_deploy_tools/config.rb', line 53

def save(opts=nil)
  options(opts)

  return unless filename = options[:filename]

  File.open(filename, 'w') do |fh|
    fh.write(serialize(self.to_hash))
  end

  self
rescue Errno::ENOENT
  FileUtils.touch(filename) and retry if options[:create]
end

#to_sObject



114
115
116
# File 'lib/amoeba_deploy_tools/config.rb', line 114

def to_s
  to_hash.to_s
end