Class: Seinfeld::Habit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Habit

Returns a new instance of Habit.



29
30
31
32
33
34
35
36
# File 'lib/seinfeld.rb', line 29

def initialize(attributes={})
  @attributes = attributes
  if @attributes['entries']
    @entries = @attributes['entries'].map {|attrs| Entry.new(attrs)}
  else
    @entries = []
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/seinfeld.rb', line 60

def method_missing(name, *args)
  if attributes[name.to_s]
    return attributes[name.to_s]
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



38
39
40
# File 'lib/seinfeld.rb', line 38

def attributes
  @attributes
end

#entriesObject (readonly)

Returns the value of attribute entries.



38
39
40
# File 'lib/seinfeld.rb', line 38

def entries
  @entries
end

Instance Method Details

#as_json(options = {}) ⇒ Object



40
41
42
43
44
# File 'lib/seinfeld.rb', line 40

def as_json(options={})
  attributes.merge(
    'entries' => entries.map(&:as_json)
  )
end

#day_countObject



56
57
58
# File 'lib/seinfeld.rb', line 56

def day_count
  @entries.length
end

#has_entry_for_date?(date) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/seinfeld.rb', line 46

def has_entry_for_date?(date)
  @entries.any? {|e| e.date.to_s == date.to_s }
end

#increment!Object



50
51
52
53
54
# File 'lib/seinfeld.rb', line 50

def increment!
  unless has_entry_for_date?(Date.today)
    @entries << Entry.new
  end
end