Class: Opinions::Opinion
- Inherits:
-
Object
- Object
- Opinions::Opinion
- Defined in:
- lib/opinions.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#object ⇒ Object
Returns the value of attribute object.
-
#opinion ⇒ Object
Returns the value of attribute opinion.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
- #==(other_opinion) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(args = {}) ⇒ Opinion
constructor
A new instance of Opinion.
- #object_key ⇒ Object
- #persist(args = {time: Time.now.utc}) ⇒ Object
- #remove ⇒ Object
- #target_key ⇒ Object
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_at ⇒ Object
Returns the value of attribute created_at.
119 120 121 |
# File 'lib/opinions.rb', line 119 def created_at @created_at end |
#object ⇒ Object
Returns the value of attribute object.
119 120 121 |
# File 'lib/opinions.rb', line 119 def object @object end |
#opinion ⇒ Object
Returns the value of attribute opinion.
119 120 121 |
# File 'lib/opinions.rb', line 119 def opinion @opinion end |
#target ⇒ Object
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
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
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_key ⇒ Object
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 |
#remove ⇒ Object
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_key ⇒ Object
138 139 140 |
# File 'lib/opinions.rb', line 138 def target_key KeyBuilder.new(object: target, opinion: opinion, target: object).key end |