Class: StructureButcher

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

Overview

vyndavator, zastamdator amputovat, implantovat amputate, implantate

Defined Under Namespace

Classes: Parser, Storer

Instance Method Summary collapse

Instance Method Details

#amputate(body, slot) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/structurebutcher.rb', line 22

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/structurebutcher.rb', line 49

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/structurebutcher.rb', line 31

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/structurebutcher.rb', line 60

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, body_file, "yaml")
end

#implantate_struct_into_file(body_file, slot, part_struct) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/structurebutcher.rb', line 78

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, body_file, "yaml")
end

#split_escape(str) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/structurebutcher.rb', line 14

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