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 31 32 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 26 def clear(owner) CassandraObject::Base.with_connection(owner.key, :write) do 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 end |
#column_family ⇒ Object
74 75 76 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 74 def column_family @owner_class.relationships_column_family end |
#connection ⇒ Object
78 79 80 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 78 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
34 35 36 37 38 39 40 41 42 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 34 def find(owner) CassandraObject::Base.with_connection(owner.key.to_s, :read) do 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 end |
#has_inverse? ⇒ Boolean
66 67 68 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 66 def has_inverse? @options[:inverse_of] end |
#inverse ⇒ Object
70 71 72 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 70 def inverse has_inverse? && target_class.associations[@options[:inverse_of]] end |
#new_key ⇒ Object
58 59 60 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 58 def new_key SimpleUUID::UUID.new end |
#new_proxy(owner) ⇒ Object
86 87 88 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 86 def new_proxy(owner) # OneToManyAssociationProxy.new(self, owner) end |
#set(owner, record, set_inverse = true) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 44 def set(owner, record, set_inverse = true) clear(owner) key = owner.key attributes = {@association_name=>{new_key=>record.key.to_s}} CassandraObject::Base.with_connection(key, :write) do 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 end if has_inverse? && set_inverse inverse.set_inverse(record, owner) end end |
#set_inverse(owner, record) ⇒ Object
62 63 64 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 62 def set_inverse(owner, record) set(owner, record, false) end |
#target_class ⇒ Object
82 83 84 |
# File 'lib/cassandra_object/associations/one_to_one.rb', line 82 def target_class @target_class ||= @target_class_name.constantize end |