Class: Opium::Model::Relation

Inherits:
Criteria show all
Includes:
ActiveModel::Dirty
Defined in:
lib/opium/model/relation.rb

Instance Attribute Summary collapse

Attributes inherited from Criteria

#model_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Criteria

#==, #chain, #constraints, #constraints?, #criteria, #inspect, #model, #to_partial_path, #total_count, #uncache, #update_constraint, #update_constraint!, #update_variable, #update_variable!, #variables, #variables?

Methods included from Queryable::ClassMethods

#all, #between, #cache, #cached?, #dont_select, #exists, #gt, #gte, #in, #keys, #limit, #lt, #lte, #ne, #nin, #or, #order, #pluck, #select, #skip, #uncache, #where

Constructor Details

#initialize(model_name) ⇒ Relation

Returns a new instance of Relation.



65
66
67
68
# File 'lib/opium/model/relation.rb', line 65

def initialize( model_name )
  super
  update_variable!( :cache, true )
end

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata.



78
79
80
# File 'lib/opium/model/relation.rb', line 78

def 
  @metadata
end

#ownerObject

Returns the value of attribute owner.



78
79
80
# File 'lib/opium/model/relation.rb', line 78

def owner
  @owner
end

Class Method Details

.to_parse(object) ⇒ Object



7
8
9
10
11
# File 'lib/opium/model/relation.rb', line 7

def to_parse( object )
  class_name = determine_class_name_from( object, 'parse Relation hash' )
  fail ArgumentError, "could not determine class_name from #{ object.inspect }" unless class_name
  { __type: 'Relation', className: class_name }.with_indifferent_access
end

.to_ruby(object) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/opium/model/relation.rb', line 13

def to_ruby( object )
  return if object.nil? || object == []
  return object if object.is_a? self
  class_name = determine_class_name_from( object, self.name )
  new( class_name ).tap do |relation|
    case object
    when String
    when is_descendant.curry[Opium::Model]
      relation.push object
    when contains_descendant.curry[Opium::Model]
      object.select(&is_descendant.curry[Opium::Model]).each {|model| relation.push model}
    end
  end
end

Instance Method Details

#build(params = {}) ⇒ Object Also known as: new



115
116
117
118
119
# File 'lib/opium/model/relation.rb', line 115

def build( params = {} )
  model.new( params || {} ).tap do |instance|
    push instance
  end
end

#delete(object) ⇒ Object



110
111
112
113
# File 'lib/opium/model/relation.rb', line 110

def delete( object )
  __deletions__.push( object )
  self
end

#each(&block) ⇒ Object

TODO: likely will need to reimplement .each



94
95
96
97
98
99
100
101
# File 'lib/opium/model/relation.rb', line 94

def each(&block)
  if !block_given?
    to_enum(:each)
  else
    super() {|model| block.call( model ) unless __deletions__.include?( model ) }
    (__additions__ - __deletions__).each(&block)
  end
end

#empty?Boolean

Returns:



74
75
76
# File 'lib/opium/model/relation.rb', line 74

def empty?
  owner.nil? || owner.new_record? ? true : super
end

#parse_responseObject



130
131
132
# File 'lib/opium/model/relation.rb', line 130

def parse_response
  @parse_response ||= []
end

#push(object) ⇒ Object Also known as: <<



103
104
105
106
# File 'lib/opium/model/relation.rb', line 103

def push( object )
  __additions__.push( object )
  self
end

#saveObject



123
124
125
126
127
128
# File 'lib/opium/model/relation.rb', line 123

def save
  self.reject {|model| model.persisted?}.each(&:save)
  __apply_additions__
  __apply_deletions__
  true
end

#to_parseObject



70
71
72
# File 'lib/opium/model/relation.rb', line 70

def to_parse
  self.class.to_parse self
end