Class: StructureButcher

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

Defined Under Namespace

Classes: Parser, Storer

Instance Method Summary collapse

Instance Method Details

#amputate(body, slot) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/structurebutcher.rb', line 34

def amputate(body, slot)
    keys = split_escape(slot)
    result = body
    while (key = keys.shift)
        result = result[key]
    end
    return result
end

#amputate_file(body_file, slot, part_file, format) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/structurebutcher.rb', line 61

def amputate_file(body_file, slot, part_file, format)
    parser = StructureButcher::Parser.new
    body   = parser.load_structure(body_file, "yaml")

    butcher = StructureButcher.new
    part = butcher.amputate(body, slot)

    storer = StructureButcher::Storer.new
    storer.save_structure(part, part_file, format)
end

#implantate(body, slot, part) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/structurebutcher.rb', line 43

def implantate(body, slot, part)
    keys = split_escape(slot)
    last_key = keys.pop
    area = body

    if not area
        area = Hash.new
    end

    while (key = keys.shift)
        if not area.has_key?(key)
            then area[key] = {}
        end
        area = area[key]
    end
    area[last_key] = part
end

#implantate_file(body_file, slot, part_file, format) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/structurebutcher.rb', line 72

def implantate_file(body_file, slot, part_file, format)
    parser = StructureButcher::Parser.new
    body   = parser.load_structure(body_file, "yaml")
    part   = parser.load_structure(part_file, format)

    # make sure we work with hash
    # it does not make sense to work with anything else
    if not body.is_a?(Hash)
        body = Hash.new
    end

    butcher = StructureButcher.new
    part = butcher.implantate(body, slot, part)

    storer = StructureButcher::Storer.new

    storer.save_structure(body.sort_by_key(true), body_file, "yaml")
end

#implantate_struct_into_file(body_file, slot, part_struct) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/structurebutcher.rb', line 91

def implantate_struct_into_file(body_file, slot, part_struct)
    parser = StructureButcher::Parser.new
    body   = parser.load_structure(body_file, "yaml")

    # make sure we work with hash
    # it does not make sense to work with anything else
    if not body.is_a?(Hash)
        body = Hash.new
    end

    butcher = StructureButcher.new
    butcher.implantate(body, slot, part_struct)

    storer = StructureButcher::Storer.new
    storer.save_structure(body.sort_by_key(true), body_file, "yaml")
end

#split_escape(str) ⇒ Object



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

def split_escape(str)
    output = []
    str.scan(/(?>\\.|[^\\.])+/) do |chunk|
        output.push( chunk.gsub(/\\(.)/, '\1') )
    end
    return output
end