Class: Rant::MetaData::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/rant/import/metadata.rb

Instance Method Summary collapse

Constructor Details

#initialize(rac) ⇒ Interface

Returns a new instance of Interface.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rant/import/metadata.rb', line 21

def initialize(rac)
    @rac = rac
    # the keys in this hash are project directory names,
    # the corresponding values are again hashes and their
    # keys are target names
    @store = {}
    # just a set
    @modified_dirs = {}
    # just a set
    @read_dirs = {}
end

Instance Method Details

#at_rant_returnObject



108
109
110
# File 'lib/rant/import/metadata.rb', line 108

def at_rant_return
    save unless @rac[:dry_run]
end

#fetch(key, target, dir = @rac.current_subdir, meta_dir = nil) ⇒ Object

Fetch the meta value associated with the given key for target in dir. Note that the value will probably end in a newline. Very important is, that the dir (third argument, relative to the projects root directory) has to be the current working directory! An example:

project root directory: /home/foo/myproject
dir:                    bar
=> Dir.pwd has to be:   /home/foo/myproject/bar

Returns nil only if the value doesn’t exist.



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

def fetch(key, target, dir=@rac.current_subdir, meta_dir=nil)
    # first check if a value for the given key, target and
    # dir already exists
    dstore = @store[dir]
    if dstore
        tstore = dstore[target]
        if tstore
            val = tstore[key]
            return val if val
        end
    end
    # check if the meta file in dir was already read
    unless @read_dirs.include? dir
        read_meta_file_in_dir(dir, meta_dir)
    end
    tstore = @store[dir][target]
    tstore[key] if tstore
end

#path_fetch(key, target_path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/rant/import/metadata.rb', line 85

def path_fetch(key, target_path)
    tdir, fn = File.split target_path
    sdir = @rac.current_subdir
    if tdir == '.'
        fetch(key, fn, sdir)
    else
        dir = sdir.empty? ? tdir : "#{sdir}/#{tdir}"
        fetch(key, fn, dir, tdir)
    end
end

#path_set(key, value, target_path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rant/import/metadata.rb', line 96

def path_set(key, value, target_path)
    tdir, fn = File.split target_path
    sdir = @rac.current_subdir
    if tdir == '.'
        dir = sdir
    else
        dir = sdir.empty? ? tdir : "#{sdir}/#{tdir}"
    end
    #STDERR.puts "#{key}:#{value}:#{fn}:#{dir}"
    set(key, value, fn, dir)
end

#saveObject

Assumes to be called from the projects root directory.



113
114
115
116
117
118
# File 'lib/rant/import/metadata.rb', line 113

def save
    @modified_dirs.each_key { |dir|
        write_dstore(@store[dir], dir)
    }
    nil
end

#set(key, value, target, dir = @rac.current_subdir) ⇒ Object

Set the key-value pair for the given target in dir. Note that if value.class is not String, the value will be replaced with a newline! key should also be a string and mustn’t contain a newline.

Returns nil.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rant/import/metadata.rb', line 68

def set(key, value, target, dir=@rac.current_subdir)
    value = "\n" unless value.class == String
    @modified_dirs[dir] ||= true
    dstore = @store[dir]
    unless dstore
        @store[dir] = {target => {key => value}}
        return
    end
    tstore = dstore[target]
    if tstore
        tstore[key] = value
    else
        dstore[target] = {key => value}
    end
    nil
end