Class: JsonTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/jsontemplate.rb,
lib/jsontemplate/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ JsonTemplate

Returns a new instance of JsonTemplate.



5
6
7
8
9
# File 'lib/jsontemplate.rb', line 5

def initialize(path)
    @path = path
    @dir  = File.dirname(path)
    @json = JSON.load(File.read(path))
end

Instance Method Details

#process_dict(root = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jsontemplate.rb', line 20

def process_dict(root=nil)
    root ||= @json

    pairs = root.collect do |k,v|
        if v.is_a?(Hash)
            v = process_dict(v)
        end

        if k == "$DirMerge"
            process_dirmerge(v)
        elsif k[0] == "$"
            raise "Unknown directive #{k}"
        else
            [[k,v]]
        end
    end
    Hash[*pairs.flatten(2)]
end

#process_dirmerge(pattern) ⇒ Object



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

def process_dirmerge(pattern)
    files = Dir.glob(@dir+"/"+pattern)
    files.collect do |path|
        r = JsonTemplate.new(path).process_dict
        key=File.basename(path).split(".")[0..-2].join(".")
        [key, r]
    end.flatten(1)
end