Class: Opinions::Opinion

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Opinion

Returns a new instance of Opinion.



121
122
123
124
125
# File 'lib/opinions.rb', line 121

def initialize(args = {})
  @target, @object, @opinion, @created_at =
    args.fetch(:target), args.fetch(:object), args.fetch(:opinion), args.fetch(:created_at, nil)
  self
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



119
120
121
# File 'lib/opinions.rb', line 119

def created_at
  @created_at
end

#objectObject

Returns the value of attribute object.



119
120
121
# File 'lib/opinions.rb', line 119

def object
  @object
end

#opinionObject

Returns the value of attribute opinion.



119
120
121
# File 'lib/opinions.rb', line 119

def opinion
  @opinion
end

#targetObject

Returns the value of attribute target.



119
120
121
# File 'lib/opinions.rb', line 119

def target
  @target
end

Instance Method Details

#==(other_opinion) ⇒ Object

Raises:

  • (ArgumentError)


153
154
155
156
157
158
159
# File 'lib/opinions.rb', line 153

def ==(other_opinion)
  raise ArgumentError, "Can't compare a #{other_opinion} with #{self}" unless other_opinion.is_a?(Opinion)
  opinion_equal  = self.opinion == other_opinion.opinion
  opinion_target = self.target  == other_opinion.target
  opinion_object = self.object  == other_opinion.object
  opinion_equal && opinion_target && opinion_object
end

#exists?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/opinions.rb', line 142

def exists?
  tk = backend.read_sub_key(target_key, object.id.to_s)
  ok = backend.read_sub_key(object_key, target.id.to_s)
  tk && ok
end

#object_keyObject



134
135
136
# File 'lib/opinions.rb', line 134

def object_key
  KeyBuilder.new(object: object, opinion: opinion, target: target).key
end

#persist(args = {time: Time.now.utc}) ⇒ Object



127
128
129
130
131
132
# File 'lib/opinions.rb', line 127

def persist(args = {time: Time.now.utc})
  backend.write_keys({
    target_key => {object.id.to_s => args.fetch(:time)},
    object_key => {target.id.to_s => args.fetch(:time)},
  })
end

#removeObject



148
149
150
151
# File 'lib/opinions.rb', line 148

def remove
  backend.remove_sub_keys([[target_key, object.id.to_s],
                           [object_key, target.id.to_s]])
end

#target_keyObject



138
139
140
# File 'lib/opinions.rb', line 138

def target_key
  KeyBuilder.new(object: target, opinion: opinion, target: object).key
end