Class: Silly::Query

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/silly.rb

Constant Summary collapse

BlackList =
["", "~", "/"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



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

def initialize
  @criteria = {
    "path" => nil,
    "sort" => ["id", "asc"],
    "where" => [],
  }
  @paths = Set.new
end

Instance Attribute Details

#pathsObject

Returns the value of attribute paths.



23
24
25
# File 'lib/silly.rb', line 23

def paths
  @paths
end

Instance Method Details

#append_path(path) ⇒ Object



34
35
36
# File 'lib/silly.rb', line 34

def append_path(path)
  @paths << path
end

#eachObject



59
60
61
62
63
# File 'lib/silly.rb', line 59

def each
  block_given? ?
    execute.each { |a| yield(a) } :
    execute
end

#executeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/silly.rb', line 65

def execute
  @criteria["path"] ||= "*"
  puts "EXECUTE:\n #{ @criteria.inspect }"
  data = files(@criteria["path"])

  unless @criteria["where"].empty?
    data = data.keep_if { |id, pointer| filter_function(pointer) }
  end

  data = data.values.sort { |a,b| sorting_function(a,b) }

  Silly::Collection.new(data)
end

#inspectObject



91
92
93
# File 'lib/silly.rb', line 91

def inspect
  "#{ self.class.name }\n criteria:\n #{ @criteria.inspect }"
end

#listObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/silly.rb', line 79

def list
  results = Set.new

  paths.each do |path|
    FileUtils.cd(path) {
      results += Dir['*'].select { |x| File.directory?(x) }
    }
  end

  results.to_a
end

#path(path) ⇒ Object



39
40
41
42
# File 'lib/silly.rb', line 39

def path(path)
  @criteria["path"] = BlackList.include?(path.to_s) ? "*" : File.join(path, "*")
  self
end

#path_all(path) ⇒ Object



44
45
46
47
# File 'lib/silly.rb', line 44

def path_all(path)
  @criteria["path"] = File.join(path, "**", "**")
  self
end

#sort(conditions = []) ⇒ Object



49
50
51
52
# File 'lib/silly.rb', line 49

def sort(conditions=[])
  @criteria["sort"] = conditions
  self
end

#where(conditions) ⇒ Object



54
55
56
57
# File 'lib/silly.rb', line 54

def where(conditions)
  @criteria["where"] << conditions
  self
end