Module: ByStar::Vanilla

Included in:
ClassMethods
Defined in:
lib/by_star/vanilla.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/by_star/vanilla.rb', line 256

def method_missing(method, *args)
    if method.to_s =~ /^(as_of|up_to)_(.+)$/
      method = $1
      expr = $2.humanize
      unless time = parse(expr)
        raise ParseError, "Chronic couldn't work out #{expr.inspect}; please be more precise."
      end

      reference = args.first || Time.now

      if "as_of" == method
        between(time, reference)
      else
        between(reference, time)
      end
    else
      super
    end
end

Instance Method Details

#by_current_weekend(options = {}, &block) ⇒ Object

Examples: Post.by_current_weekend



114
115
116
117
118
119
120
121
# File 'lib/by_star/vanilla.rb', line 114

def by_current_weekend(options = {}, &block)
  time = Time.zone.now
  # Friday, 3pm
  start_time = time.beginning_of_weekend
  # Monday, 3am
  end_time = time.end_of_weekend
  by_star(start_time, end_time, options, &block)
end

#by_current_work_week(options = {}, &block) ⇒ Object

Examples: Post.by_current_work_week



125
126
127
128
129
130
131
132
133
# File 'lib/by_star/vanilla.rb', line 125

def by_current_work_week(options = {}, &block)
  time = Time.zone.now
  # Monday, 3am
  time = time + 1.week if time.wday == 6 || time.wday == 0
  start_time = time.beginning_of_week + 3.hours
  # Friday, 3pm
  end_time = time.beginning_of_weekend
  by_star(start_time, end_time, options, &block)
end

#by_day(time = Time.zone.now, options = {}, &block) ⇒ Object Also known as: today

Examples:

Post.by_day
Post.by_day(Time.yesterday)
Post.by_day("next tuesday")


140
141
142
143
# File 'lib/by_star/vanilla.rb', line 140

def by_day(time = Time.zone.now, options = {}, &block)
  time = parse(time)
  by_star(time.beginning_of_day, time.end_of_day, options, &block)
end

#by_fortnight(time = Time.zone.now, options = {}, &block) ⇒ Object

Examples:

# 18th fortnight of 2004
Post.by_fortnight(18, :year => 2004)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/by_star/vanilla.rb', line 38

def by_fortnight(time=Time.zone.now, options = {}, &block)
  time = parse(time)

  # If options[:year] is passed in, use that year regardless.
  year = work_out_year(options[:year]) if options[:year]
  # If the first argument is a date or time, ask it for the year
  year ||= time.year unless time.is_a?(Numeric)
  # If the first argument is a fixnum, assume this year.
  year ||= Time.zone.now.year

  # Dodgy!
  # Surely there's a method in Rails to do this.
  start_time = if valid_time_or_date?(time)
    time.beginning_of_year + (time.strftime("%U").to_i).weeks
  elsif time.is_a?(Numeric) && time <= 26
    Time.utc(year, 1, 1) + ((time.to_i) * 2).weeks
  else
    raise ParseError, "by_fortnight takes only a Time or Date object, a Fixnum (less than or equal to 26) or a Chronicable string."
  end
  start_time = start_time.beginning_of_week
  end_time = start_time + 2.weeks
  by_star(start_time, end_time, options, &block)
end

#by_month(time = Time.zone.now.month, options = {}, &block) ⇒ Object

Examples:

by_month(1)
by_month("January")
by_month("January", :year => 2008)
by_month(time)


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

def by_month(time=Time.zone.now.month, options={}, &block)
  time = Time.zone.now.month if time.nil?
  year, month = work_out_month(time, options.delete(:year))

  start_time = start_of_month(month, year)
  end_time = start_time.end_of_month

  by_star(start_time, end_time, options, &block)
end

#by_week(time = Time.zone.now, options = {}, &block) ⇒ Object

Examples:

# 36th week
Post.by_week(36)
Post.by_week(36.54)
Post.by_week(36, :year => 2004)
Post.by_week(<Time object>)
Post.by_week(<Date object>)
Post.by_week("next tuesday")


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/by_star/vanilla.rb', line 70

