Class: Friendable::Friendship

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_resource, target_resource, attrs = {}) ⇒ Friendship

Returns a new instance of Friendship.



8
9
10
11
12
13
# File 'lib/friendable/friendship.rb', line 8

def initialize(source_resource, target_resource, attrs = {})
  # options = attrs.delete(:options) if attrs[:options]
  @source_resource = source_resource
  @target_resource = target_resource
  @attributes = attrs.reverse_merge!(:created_at => nil, :updated_at => nil).symbolize_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



49
50
51
52
53
54
55
56
57
# File 'lib/friendable/friendship.rb', line 49

def method_missing(method, *args, &block)
  if method.to_s.last == "=" && @attributes.has_key?(method.to_s[0..-2].to_sym)
    write_attribute(method.to_s[0..-2], args.first)
  elsif @attributes.has_key?(method)
    return @attributes[method]
  else
    super(method, args, block)
  end
end

Instance Attribute Details

#source_resourceObject

Returns the value of attribute source_resource.



5
6
7
# File 'lib/friendable/friendship.rb', line 5

def source_resource
  @source_resource
end

#target_resourceObject Also known as: friend

Returns the value of attribute target_resource.



5
6
7
# File 'lib/friendable/friendship.rb', line 5

def target_resource
  @target_resource
end

Class Method Details

.deserialize!(source_resource, target_resource, options) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/friendable/friendship.rb', line 39

def self.deserialize!(source_resource, target_resource, options)
  attrs = MessagePack.unpack(options).symbolize_keys
  attrs[:created_at] = Time.zone.at(attrs[:created_at])
  attrs[:updated_at] = Time.zone.at(attrs[:updated_at])

  Friendship.new(source_resource, target_resource, attrs)
end

Instance Method Details

#inspectObject

Get a pretty string representation of the friendship, including the user who is a friend with and attributes for inspection.



33
34
35
36
37
# File 'lib/friendable/friendship.rb', line 33

def inspect
  "#<Friendable::Friendship with: #{@target_resource.class}(id: #{@target_resource.id}), " <<
  "attributes: " <<
  @attributes.to_a.map{|ary| ary.join(": ") }.join(", ") << ">"
end

#saveObject



19
20
21
22
23
24
# File 'lib/friendable/friendship.rb', line 19

def save
  write_attribute(:created_at, Time.zone.now) unless @attributes[:created_at]
  write_attribute(:updated_at, Time.zone.now)

  Friendable.redis.hset(redis_key, target_resource.id, self.to_msgpack)
end

#to_msgpackObject



26
27
28
# File 'lib/friendable/friendship.rb', line 26

def to_msgpack
  serializable.to_msgpack
end

#write_attribute(attr_name, value) ⇒ Object



15
16
17
# File 'lib/friendable/friendship.rb', line 15

def write_attribute(attr_name, value)
  @attributes[attr_name.to_sym] = value
end