Class: Pipl::DateRange

Inherits:
Object
  • Object
show all
Defined in:
lib/pipl/fields.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#endObject (readonly)

Returns the value of attribute end.



690
691
692
# File 'lib/pipl/fields.rb', line 690

def end
  @end
end

#startObject (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?, :==

Returns:

  • (Boolean)


706
707
708
# File 'lib/pipl/fields.rb', line 706

def is_exact?
  @start && @end && @start == @end
end

#middleObject



710
711
712
# File 'lib/pipl/fields.rb', line 710

def middle
  @start && @end ? @start + ((@end - @start) / 2) : @start || @end
end

#to_hashObject



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_rangeObject



714
715
716
# File 'lib/pipl/fields.rb', line 714

def years_range
  [@start.year, @end.year] if @start && @end
end