Class: Confickle

Inherits:
Object
  • Object
show all
Defined in:
lib/confickle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Confickle

Returns a new instance of Confickle.



11
12
13
14
15
16
17
18
# File 'lib/confickle.rb', line 11

def initialize(options)
  if options.is_a? String
    options = {root: options}
  end

  @root            = File.absolute_path(options.fetch(:root))
  @symbolize_names = options.fetch(:symbolize_names, true)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/confickle.rb', line 9

def root
  @root
end

#symbolize_namesObject (readonly)

Returns the value of attribute symbolize_names.



9
10
11
# File 'lib/confickle.rb', line 9

def symbolize_names
  @symbolize_names
end

Instance Method Details

#content(*args) ⇒ Object Also known as: contents



25
26
27
# File 'lib/confickle.rb', line 25

def content(*args)
  File.read(self.path(*args))
end

#json(*args) ⇒ Object



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

def json(*args)
  if args.last.is_a? Hash
    args    = args.dup
    options = args.pop
  else
    options = {}
  end

  sn = options.fetch(:symbolize_names, self.symbolize_names)
  JSON.parse(
    self.content(*args),
    symbolize_names: sn
  )
end

#path(*args) ⇒ Object



21
22
23
# File 'lib/confickle.rb', line 21

def path(*args)
  File.join(self.root, *args)
end

#yaml(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/confickle.rb', line 46

def yaml(*args)
  if args.last.is_a? Hash
    args    = args.dup
    options = args.pop
  else
    options = {}
  end

  retval = YAML.load_file(path(*args))

  sn = options.fetch(:symbolize_names, self.symbolize_names)
  if sn
    RecSym.this(retval)
  else
    retval
  end
end