Class: RelaxDB::HasOneProxy
- Inherits:
-
Object
- Object
- RelaxDB::HasOneProxy
- Defined in:
- lib/relaxdb/has_one_proxy.rb
Instance Method Summary collapse
-
#initialize(client, relationship) ⇒ HasOneProxy
constructor
A new instance of HasOneProxy.
- #load_target ⇒ Object
- #target ⇒ Object
-
#target=(new_target) ⇒ Object
All database changes performed by this method would ideally be done in a transaction.
Constructor Details
#initialize(client, relationship) ⇒ HasOneProxy
Returns a new instance of HasOneProxy.
5 6 7 8 9 10 11 12 |
# File 'lib/relaxdb/has_one_proxy.rb', line 5 def initialize(client, relationship) @client = client @relationship = relationship @target_class = @relationship.to_s.camel_case @relationship_as_viewed_by_target = client.class.name.snake_case @target = nil end |
Instance Method Details
#load_target ⇒ Object
35 36 37 38 |
# File 'lib/relaxdb/has_one_proxy.rb', line 35 def load_target view_name = "#{@client.class}_#{@relationship}" RelaxDB.view(view_name, :key => @client._id).first end |
#target ⇒ Object
14 15 16 17 |
# File 'lib/relaxdb/has_one_proxy.rb', line 14 def target return @target if @target @target = load_target end |
#target=(new_target) ⇒ Object
All database changes performed by this method would ideally be done in a transaction
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/relaxdb/has_one_proxy.rb', line 20 def target=(new_target) # Nullify any existing relationship on assignment old_target = target if old_target old_target.send("#{@relationship_as_viewed_by_target}=".to_sym, nil) old_target.save end @target = new_target unless @target.nil? @target.send("#{@relationship_as_viewed_by_target}=".to_sym, @client) @target.save end end |