Class: Widget
- Inherits:
-
Object
- Object
- Widget
- Defined in:
- lib/mancora/widget.rb
Instance Attribute Summary collapse
-
#class_method(str) ⇒ Object
Returns the value of attribute class_method.
-
#class_name(str) ⇒ Object
Returns the value of attribute class_name.
-
#conditions(str) ⇒ Object
Returns the value of attribute conditions.
-
#count_type(str) ⇒ Object
Returns the value of attribute count_type.
-
#end ⇒ Object
Returns the value of attribute end.
-
#field(str) ⇒ Object
Returns the value of attribute field.
-
#intervals(opts) ⇒ Object
Returns the value of attribute intervals.
-
#name ⇒ Object
Returns the value of attribute name.
-
#query(str) ⇒ Object
Returns the value of attribute query.
-
#start ⇒ Object
Returns the value of attribute start.
-
#time(str) ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
-
#get_interval(interval) ⇒ Object
given an interval symbol, return a hash with the correct conditions.
-
#initialize(name, backfill = 0, &block) ⇒ Widget
constructor
A new instance of Widget.
- #lazy_names ⇒ Object
- #run(interval, all_conditions) ⇒ Object
-
#should_i_run?(interval) ⇒ Boolean
given an interval symbol, determine whether or not a query (and relative db insert) should take place.
Constructor Details
#initialize(name, backfill = 0, &block) ⇒ Widget
Returns a new instance of Widget.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mancora/widget.rb', line 5 def initialize(name, backfill = 0, &block) @name = name @conditions = {} instance_eval &block lazy_names #run all backfills plus the original @time backfill.downto(0) do |b| b != 0 ? @backfilling = true : @backfilling = false @run_time = @time - b.hours @intervals.each do |i| time_interval = @query.blank? ? get_interval(i) : {} all_conditions = @conditions.merge(time_interval) run(i, all_conditions) if should_i_run?(i) end end end |
Instance Attribute Details
#class_method(str) ⇒ Object
Returns the value of attribute class_method.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def class_method @class_method end |
#class_name(str) ⇒ Object
Returns the value of attribute class_name.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def class_name @class_name end |
#conditions(str) ⇒ Object
Returns the value of attribute conditions.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def conditions @conditions end |
#count_type(str) ⇒ Object
Returns the value of attribute count_type.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def count_type @count_type end |
#end ⇒ Object
Returns the value of attribute end.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def end @end end |
#field(str) ⇒ Object
Returns the value of attribute field.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def field @field end |
#intervals(opts) ⇒ Object
Returns the value of attribute intervals.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def intervals @intervals end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def name @name end |
#query(str) ⇒ Object
Returns the value of attribute query.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def query @query end |
#start ⇒ Object
Returns the value of attribute start.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def start @start end |
#time(str) ⇒ Object
Returns the value of attribute time.
2 3 4 |
# File 'lib/mancora/widget.rb', line 2 def time @time end |
Instance Method Details
#get_interval(interval) ⇒ Object
given an interval symbol, return a hash with the correct conditions
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mancora/widget.rb', line 91 def get_interval(interval) case interval when :custom return {} when :hourly @start = (@run_time - 1.hour).beginning_of_hour @end = (@run_time - 1.hour).end_of_hour return {@field => @start..@end} when :daily @start = @run_time.yesterday.beginning_of_day @end = @run_time.yesterday.end_of_day return {@field => @start..@end} when :weekly @start = @run_time.yesterday.beginning_of_week @end = @run_time.yesterday.end_of_week return {@field => @start..@end} when :monthly @start = @run_time.yesterday.beginning_of_month @end = @run_time.yesterday.end_of_month return {@field => @start..@end} when :yearly @start = @run_time.yesterday.beginning_of_year @end = @run_time.yesterday.end_of_year return {@field => @start..@end} end end |
#lazy_names ⇒ Object
57 58 59 60 61 |
# File 'lib/mancora/widget.rb', line 57 def lazy_names @time ||= Time.now @field ||= :created_at @count_type ||= :timed end |
#run(interval, all_conditions) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mancora/widget.rb', line 63 def run(interval, all_conditions) puts "Running " + interval.to_s + " " + self.name.to_s if @class_method @class_name.send(@class_method) else if @count_type == :timed result = @class_name.where(all_conditions) elsif @count_type == :total count = (@backfilling ? nil : @class_name.where(@conditions).count) elsif !@query.blank? result = eval(@query) end #create or update record, this way we can backfill without overwriting obj = Mancora::Stat.find_or_initialize_by_name_and_start(name, @start) obj.update_attributes( :name => @name, :start => @start, :end => @end, :intervals => interval, :count => (@count_type == :timed ? result.count : count) ) end end |
#should_i_run?(interval) ⇒ Boolean
given an interval symbol, determine whether or not a query (and relative db insert) should take place
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mancora/widget.rb', line 119 def should_i_run?(interval) if interval == :hourly true elsif interval == :daily && @run_time.hour == 0 true elsif interval == :weekly && @run_time.beginning_of_week == @run_time.beginning_of_hour true elsif interval == :monthly && @run_time.day == 1 && @run_time.hour == 0 true elsif interval == :yearly && @run_time.beginning_of_year == @run_time.beginning_of_hour true else false end end |