orion 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/orion.rb +4 -4
- data/lib/orion/orion_objects/fixnum.rb +37 -0
- data/lib/orion/orion_objects/hash.rb +4 -0
- data/lib/orion/orion_objects/time.rb +13 -0
- data/lib/orion/orion_support/find/find.rb +72 -0
- data/lib/orion/search.rb +3 -21
- data/lib/orion/version.rb +1 -1
- metadata +5 -4
- data/lib/boyer_moore/boyer_moore.rb +0 -90
- data/lib/boyer_moore/rich_hash.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5725831d98f7cd26ee72862fb3548ffbca64bef
|
4
|
+
data.tar.gz: 8f45aa5e5cabc427b37f63848e5028dcb66595f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fae5ab16115782b25d90634c12ab029ca87a633a7e7c4af00785e6b129cdde4444a335040b8bcb30423d0aebff3d61f8c7cefe7ec72aa6bda9a351b524908703
|
7
|
+
data.tar.gz: 808686314a191d1444806e7cac7edf83db8e7e91c1ba00a18195d86c15d6b4af2d749ed51c05a513f2bb7dd4d5465c3c6a444d1f65134827542bea20abc2dd5b
|
data/lib/orion.rb
CHANGED
@@ -5,13 +5,13 @@ require "orion/delete"
|
|
5
5
|
require "orion/info"
|
6
6
|
|
7
7
|
module Orion
|
8
|
-
def self.search(root_path,
|
9
|
-
method_call = Orion::Search.new(root_path).
|
8
|
+
def self.search(root_path, query_hash)
|
9
|
+
method_call = Orion::Search.new(root_path).search(query_hash).to_orion
|
10
10
|
block_given? ? yield(method_call) : method_call
|
11
11
|
end
|
12
12
|
|
13
|
-
def self.delete(root_path,
|
14
|
-
found_files = Orion::Search.new(root_path).search(
|
13
|
+
def self.delete(root_path, query_hash)
|
14
|
+
found_files = Orion::Search.new(root_path).search(query_hash)
|
15
15
|
if block_given?
|
16
16
|
yield Orion::Delete.with_response(found_files)
|
17
17
|
else
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Fixnum
|
2
|
+
def seconds
|
3
|
+
self
|
4
|
+
end
|
5
|
+
|
6
|
+
def minutes
|
7
|
+
self * 60.seconds
|
8
|
+
end
|
9
|
+
|
10
|
+
def hours
|
11
|
+
self * 60.minutes
|
12
|
+
end
|
13
|
+
|
14
|
+
def days
|
15
|
+
self * 24.hours
|
16
|
+
end
|
17
|
+
|
18
|
+
def weeks
|
19
|
+
self * 7.days
|
20
|
+
end
|
21
|
+
|
22
|
+
def months
|
23
|
+
self * 4.weeks
|
24
|
+
end
|
25
|
+
|
26
|
+
def years
|
27
|
+
self * 12.months
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :second, :seconds
|
31
|
+
alias_method :minute, :minutes
|
32
|
+
alias_method :hour, :hours
|
33
|
+
alias_method :day, :days
|
34
|
+
alias_method :week, :weeks
|
35
|
+
alias_method :month, :months
|
36
|
+
alias_method :year, :years
|
37
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# Original from:
|
2
|
+
# https://github.com/ruby/ruby/blob/trunk/lib/find.rb?source=cc
|
3
|
+
# Modified to fit Orion purposes
|
4
|
+
|
5
|
+
require "orion/orion_support/i18n"
|
6
|
+
require "orion/orion_objects/fixnum"
|
7
|
+
|
8
|
+
module Orion
|
9
|
+
module Find
|
10
|
+
def find(*paths, conditions) # :yield: path
|
11
|
+
raise I18n.t("errors.search.not_conditions_provided") if conditions.empty?
|
12
|
+
condition_string = condition_statement(validate(conditions))
|
13
|
+
results = []
|
14
|
+
|
15
|
+
paths.collect!{|d| raise Errno::ENOENT unless File.exist?(d); d.dup}
|
16
|
+
while file = paths.shift
|
17
|
+
catch(:prune) do
|
18
|
+
results << file.dup.taint if eval(condition_string)
|
19
|
+
begin
|
20
|
+
s = File.lstat(file)
|
21
|
+
rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
|
22
|
+
next
|
23
|
+
end
|
24
|
+
if s.directory? then
|
25
|
+
begin
|
26
|
+
fs = Dir.entries(file)
|
27
|
+
rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
|
28
|
+
next
|
29
|
+
end
|
30
|
+
fs.sort!
|
31
|
+
fs.reverse_each {|f|
|
32
|
+
next if f == "." or f == ".."
|
33
|
+
f = File.join(file, f)
|
34
|
+
paths.unshift f.untaint
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
results
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.validate(conditions)
|
43
|
+
valid_conditions = [:name, :atime, :ctime, :mtime, :ftype, :size, :absolute_path, :basename, :directory?, :dirname, :executable?, :exists?, :extname, :file?, :readable?, :socket?, :symlink?, :writable?, :zero?]
|
44
|
+
invalid_cond = ((valid_conditions+conditions.keys)-(valid_conditions&conditions.keys))-(valid_conditions-(valid_conditions&conditions.keys))
|
45
|
+
invalid_cond.empty? ? conditions : raise(NoMethodError)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.arrange_conditions(conditions)
|
49
|
+
@comparative_conditions = conditions.select{|k,v| v =~ /(>)|(<)/ } # size: '> 999'
|
50
|
+
@equality_conditions = Hash.diff(conditions, @comparative_conditions) # name: '.rb'
|
51
|
+
end
|
52
|
+
|
53
|
+
# Creates a conditional statement string with the given conditions
|
54
|
+
def self.condition_statement(conditions)
|
55
|
+
arrange_conditions(conditions)
|
56
|
+
conditions_array = conditions.has_key?(:name) ? ["file.include?(conditions[:name])"] : []
|
57
|
+
@equality_conditions.each do |k, v|
|
58
|
+
conditions_array << "File.send(:#{k}, file) == conditions[:#{k}]" unless k == :name
|
59
|
+
end
|
60
|
+
@comparative_conditions.each do |k, v|
|
61
|
+
conditions_array << "File.send(:#{k}, file) #{conditions[k]}"
|
62
|
+
end
|
63
|
+
conditions_array.join(" && ")
|
64
|
+
end
|
65
|
+
|
66
|
+
def prune
|
67
|
+
throw :prune
|
68
|
+
end
|
69
|
+
|
70
|
+
module_function :find, :prune
|
71
|
+
end
|
72
|
+
end
|
data/lib/orion/search.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
require 'find'
|
2
|
-
require "orion/orion_objects/array"
|
1
|
+
require 'orion/orion_support/find/find'
|
3
2
|
require "orion/orion_objects/nil"
|
4
3
|
|
5
4
|
module Orion
|
@@ -7,28 +6,11 @@ module Orion
|
|
7
6
|
attr_accessor :root_path
|
8
7
|
|
9
8
|
def initialize(root_path)
|
10
|
-
@root_path ||= root_path
|
9
|
+
@root_path ||= File.expand_path(root_path)
|
11
10
|
end
|
12
11
|
|
13
12
|
def search(query)
|
14
|
-
find(query)
|
15
|
-
end
|
16
|
-
|
17
|
-
def with_response(query)
|
18
|
-
find(query).to_orion
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def find(query)
|
24
|
-
results = []
|
25
|
-
Find.find(@root_path) do |path|
|
26
|
-
unless FileTest.directory?(path)
|
27
|
-
file = File.basename(path)
|
28
|
-
results << path if file.include?(query)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
results
|
13
|
+
Find.find(@root_path, query)
|
32
14
|
end
|
33
15
|
end
|
34
16
|
end
|
data/lib/orion/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: orion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bismark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -73,13 +73,14 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
- lib/boyer_moore/rich_hash.rb
|
77
|
-
- lib/boyer_moore/boyer_moore.rb
|
78
76
|
- lib/orion.rb
|
77
|
+
- lib/orion/orion_support/find/find.rb
|
79
78
|
- lib/orion/orion_support/i18n.rb
|
79
|
+
- lib/orion/orion_objects/fixnum.rb
|
80
80
|
- lib/orion/orion_objects/array.rb
|
81
81
|
- lib/orion/orion_objects/nil.rb
|
82
82
|
- lib/orion/orion_objects/hash.rb
|
83
|
+
- lib/orion/orion_objects/time.rb
|
83
84
|
- lib/orion/version.rb
|
84
85
|
- lib/orion/delete.rb
|
85
86
|
- lib/orion/search.rb
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require "boyer_moore/rich_hash"
|
2
|
-
# ported directly from this version wikipedia:
|
3
|
-
# http://en.wikipedia.org/w/index.php?title=Boyer%E2%80%93Moore_string_search_algorithm&diff=391986850&oldid=391398281
|
4
|
-
# it's not very rubyish but it works
|
5
|
-
module Orion
|
6
|
-
module BoyerMoore
|
7
|
-
|
8
|
-
def self.compute_prefix(str)
|
9
|
-
size = str.length
|
10
|
-
k = 0
|
11
|
-
result = [0]
|
12
|
-
1.upto(size - 1) do |q|
|
13
|
-
while (k > 0) && (str[k] != str[q])
|
14
|
-
k = result[k-1]
|
15
|
-
end
|
16
|
-
k += 1 if(str[k] == str[q])
|
17
|
-
result[q] = k
|
18
|
-
end
|
19
|
-
result
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.prepare_badcharacter_heuristic(str)
|
23
|
-
result = Orion::RichHash.new
|
24
|
-
0.upto(str.length - 1) do |i|
|
25
|
-
result[str[i]] = i
|
26
|
-
end
|
27
|
-
result
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.prepare_goodsuffix_heuristic(normal)
|
31
|
-
size = normal.size
|
32
|
-
result = []
|
33
|
-
|
34
|
-
reversed = normal.dup.reverse
|
35
|
-
prefix_normal = compute_prefix(normal)
|
36
|
-
prefix_reversed = compute_prefix(reversed)
|
37
|
-
|
38
|
-
0.upto(size) do |i|
|
39
|
-
result[i] = size - prefix_normal[size-1]
|
40
|
-
end
|
41
|
-
|
42
|
-
0.upto(size-1) do |i|
|
43
|
-
j = size - prefix_reversed[i]
|
44
|
-
k = i - prefix_reversed[i]+1
|
45
|
-
result[j] = k if result[j] > k
|
46
|
-
end
|
47
|
-
result
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.search(haystack, needle)
|
51
|
-
needle_len = needle.size
|
52
|
-
haystack_len = haystack.size
|
53
|
-
|
54
|
-
return nil if haystack_len == 0
|
55
|
-
return haystack if needle_len == 0
|
56
|
-
|
57
|
-
badcharacter = self.prepare_badcharacter_heuristic(needle)
|
58
|
-
goodsuffix = self.prepare_goodsuffix_heuristic(needle)
|
59
|
-
|
60
|
-
s = 0
|
61
|
-
while s <= haystack_len - needle_len
|
62
|
-
j = needle_len
|
63
|
-
while (j > 0) && self.needle_matches?(needle[j-1], haystack[s+j-1])
|
64
|
-
j -= 1
|
65
|
-
end
|
66
|
-
|
67
|
-
if(j > 0)
|
68
|
-
k = badcharacter[haystack[s+j-1]]
|
69
|
-
k = -1 unless k
|
70
|
-
if (k < j) && (m = j-k-1) > goodsuffix[j]
|
71
|
-
s += m
|
72
|
-
else
|
73
|
-
s += goodsuffix[j]
|
74
|
-
end
|
75
|
-
else
|
76
|
-
return s
|
77
|
-
end
|
78
|
-
end
|
79
|
-
return nil
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.needle_matches?(needle, haystack)
|
83
|
-
if needle.kind_of?(Regexp)
|
84
|
-
needle.match(haystack) ? true : false
|
85
|
-
else
|
86
|
-
needle == haystack
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
# Hash impersonator that accepts regular expressions as keys. But the regular
|
2
|
-
# expression lookups are slow, so don't use them (unless you have to).
|
3
|
-
module Orion
|
4
|
-
class RichHash
|
5
|
-
def initialize
|
6
|
-
@regexps = {}
|
7
|
-
@regular = {}
|
8
|
-
end
|
9
|
-
|
10
|
-
def [](k)
|
11
|
-
regular = @regular[k]
|
12
|
-
return regular if regular
|
13
|
-
if @regexps.size > 0
|
14
|
-
@regexps.each do |regex,v| # linear search is going to be slow
|
15
|
-
return v if regex.match(k)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
nil
|
19
|
-
end
|
20
|
-
|
21
|
-
def []=(k,v)
|
22
|
-
if k.kind_of?(Regexp)
|
23
|
-
@regexps[k] = v
|
24
|
-
else
|
25
|
-
@regular[k] = v
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|