Class: Quarter

Inherits:
Range show all
Extended by:
Argumentation
Defined in:
lib/quarter_system/quarter.rb

Constant Summary collapse

DEFAULTS =

Q1

{ :us_fiscal => [ [ 10, 12 ],  # Q1
                 [  1,  3 ],  # Q2
                 [  4,  6 ],  # Q3
                 [  7,  9 ] ] # Q4
}

Constants included from Argumentation

Argumentation::REGES, Argumentation::REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Argumentation

build_arguments

Constructor Details

#initialize(*args) ⇒ Quarter

Returns a new instance of Quarter.



95
96
97
98
99
100
101
# File 'lib/quarter_system/quarter.rb', line 95

def initialize(*args)
  attrs       = args.extract_options!
  self.type   = attrs.delete(:type)
  self.count  = attrs.delete(:number)
  self.number = (self.count.pred % Quarter.number_of_quarters(:type => self.type)).next
  super
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



93
94
95
# File 'lib/quarter_system/quarter.rb', line 93

def count
  @count
end

#numberObject

Returns the value of attribute number.



93
94
95
# File 'lib/quarter_system/quarter.rb', line 93

def number
  @number
end

#typeObject

Returns the value of attribute type.



93
94
95
# File 'lib/quarter_system/quarter.rb', line 93

def type
  @type
end

Class Method Details

.all(*args) ⇒ Object



52
53
54
55
# File 'lib/quarter_system/quarter.rb', line 52

def all(*args)
  type, year = type_and_year_from_args(*args)
  1.upto(number_of_quarters(type)).map { |number| specific(*args.merge_options(:number => number)) }
end

.current(*args) ⇒ Object



61
62
63
# File 'lib/quarter_system/quarter.rb', line 61

def current(*args)
  all(*args.merge_options(:this_year => true)).detect { |rng| rng.include? ::Time.zone_default.today }
end

.current_number(*args) ⇒ Object



65
66
67
# File 'lib/quarter_system/quarter.rb', line 65

def current_number(*args)
  current(*args).number
end

.default_numberObject

depends on time zone.



20
21
22
# File 'lib/quarter_system/quarter.rb', line 20

def default_number
  1
end

.default_typeObject



12
13
14
# File 'lib/quarter_system/quarter.rb', line 12

def default_type
  :us_fiscal
end

.default_yearObject



16
17
18
# File 'lib/quarter_system/quarter.rb', line 16

def default_year
  Time.zone_default.today.year rescue Time.new.year
end

.general(*args) ⇒ Object



57
58
59
# File 'lib/quarter_system/quarter.rb', line 57

def general(*args)
  specific(*args.merge_options(:this_year => true))
end

.method_missing(method_symbol, *arguments, &block) ⇒ Object



86
87
88
89
# File 'lib/quarter_system/quarter.rb', line 86

def method_missing(method_symbol, *arguments, &block)
  ordinals = Integer::ORDINALS
  method_symbol.to_s.match(/^(#{ordinals.join('|')})$/).only_if_a?(MatchData) { |md| specific(ordinals.index(md[1]), *arguments) } || super
end

.number_from_args(*args) ⇒ Object



82
83
84
# File 'lib/quarter_system/quarter.rb', line 82

def number_from_args(*args)
  find_from_args(args, :quey => :number, :class => Numeric, :condition => ".to_s.size < 3")
end

.number_of_quarters(*args) ⇒ Object



32
33
34
# File 'lib/quarter_system/quarter.rb', line 32

def number_of_quarters(*args);
  quarters_table(*args).size
end

.quarters_table(*args) ⇒ Object

Changing this so that table type can be input as an option. This is so that when changing quarter system, the dev can use the with_options to change the type easily for all relevant calls. Ex: Quarter.quarters_table(:uk_fiscal)

Quarter.quarters_table(:type => :uk_fiscal)


28
29
30
# File 'lib/quarter_system/quarter.rb', line 28

def quarters_table(*args)
  DEFAULTS[type_from_args(args)]
end

.specific(*args) ⇒ Object

Sequential returns the Quarter for the correct year depending on the magnitude of the ‘number` argument.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/quarter_system/quarter.rb', line 37

def specific(*args)
  number, year, type  = number_and_year_and_type_from_args(*args)
  index               = number.pred % number_of_quarters(*args)
  opts                = args.extract_options! # at this point, args not needed in its original form.
  
  months_array            = quarters_table(type)[index]
  first_months_array      = quarters_table(type)[0]
  specified_quarter_month =       months_array[0]
  first_quarter_month     = first_months_array[0]
  
  year += opts[:this_year] ?  0 : (number.pred / number_of_quarters) + (first_quarter_month > specified_quarter_month ? 1 : 0)
  
  new(Date.new(year, months_array[0], 1), Date.new(year, months_array[1], 1).end_of_month, :number => number)
end

.type_from_args(*args) ⇒ Object

ARGUMENTS MUST NOT BE SPLATTED INTO THIS. Point is for this method to not be destructive. Also, whatever ‘type` is defined within normal list of arguments overrides any written as a hash value. Thing about this whole file is that `type` is the only argument ever that’s a symbol,

  `year` is the only argument that's ever a Numeric of length == 4,
`number` is the only argument that's ever a Numeric of length < 4.


74
75
76
# File 'lib/quarter_system/quarter.rb', line 74

def type_from_args(*args)
  find_from_args(args, :quey => :type, :class => Symbol)
end

.year_from_args(*args) ⇒ Object



78
79
80
# File 'lib/quarter_system/quarter.rb', line 78

def year_from_args(*args)
  find_from_args(args, :quey => :year, :class => Numeric, :condition => ".to_s.size == 4")
end

Instance Method Details

#include?(date_or_time) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
# File 'lib/quarter_system/quarter.rb', line 103

def include?(date_or_time)
  if date_or_time.is_a?(Time)
    super(date_or_time.in_time_zone(Time.zone_default).to_date)
  else
    super
  end
end