Module: Kofi

Defined in:
lib/kofi.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.trailObject (readonly)

Returns the value of attribute trail.



7
8
9
# File 'lib/kofi.rb', line 7

def trail
  @trail
end

Class Method Details

.deep_merge(lhs, rhs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kofi.rb', line 52

def deep_merge(lhs, rhs)
  target = lhs.dup

  rhs.keys.each do |key|
    if rhs[key].is_a? Hash and lhs[key].is_a? Hash
      target[key] = deep_merge(target[key], rhs[key])
      next
    end

    target[key] = rhs[key]
  end

  target
end

.filenameObject



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

def filename
  File.join(@locale_path, "#{@locale}.yml")
end

.get(key) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/kofi.rb', line 29

def get(key)
  current = YAML::load_file(filename)
  key.each do |part|
    current = current[part]
  end

  return current
end

.hash_to_be_merged(trail, value) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/kofi.rb', line 42

def hash_to_be_merged(trail, value)
  trail = trail.dup
  if trail.size > 0
    key = trail.shift
    { key => hash_to_be_merged(trail, value) }
  else
    value
  end
end

.run(opts, args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kofi.rb', line 9

def run(opts, args)
  @locale_path = opts[:locale_path]
  @trail       = args.shift.split('.')
  @locale      = trail.first
  @value       = args.shift

  if @value
    data = set YAML::load_file(filename), trail, @value
    File.open(filename, 'w') do |file|
      YAML::dump(data, file)
    end
  end

  puts "#{trail.join('.')} => #{get(trail).inspect}"
end

.set(data, key, value) ⇒ Object



38
39
40
# File 'lib/kofi.rb', line 38

def set(data, key, value)
  deep_merge(data, hash_to_be_merged(key, value))
end