Class: OBFS::Store

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Store

hash argument



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

def initialize(attributes = {}) # hash argument
    @path = (attributes.keys.include? :path) ? attributes[:path] : (File.join(Dir.home, '.obfs'))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

regular methods



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/obfs/store.rb', line 11

def method_missing(m, *args, &block)

    # normalize
    method_name = m.to_s
    dataA = args[0]
    dataB = args[1]

    # prevent traversing out of dir
    raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name

    # setter call
    if  method_name.end_with?('=')

        # clean up name
        method_name = method_name.gsub('=','')

        # reassign if square bracket notation
        if method_name == "[]"
            method_name = dataA
            data = dataB
        else # make sure we load the proper method_name and data
            method_name = m.to_s.gsub('=','')
            data = args[0]
        end

        # prevent traversing out of dir
        raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name
            
        # write data
        if data == nil
            FileUtils.rm_rf (File.join @path, method_name)
        else
            FileUtils.rm_rf (File.join @path, method_name) if File.exist? (File.join @path, method_name)
            FileUtils.mkpath @path if !File.directory? @path
            write(@path, method_name, data)
        end
    
    # bracket notation
    elsif method_name == "[]"

        method_name = dataA.to_s.gsub(/\["/,'').gsub(/"\]/,'')

        # prevent traversing out of dir
        raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name
            
        if (!File.directory? File.join(@path, method_name)) && (File.exist? File.join(@path, method_name))
            read(@path, method_name)
        else
            OBFS::Store.new({ path: File.join(@path, method_name.to_s) })
        end

    # recurse or read
    else

        # prevent traversing out of dir
        raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name

        if (!File.directory? File.join(@path, method_name)) && (File.exist? File.join(@path, method_name))
            read(@path, method_name)
        else
            OBFS::Store.new({ path: File.join(@path, method_name.to_s) })
        end

    end
    
end

Instance Method Details

#_exist(term = '') ⇒ Object

searches directory contents (1 level) and returns boolean if term exist



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/obfs/store.rb', line 103

def _exist(term = '')
    exist_space = Dir.entries(@path).reject { |k| k != term.to_s || k == '.' || k == '..' } rescue nil
    if !exist_space.nil?
        if exist_space.length > 0
            true
        else
            false
        end
    else
        false
    end 
end

#_find(term = '', records = 1000, tolerance = 50) ⇒ Object

searches directory contents (1 level) and returns array sorted by relevance



91
92
93
94
95
96
97
98
99
100
# File 'lib/obfs/store.rb', line 91

def _find(term = '', records = 1000, tolerance = 50)
    output = []
    search_space = Dir.entries(@path).reject { |k| k == '.' || k == '..' } rescue []
    search_space.each do |search_space_term|
        if OBFS::Levenshtein.distance(search_space_term, term) <= tolerance && OBFS::WhiteSimilarity.similarity(search_space_term, term) > 0.0
            output << search_space_term
        end
    end
    output.first(records)
end

#_indexObject

returns directory contents in an array



86
87
88
# File 'lib/obfs/store.rb', line 86

def _index
    Dir.entries(@path).reject { |k| k == '.' || k == '..' } rescue nil
end

#_pathObject

returns current working path for obfs



81
82
83
# File 'lib/obfs/store.rb', line 81

def _path
    @path
end