Class: GlobalSetting::FileProvider

Inherits:
BaseProvider show all
Defined in:
app/models/global_setting.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseProvider

coerce, #resolve

Constructor Details

#initialize(file) ⇒ FileProvider

Returns a new instance of FileProvider.



306
307
308
309
# File 'app/models/global_setting.rb', line 306

def initialize(file)
  @file = file
  @data = {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



301
302
303
# File 'app/models/global_setting.rb', line 301

def data
  @data
end

Class Method Details

.from(file) ⇒ Object



302
303
304
# File 'app/models/global_setting.rb', line 302

def self.from(file)
  parse(file) if File.exist?(file)
end

Instance Method Details

#keysObject



328
329
330
# File 'app/models/global_setting.rb', line 328

def keys
  @data.keys
end

#lookup(key, default) ⇒ Object



323
324
325
326
# File 'app/models/global_setting.rb', line 323

def lookup(key, default)
  var = @data[key]
  resolve(var, var.nil? ? default : "")
end

#readObject



311
312
313
314
315
316
317
318
319
320
321
# File 'app/models/global_setting.rb', line 311

def read
  ERB
    .new(File.read(@file))
    .result()
    .split("\n")
    .each do |line|
      if line =~ /\A\s*([a-z_]+[a-z0-9_]*)\s*=\s*(\"([^\"]*)\"|\'([^\']*)\'|[^#]*)/
        @data[$1.strip.to_sym] = ($4 || $3 || $2).strip
      end
    end
end