Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/time_period/active_record.rb

Overview

Time periods

monkey patch ActiveRecord::Base

© 2012 Peter Horn, metaminded UG

Class Method Summary collapse

Class Method Details

.time_period(name, oopts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/time_period/active_record.rb', line 10

def self.time_period(name, oopts)
  opts = {validate: true, default: nil}.merge oopts

  define_method "#{name}_number" do
    read_attribute(name).split(" ").first.to_i
  end

  define_method "#{name}_unit" do
    read_attribute(name).split(" ").last
  end

  define_method "#{name}_value" do
    n, p = read_attribute(name).split(" ")
    case p
    when 'week'  then n.to_i.week
    when 'month' then n.to_i.month
    when 'day'   then n.to_i.day
    when 'year'  then n.to_i.year
    else raise "wrong format"
    end
  end

  define_method "#{name}_number=" do |number|
    n,p = read_attribute(name).split(" ")
    p ||= 'day'
    write_attribute(name, "#{number.to_i} #{p}")
    number
  end

  define_method "#{name}_unit=" do |unit|
    n,p = read_attribute(name).split(" ")
    raise "unsupported unit '#{unit}'" unless %w{day week month year}.member?(unit.to_s)
    write_attribute(name, "#{n.to_i} #{unit}")
    unit
  end

  define_method "#{name}=" do |n_u|
    number,unit = if n_u.is_a?(String)
      n_u.split(" ")
    elsif n_u.is_a?(ActiveSupport::Duration)
      u,n = n_u.parts.flatten
      [n.to_i, u.to_s.singularize]
    else raise "Can't assign '#{n_u}' to '#{name}'."
    end
    raise "unsupported unit '#{unit}'" unless %w{day week month year}.member?(unit.to_s)
    write_attribute(name, "#{number.to_i} #{unit}")
    n_u
  end

  validates_format_of(name, :with => /^\d+ ((day)|(week)|(month)|(year))$/) if opts[:validate]

  before_validation do
    self.send("#{name}=", opts[:default]) unless read_attribute(name).present?
  end if opts[:default]

end