Class: JsonTemplate

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

Constant Summary collapse

VERSION =
"0.4.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, path = nil) ⇒ JsonTemplate

Returns a new instance of JsonTemplate.



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

def initialize(src, path=nil)
    path ||= src.kind_of?(String) ? src : nil
    @path = path || (src.respond_to?(:path) ? src.path : nil)
    @dir  = File.expand_path(path ? File.dirname(path) : ".")

    @properties_file = File.join(@dir,"properties.json")
    @secrets_file    = File.join(@dir,"secrets.json")

    if src.respond_to?(:read)
      @filename = "<stream>"
      data = src.read
    else
      @filename = File.join(@dir, File.basename(src))
      data = File.read(@filename)
    end
    @json = JSON.load(data)
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



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

def json
  @json
end

#properties_fileObject

Returns the value of attribute properties_file.



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

def properties_file
  @properties_file
end

#secrets_fileObject

Returns the value of attribute secrets_file.



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

def secrets_file
  @secrets_file
end

Instance Method Details

#process(root = nil) ⇒ Object



46
47
48
49
50
51
# File 'lib/jsontemplate.rb', line 46

def process(root=nil)
    root ||= @json
    return process_dict(root) if root.is_a?(Hash)
    return process_array(root) if root.is_a?(Array)
    return root
end

#process_array(root) ⇒ Object



40
41
42
43
44
# File 'lib/jsontemplate.rb', line 40

def process_array(root)
    root.collect do |v|
       process(v)
    end
end

#process_dict(root = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jsontemplate.rb', line 53

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

    return process(root) if !root.is_a?(Hash) # Backwards compat hack

    pairs = root.collect do |k,v|
        v = process(v)

        if k == "$DirMerge"
            process_dirmerge(v)
        elsif k == "$FLookup"
            process_flookup(*v)
        elsif k == "$Prop"
            process_flookup(@properties_file, v)
        elsif k == "$Secret"
            process_flookup(@secrets_file, v)
        elsif k[0] == "$"
            raise "Unknown directive #{k}"
        else
            [k,v]
        end
    end
    if pairs.length == 1 && !pairs.first.kind_of?(Array)
        pairs.first
    else
        Hash[*pairs.flatten(1)]
    end
end

#process_dirmerge(pattern) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/jsontemplate.rb', line 26

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

#process_flookup(file, v) ⇒ Object



35
36
37
38
# File 'lib/jsontemplate.rb', line 35

def process_flookup(file, v)
    map = JsonTemplate.new(file,@path).process_dict
    map[v]
end