Class: Redistat::Finder

Inherits:
Object
  • Object
show all
Includes:
Database
Defined in:
lib/redistat/finder.rb,
lib/redistat/finder/date_set.rb

Defined Under Namespace

Classes: DateSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Database

included

Constructor Details

#initialize(opts = {}) ⇒ Finder

Returns a new instance of Finder.



45
46
47
# File 'lib/redistat/finder.rb', line 45

def initialize(opts = {})
  set_options(opts)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/redistat/finder.rb', line 43

def options
  @options
end

Class Method Details

.dateObject



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

def dates(from, till)
  new.dates(from, till)
end

.dates(from, till) ⇒ Object



20
21
22
# File 'lib/redistat/finder.rb', line 20

def dates(from, till)
  new.dates(from, till)
end

.depth(unit) ⇒ Object



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

def depth(unit)
  new.depth(unit)
end

.find(*args) ⇒ Object



8
9
10
# File 'lib/redistat/finder.rb', line 8

def find(*args)
  new.find(*args)
end

.from(date) ⇒ Object



25
26
27
# File 'lib/redistat/finder.rb', line 25

def from(date)
  new.from(date)
end

.interval(unit) ⇒ Object



38
39
40
# File 'lib/redistat/finder.rb', line 38

def interval(unit)
  new.interval(unit)
end

.label(label) ⇒ Object



16
17
18
# File 'lib/redistat/finder.rb', line 16

def label(label)
  new.label(label)
end

.scope(scope) ⇒ Object



12
13
14
# File 'lib/redistat/finder.rb', line 12

def scope(scope)
  new.scope(scope)
end

.till(date) ⇒ Object



29
30
31
# File 'lib/redistat/finder.rb', line 29

def till(date)
  new.till(date)
end

.untillObject



32
33
34
# File 'lib/redistat/finder.rb', line 32

def till(date)
  new.till(date)
end

Instance Method Details

#all(reload = false) ⇒ Object



53
54
55
56
# File 'lib/redistat/finder.rb', line 53

def all(reload = false)
  @result = nil if reload
  @result ||= find
end

#childrenObject



78
79
80
81
82
# File 'lib/redistat/finder.rb', line 78

def children
  build_key.children.map { |key|
    self.class.new(options.merge(:label => key.label.to_s))
  }
end

#connection_ref(ref = nil) ⇒ Object



84
85
86
87
88
89
# File 'lib/redistat/finder.rb', line 84

def connection_ref(ref = nil)
  return options[:connection_ref] if ref.nil?
  reset! if options[:connection_ref] != ref
  options[:connection_ref] = ref
  self
end

#dates(start, finish) ⇒ Object Also known as: date



105
106
107
# File 'lib/redistat/finder.rb', line 105

def dates(start, finish)
  from(start).till(finish)
end

#depth(unit = nil) ⇒ Object



125
126
127
128
129
130
# File 'lib/redistat/finder.rb', line 125

def depth(unit = nil)
  return options[:depth] if unit.nil?
  reset! if options[:depth] != unit
  options[:depth] = unit
  self
end

#each(&block) ⇒ Object



62
63
64
# File 'lib/redistat/finder.rb', line 62

def each(&block)
  all.each(&block)
end

#each_with_index(&block) ⇒ Object



70
71
72
# File 'lib/redistat/finder.rb', line 70

def each_with_index(&block)
  all.each_with_index(&block)
end

#find(opts = {}) ⇒ Object

Raises:



139
140
141
142
143
144
145
146
147
# File 'lib/redistat/finder.rb', line 139

def find(opts = {})
  set_options(opts)
  raise InvalidOptions.new if !valid_options?
  if options[:interval].nil? || !options[:interval]
    find_by_magic
  else
    find_by_interval
  end
end

#from(date = nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/redistat/finder.rb', line 110

def from(date = nil)
  return options[:from] if date.nil?
  reset! if options[:from] != date
  options[:from] = date
  self
end

#interval(unit = nil) ⇒ Object



132
133
134
135
136
137
# File 'lib/redistat/finder.rb', line 132

def interval(unit = nil)
  return options[:interval] if unit.nil?
  reset! if options[:interval] != unit
  options[:interval] = unit
  self
end

#label(input = nil) ⇒ Object



98
99
100
101
102
103
# File 'lib/redistat/finder.rb', line 98

def label(input = nil)
  return options[:label] if input.nil?
  reset! if options.has_key?(:label) && options[:label].to_s != input.to_s
  options[:label] = (!input.nil?) ? Label.new(input) : nil
  self
end

#map(&block) ⇒ Object



66
67
68
# File 'lib/redistat/finder.rb', line 66

def map(&block)
  all.map(&block)
end

#parentObject



74
75
76
# File 'lib/redistat/finder.rb', line 74

def parent
  @parent ||= self.class.new(options.merge(:label => options[:label].parent)) unless options[:label].nil?
end

#scope(input = nil) ⇒ Object



91
92
93
94
95
96
# File 'lib/redistat/finder.rb', line 91

def scope(input = nil)
  return options[:scope] if input.nil?
  reset! if !options[:scope].nil? && options[:scope].to_s != input.to_s
  options[:scope] = Scope.new(input)
  self
end

#till(date = nil) ⇒ Object Also known as: until



117
118
119
120
121
122
# File 'lib/redistat/finder.rb', line 117

def till(date = nil)
  return options[:till] if date.nil?
  reset! if options[:till] != date
  options[:till] = date
  self
end

#totalObject



58
59
60
# File 'lib/redistat/finder.rb', line 58

def total
  all.total
end