Class: Friendable::Friendship
- Inherits:
-
Object
- Object
- Friendable::Friendship
- Defined in:
- lib/friendable/friendship.rb
Instance Attribute Summary collapse
-
#source_resource ⇒ Object
Returns the value of attribute source_resource.
-
#target_resource ⇒ Object
(also: #friend)
Returns the value of attribute target_resource.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(source_resource, target_resource, attrs = {}) ⇒ Friendship
constructor
A new instance of Friendship.
-
#inspect ⇒ Object
Get a pretty string representation of the friendship, including the user who is a friend with and attributes for inspection.
- #save ⇒ Object
- #to_msgpack ⇒ Object
- #write_attribute(attr_name, value) ⇒ Object
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_resource ⇒ Object
Returns the value of attribute source_resource.
5 6 7 |
# File 'lib/friendable/friendship.rb', line 5 def source_resource @source_resource end |
#target_resource ⇒ Object 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, ) attrs = MessagePack.unpack().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
#inspect ⇒ Object
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 |
#save ⇒ Object
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_msgpack ⇒ Object
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 |