Class: Person

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bday) ⇒ Person

Returns a new instance of Person.



23
24
25
26
27
28
29
30
31
# File 'lib/birthdays.rb', line 23

def initialize(name,bday)
	@name=name
	if bday.class == Date then
		@bday=bday
	else
		@bday = DR::TimeParse.parse(bday).to_date
	end
	@ybday=Date.parse("#{@bday.month}/#{@bday.day}")
end

Instance Attribute Details

#bdayObject

Returns the value of attribute bday.



7
8
9
# File 'lib/birthdays.rb', line 7

def bday
  @bday
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/birthdays.rb', line 7

def name
  @name
end

#ybdayObject

Returns the value of attribute ybday.



7
8
9
# File 'lib/birthdays.rb', line 7

def ybday
  @ybday
end

Class Method Details

.get_day(*args, range: :keep) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/birthdays.rb', line 9

def self.get_day(*args, range: :keep)
	return Time.now if args.empty?
	day=args.first
	if Range === day
		case range
		when :begin
			return day.begin
		when :end
			return day.end
		end
	end
	return day
end

Instance Method Details

#<=>(el) ⇒ Object



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

def <=>(el)
	return @ybday <=> el.ybday
end

#age(*day) ⇒ Object



48
49
50
51
# File 'lib/birthdays.rb', line 48

def age(*day)
	day=Person.get_day(*day, range: :end)
	ChronicDuration.output(day - @bday.to_time)
end

#bday_age(*day) ⇒ Object

round the age in years



53
54
55
56
57
58
# File 'lib/birthdays.rb', line 53

def bday_age(*day)
	day=Person.get_day(*day, range: :end).to_date
	age=day.year-@bday.year
	age-=1 if day < @ybday
	return age
end

#birthday?(*day) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/birthdays.rb', line 36

def birthday?(*day)
	day=Person.get_day(*day)
	case day
	when Range
		first=day.begin
		last=day.end
		return (first.month < @bday.month or first.month == @bday.month && first.day <= @bday.day) &&
			(last.month > @bday.month or last.month == @bday.month && last.day >= @bday.day)
	else
		return day.month == @bday.month && day.day == @bday.day
	end
end

#to_s(*day) ⇒ Object



59
60
61
62
# File 'lib/birthdays.rb', line 59

def to_s(*day)
	day=Person.get_day(*day, range: :end)
	@name+" (born #{@bday}, #{bday_age(day)}y)"
end