Class: CassandraObject::Associations::OneToMany
- Inherits:
-
Object
- Object
- CassandraObject::Associations::OneToMany
- Defined in:
- lib/cassandra_object/associations/one_to_many.rb
Instance Method Summary collapse
- #add(owner, record, set_inverse = true) ⇒ Object
- #column_family ⇒ Object
- #connection ⇒ Object
- #define_methods! ⇒ Object
- #find(owner, options = {}) ⇒ Object
- #has_inverse? ⇒ Boolean
-
#initialize(association_name, owner_class, options) ⇒ OneToMany
constructor
A new instance of OneToMany.
- #inverse ⇒ Object
- #new_key ⇒ Object
- #new_proxy(owner) ⇒ Object
- #reversed? ⇒ Boolean
- #set_inverse(owner, record) ⇒ Object
- #target_class ⇒ Object
Constructor Details
#initialize(association_name, owner_class, options) ⇒ OneToMany
Returns a new instance of OneToMany.
4 5 6 7 8 9 10 11 |
# File 'lib/cassandra_object/associations/one_to_many.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.singularize.camelize @options = define_methods! end |
Instance Method Details
#add(owner, record, set_inverse = true) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 19 def add(owner, record, set_inverse = true) 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 |
#column_family ⇒ Object
34 35 36 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 34 def column_family @owner_class.relationships_column_family end |
#connection ⇒ Object
38 39 40 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 38 def connection @owner_class.connection end |
#define_methods! ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 66 def define_methods! @owner_class.class_eval <<-eos def #{@association_name} @_#{@association_name} ||= self.class.associations[:#{@association_name}].new_proxy(self) end eos end |
#find(owner, options = {}) ⇒ Object
13 14 15 16 17 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 13 def find(owner, = {}) reversed = .has_key?(:reversed) ? [:reversed] : reversed? cursor = CassandraObject::Cursor.new(target_class, column_family, owner.key.to_s, @association_name, :start_after => [:start_after], :reversed => reversed) cursor.find([:limit] || 100) end |
#has_inverse? ⇒ Boolean
50 51 52 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 50 def has_inverse? @options[:inverse_of] end |
#inverse ⇒ Object
54 55 56 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 54 def inverse has_inverse? && target_class.associations[@options[:inverse_of]] end |
#new_key ⇒ Object
30 31 32 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 30 def new_key SimpleUUID::UUID.new end |
#new_proxy(owner) ⇒ Object
46 47 48 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 46 def new_proxy(owner) OneToManyAssociationProxy.new(self, owner) end |
#reversed? ⇒ Boolean
62 63 64 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 62 def reversed? @options[:reversed] == true end |
#set_inverse(owner, record) ⇒ Object
58 59 60 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 58 def set_inverse(owner, record) add(owner, record, false) end |
#target_class ⇒ Object
42 43 44 |
# File 'lib/cassandra_object/associations/one_to_many.rb', line 42 def target_class @target_class ||= @target_class_name.constantize end |