Class: AmoebaDeployTools::Config
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- AmoebaDeployTools::Config
- Defined in:
- lib/amoeba_deploy_tools/config.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #flatten ⇒ Object
- #new_file? ⇒ Boolean
- #options(opts = nil) ⇒ Object
- #reload! ⇒ Object
- #restore(opts = nil) ⇒ Object
- #save(opts = nil) ⇒ Object
- #to_s ⇒ Object
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.(filename), create: true }) Config.new.tap do |c| c.(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. 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 |
#flatten ⇒ Object
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
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 (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: [: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) (opts) return unless filename = [:filename] self.clear.deep_merge! deserialize(File.read(filename)) self rescue Errno::ENOENT @new_file = true FileUtils.touch(filename) and retry if [: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) (opts) return unless filename = [:filename] File.open(filename, 'w') do |fh| fh.write(serialize(self.to_hash)) end self rescue Errno::ENOENT FileUtils.touch(filename) and retry if [:create] end |
#to_s ⇒ Object
114 115 116 |
# File 'lib/amoeba_deploy_tools/config.rb', line 114 def to_s to_hash.to_s end |