Class: Pipl::DateRange
- Inherits:
-
Object
- Object
- Pipl::DateRange
- Defined in:
- lib/pipl/fields.rb
Instance Attribute Summary collapse
-
#end ⇒ Object
readonly
Returns the value of attribute end.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(start, end_) ⇒ DateRange
constructor
A new instance of DateRange.
-
#is_exact? ⇒ Boolean
def ==(other) other.instance_of?(self.class) && inspect == other.inspect end.
- #middle ⇒ Object
- #to_hash ⇒ Object
- #years_range ⇒ Object
Constructor Details
#initialize(start, end_) ⇒ DateRange
Returns a new instance of DateRange.
692 693 694 695 696 697 698 |
# File 'lib/pipl/fields.rb', line 692 def initialize(start, end_) @start = start @end = end_ if @start && @end && @start > @end @start, @end = @end, @start end end |
Instance Attribute Details
#end ⇒ Object (readonly)
Returns the value of attribute end.
690 691 692 |
# File 'lib/pipl/fields.rb', line 690 def end @end end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
690 691 692 |
# File 'lib/pipl/fields.rb', line 690 def start @start end |
Class Method Details
.from_hash(h) ⇒ Object
722 723 724 725 726 727 |
# File 'lib/pipl/fields.rb', line 722 def self.from_hash(h) start_, end_ = h[:start], h[:end] initializing_start = start_ ? Date.strptime(start_, Pipl::DATE_FORMAT) : nil initializing_end = end_ ? Date.strptime(end_, Pipl::DATE_FORMAT) : nil self.new(initializing_start, initializing_end) end |
.from_years_range(start_year, end_year) ⇒ Object
718 719 720 |
# File 'lib/pipl/fields.rb', line 718 def self.from_years_range(start_year, end_year) self.new(Date.new(start_year, 1, 1), Date.new(end_year, 12, 31)) end |
Instance Method Details
#is_exact? ⇒ Boolean
def ==(other)
other.instance_of?(self.class) && inspect == other.inspect
end
alias_method :eql?, :==
706 707 708 |
# File 'lib/pipl/fields.rb', line 706 def is_exact? @start && @end && @start == @end end |
#middle ⇒ Object
710 711 712 |
# File 'lib/pipl/fields.rb', line 710 def middle @start && @end ? @start + ((@end - @start) / 2) : @start || @end end |
#to_hash ⇒ Object
729 730 731 732 733 734 |
# File 'lib/pipl/fields.rb', line 729 def to_hash h = {} h[:start] = @start.strftime(Pipl::DATE_FORMAT) if @start h[:end] = @end.strftime(Pipl::DATE_FORMAT) if @end h end |
#years_range ⇒ Object
714 715 716 |
# File 'lib/pipl/fields.rb', line 714 def years_range [@start.year, @end.year] if @start && @end end |