Class: CassandraObject::Associations::OneToOne
- Inherits:
-
Object
- Object
- CassandraObject::Associations::OneToOne
- Defined in:
- lib/cassandra_object/associations/one_to_one.rb
Instance Method Summary collapse
- #clear(owner) ⇒ Object
- #column_family ⇒ Object
- #connection ⇒ Object
- #define_methods! ⇒ Object
- #find(owner) ⇒ Object
- #has_inverse? ⇒ Boolean
-
#initialize(association_name, owner_class, options) ⇒ OneToOne
constructor
A new instance of OneToOne.
- #inverse ⇒ Object
- #new_key ⇒ Object
- #new_proxy(owner) ⇒ Object
- #set(owner, record, set_inverse = true) ⇒ Object
- #set_inverse(owner, record) ⇒ Object
- #target_class ⇒ Object
Constructor Details
#initialize(association_name, owner_class, options) ⇒ OneToOne
Returns a new instance of OneToOne.
4 5 6 7 8 9 10 11 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 4 def initialize(association_name, owner_class, ) @association_name = association_name.to_s @owner_class = owner_class @target_class_name = [:class_name] || association_name.to_s.camelize @options = define_methods! end |
Instance Method Details
#clear(owner) ⇒ Object
26 27 28 29 30 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 26 def clear(owner) ActiveSupport::Notifications.instrument("remove.cassandra_object", :column_family => column_family, :key => owner.key, :columns => @association_name) do connection.remove(column_family, owner.key.to_s, @association_name) end end |
#column_family ⇒ Object
68 69 70 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 68 def column_family @owner_class.relationships_column_family end |
#connection ⇒ Object
72 73 74 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 72 def connection @owner_class.connection end |
#define_methods! ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 13 def define_methods! @owner_class.class_eval <<-eos def #{@association_name} @_#{@association_name} ||= self.class.associations[:#{@association_name}].find(self) end def #{@association_name}=(record) @_#{@association_name} = record self.class.associations[:#{@association_name}].set(self, record) end eos end |
#find(owner) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 32 def find(owner) if key = connection.get(column_family, owner.key.to_s, @association_name.to_s, :count=>1).values.first target_class.get(key) else nil end end |
#has_inverse? ⇒ Boolean
60 61 62 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 60 def has_inverse? @options[:inverse_of] end |
#inverse ⇒ Object
64 65 66 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 64 def inverse has_inverse? && target_class.associations[@options[:inverse_of]] end |
#new_key ⇒ Object
52 53 54 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 52 def new_key SimpleUUID::UUID.new end |
#new_proxy(owner) ⇒ Object
80 81 82 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 80 def new_proxy(owner) # OneToManyAssociationProxy.new(self, owner) end |
#set(owner, record, set_inverse = true) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 40 def set(owner, record, set_inverse = true) clear(owner) key = owner.key attributes = {@association_name=>{new_key=>record.key.to_s}} ActiveSupport::Notifications.instrument("insert.cassandra_object", :column_family => column_family, :key => key, :attributes => attributes) do connection.insert(column_family, key.to_s, attributes, :consistency => @owner_class.thrift_write_consistency) end if has_inverse? && set_inverse inverse.set_inverse(record, owner) end end |
#set_inverse(owner, record) ⇒ Object
56 57 58 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 56 def set_inverse(owner, record) set(owner, record, false) end |
#target_class ⇒ Object
76 77 78 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 76 def target_class @target_class ||= @target_class_name.constantize end |