Class: Rbcli::UserConf::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcli/components/config/backend.rb

Direct Known Subclasses

Env, Ini, Json, Null, Toml, Yaml

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, type) ⇒ Backend



36
37
38
39
40
41
# File 'lib/rbcli/components/config/backend.rb', line 36

def initialize filename, type
  @path = File.expand_path(filename)
  @path = File.realpath(@path) if File.symlink?(@path)
  @type = type.to_s.capitalize
  @loaded = false
end

Instance Attribute Details

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



43
44
45
# File 'lib/rbcli/components/config/backend.rb', line 43

def loaded
  @loaded
end

#typeObject (readonly)

Returns the value of attribute type.



43
44
45
# File 'lib/rbcli/components/config/backend.rb', line 43

def type
  @type
end

Class Method Details

.create(filename, type: nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/rbcli/components/config/backend.rb', line 29

def self.create filename, type: nil
  type ||= self.types.map { |slug, check| [slug, check.call(filename)] }.select { |slug, match| match }.first.first
  type = type.to_s.downcase.to_sym
  require_relative "backends/#{type.to_s}"
  @registered_types[type].new(filename, type)
end

.inherited(subklass) ⇒ Object



25
26
27
# File 'lib/rbcli/components/config/backend.rb', line 25

def self.inherited(subklass)
  @registered_types[subklass.name.split('::').last.sub(/Store$/, '').downcase.to_sym] = subklass
end

.typesObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/rbcli/components/config/backend.rb', line 14

def self.types
  {
    yaml: Proc.new { |filename| filename.is_a?(String) && (filename.downcase.end_with?('.yaml') || filename.downcase.end_with?('.yml')) },
    json: Proc.new { |filename| filename.is_a?(String) && filename.downcase.end_with?('.json') },
    ini: Proc.new { |filename| filename.is_a?(String) && filename.downcase.end_with?('.ini') },
    toml: Proc.new { |filename| filename.is_a?(String) && filename.downcase.end_with?('.toml') },
    env: Proc.new { |filename| false },
    null: Proc.new { |filename| true }
  }
end

Instance Method Details

#annotate!(banner) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbcli/components/config/backend.rb', line 92

def annotate! banner
  return true unless self.respond_to?(:inject_banner) || self.private_methods.include?(:inject_banner)

  begin
    text = File.read(@path)
  rescue Errno::ENOENT => _
    Rbcli.log.debug "Attempted to annotate #{@type} config file but did not find it at '#{@path}'", "CONF"
    return nil
  end

  Rbcli.log.debug "Annotating #{@type} config file at '#{@path}'", "CONF"
  text = self.inject_banner(text, banner)

  begin
    File.write(@path, text)
    Rbcli.log.debug "Saved #{@type} annotations at '#{@path}'", "CONF"
  rescue Errno => err
    Rbcli.log.error "Could not save annotations to '#{@path}'", "CONF"
    Rbcli.log.error "Error: #{err.message}", "CONF"
    return nil
  else
    return true
  end
end

#exist?Boolean



88
89
90
# File 'lib/rbcli/components/config/backend.rb', line 88

def exist?
  File.exist?(@path)
end

#load(_defaults = nil) ⇒ Object

The defaults parameter is used on some backends which override this method



47
48
49
50
51
52
53
54
55
56
# File 'lib/rbcli/components/config/backend.rb', line 47

def load _defaults = nil
  begin
    text = File.read(@path)
  rescue Errno::ENOENT => _
    Rbcli.log.debug "Attempted to load #{@type} config file but did not find it at '#{@path}'", "CONF"
  else
    Rbcli.log.debug "Loaded #{@type} config file at '#{@path}'", "CONF"
    self.parse(text)
  end
end

#savable?Boolean



84
85
86
# File 'lib/rbcli/components/config/backend.rb', line 84

def savable?
  File.writable?(File.exist?(@path) ? @path : File.dirname(@path))
end

#save(hash) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rbcli/components/config/backend.rb', line 58

def save hash
  Rbcli.log.debug "Saving #{@type} config file at '#{@path}'", "CONF"
  begin
    File.write(@path, self.unparse(hash))
  rescue Errno => err
    Rbcli.log.error "Could not save config to file at '#{@path}'", "CONF"
    Rbcli.log.error "Error: #{err.message}", "CONF"
    false
  else
    hash
  end
end

#save_raw(text) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbcli/components/config/backend.rb', line 71

def save_raw text
  Rbcli.log.debug "Saving #{@type} config file at '#{@path}' using raw text", "CONF"
  begin
    File.write(@path, text)
  rescue Errno => err
    Rbcli.log.error "Could not save config to file at '#{@path}'", "CONF"
    Rbcli.log.error "Error: #{err.message}", "CONF"
    false
  else
    true
  end
end