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.



687
688
689
690
691
692
693
# File 'lib/pipl/fields.rb', line 687

def initialize(start, end_)
  @start = start
  @end = end_
  if @start and @end and @start > @end
    @start, @end = @end, @start
  end
end

Instance Attribute Details

#endObject (readonly)

Returns the value of attribute end.



685
686
687
# File 'lib/pipl/fields.rb', line 685

def end
  @end
end

#startObject (readonly)

Returns the value of attribute start.



685
686
687
# File 'lib/pipl/fields.rb', line 685

def start
  @start
end

Class Method Details

.from_hash(h) ⇒ Object



717
718
719
720
721
722
# File 'lib/pipl/fields.rb', line 717

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



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

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) and inspect == other.inspect

end

alias_method :eql?, :==

Returns:

  • (Boolean)


701
702
703
# File 'lib/pipl/fields.rb', line 701

def is_exact?
  @start and @end and @start == @end
end

#middleObject



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

def middle
  @start and @end ? @start + ((@end - @start) / 2) : @start or @end
end

#to_hashObject



724
725
726
727
728
729
# File 'lib/pipl/fields.rb', line 724

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



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

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