Class: Likes::Like

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

Overview

Job: Understands relation between person and the item they like

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Like

Creates new instance of Like

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to create Like instance with

Options Hash (attributes):

  • :person (Person#==)

    The person under the question. Required

  • :item (Item#==)

    The item person like. Required



9
10
11
12
# File 'lib/likes/like.rb', line 9

def initialize(attributes={})
  @person = fetch_required_attribute(attributes, :person)
  @item = fetch_required_attribute(attributes, :item)
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Tests other object for equality with itself. Objects of type different from Like are considered non-equal.

Parameters:

  • other (Any, Like)

    Other object to test for equality

Returns:

  • (TrueClass, FalseClass)

    Returns true if objects are equal, false otherwise



20
21
22
23
24
# File 'lib/likes/like.rb', line 20

def ==(other)
  return false unless Like === other
  self.person == other.person &&
    self.item == other.item
end

#add_item_to_map(map) ⇒ Object



27
28
29
# File 'lib/likes/like.rb', line 27

def add_item_to_map(map)
  (map[person] ||= []) << item
end

#add_person_to_map(map) ⇒ Object



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

def add_person_to_map(map)
  (map[item] ||= []) << person
end