def by_week(time=Time.zone.now, options = {}, &block)
  time = parse(time)

  # If options[:year] is passed in, use that year regardless.
  year = work_out_year(options.delete(:year)) if options[:year]
  # If the first argument is a date or time, ask it for the year
  year ||= time.year unless time.is_a?(Numeric)
  # If the first argument is a fixnum, assume this year.
  year ||= Time.zone.now.year

  # Dodgy!
  # Surely there's a method in Rails to do this.
  start_time = if valid_time_or_date?(time)
    weeks = time.strftime("%U").to_i
    Time.zone.now.beginning_of_year
  elsif time.is_a?(Numeric) && time < 53
    weeks = time.to_i
    Time.utc(year, 1, 1)
  else
    raise ParseError, "by_week takes only a Time or Date object, a Fixnum (less than or equal to 53) or a Chronicable string."
  end
  # Make the start of the week predictably Sunday.
  start_time.strftime("%w").to_i != 0 ? start_time - (7 - start_time.strftime("%w").to_i).days : start_time
  start_time += weeks.weeks
  end_time = start_time + 1.week
  by_star(start_time, end_time, options, &block)
end

#by_weekend(time = Time.now, options = {}, &block) ⇒ Object

Examples:

Post.by_weekend
Post.by_weekend(Time.now + 5.days)
Post.by_weekend(Date.today + 5)
Post.by_weekend("next tuesday")


104
105
106
107
108
109
# File 'lib/by_star/vanilla.rb', line 104

def by_weekend(time=Time.now, options = {}, &block)
  time = parse(time)
  start_time = time.beginning_of_weekend
  end_time = (start_time + 1.day).end_of_day
  by_star(start_time, end_time, options, &block)
end

#by_year(time = Time.zone.now.year, options = {}, &block) ⇒ Object

Examples:

by_year(2010)
# 2-digit year:
by_year(10)
# Time or Date object:
by_year(time)
# String:
by_year("2010")


11
12
13
14
15
16
17
18
# File 'lib/by_star/vanilla.rb', line 11

def by_year(time=Time.zone.now.year, options={}, &block)
  year = work_out_year(time)
  start_time = start_of_year(year)
  end_time = start_time.end_of_year
  by_star(start_time, end_time, options, &block)
rescue ArgumentError
  raise ParseError, "Invalid arguments detected, year may possibly be outside of valid range (1902-2039). This is no longer a problem on Ruby versions >= 1.8.7, so we recommend you upgrade to at least 1.8.7."
end

#future(time = Time.zone.now, options = {}, &block) ⇒ Object

Scopes to records newer than current or given time



177
178
179
180
# File 'lib/by_star/vanilla.rb', line 177

def future(time = Time.zone.now, options = {}, &block)
  time = parse(time)
  by_direction(">", time, options, &block)
end

#past(time = Time.zone.now, options = {}, &block) ⇒ Object

Scopes to records older than current or given time Post.past Post.past()



171
172
173
174
# File 'lib/by_star/vanilla.rb', line 171

def past(time = Time.zone.now, options = {}, &block)
  time = parse(time)
  by_direction("<", time, options, &block)
end

#tomorrow(time = Time.zone.now, options = {}, &block) ⇒ Object

Examples:

Post.tomorrow
# 2 days from now:
Post.tomorrow(Time.tomorrow)
# day after next tuesday
Post.tomorrow("next tuesday")


163
164
165
166
# File 'lib/by_star/vanilla.rb', line 163

def tomorrow(time = Time.zone.now, options = {}, &block)
  time = parse(time)
  by_day(time.advance(:days => 1), options, &block)
end

#yesterday(time = Time.zone.now, options = {}, &block) ⇒ Object

Examples:

Post.yesterday
# 2 days ago:
Post.yesterday(Time.yesterday)
# day before next tuesday
Post.yesterday("next tuesday")


152
153
154
155
# File 'lib/by_star/vanilla.rb', line 152

def yesterday(time = Time.zone.now, options = {}, &block)
  time = parse(time)
  by_day(time.advance(:days => -1), options, &block)
end