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.



272
273
274
275
# File 'app/models/global_setting.rb', line 272

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



267
268
269
# File 'app/models/global_setting.rb', line 267

def data
  @data
end

Class Method Details

.from(file) ⇒ Object



268
269
270
# File 'app/models/global_setting.rb', line 268

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

Instance Method Details

#keysObject



294
295
296
# File 'app/models/global_setting.rb', line 294

def keys
  @data.keys
end

#lookup(key, default) ⇒ Object



289
290
291
292
# File 'app/models/global_setting.rb', line 289

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

#readObject



277
278
279
280
281
282
283
284
285
286
287
# File 'app/models/global_setting.rb', line 277

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