Class: PGArrays::ReferencesBy::RelationHolder

Inherits:
Struct
  • Object
show all
Defined in:
lib/ar_jdbc_pg_array/references_by.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldObject

Returns the value of attribute field

Returns:

  • (Object)

    the current value of field



15
16
17
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 15

def field
  @field
end

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



15
16
17
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 15

def klass
  @klass
end

#relationObject

Returns the value of attribute relation

Returns:

  • (Object)

    the current value of relation



15
16
17
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 15

def relation
  @relation
end

Instance Method Details

#for_referenced(obj_klass) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 41

def for_referenced(obj_klass)
  myself = self
  obj_klass.class_exec do
    puts "named_scope #{NAMED_SCOPE} #{myself.relation}_include"
    self.send NAMED_SCOPE, "#{myself.relation}_include", lambda{|*objs|
      objs = myself.map_to_ids objs.flatten
      where(myself.field.to_sym => objs.search_all)
    }

    self.send NAMED_SCOPE, "#{myself.relation}_have_all", lambda{|*objs|
      objs = myself.map_to_ids objs.flatten
      where(myself.field.to_sym => objs.search_all)
    }

    self.send NAMED_SCOPE, "#{myself.relation}_have_any", lambda{|*objs|
      objs = myself.map_to_ids objs.flatten
      where(myself.field.to_sym => objs.search_any)
    }

    self.send NAMED_SCOPE, "#{myself.relation}_included_into", lambda{|*objs|
      objs = myself.map_to_ids objs.flatten
      where(myself.field.to_sym => objs.search_subarray)
    }
  end
end

#map_to_ids(vals) ⇒ Object



111
112
113
114
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 111

def map_to_ids(vals)
  (r = vals.map{|v| val_to_id(v)}).compact!
  r
end

#referenced(obj) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 17

def referenced(obj)
  ids = (obj[field] || []).map{|i| i.to_i}
  objs = klass.find_all_by_id( ids.sort )
  if ids.size < 20
    objs.sort_by{|o| ids.index(o.id)}
  else
    to_ind = ids.each_with_index.inject({}){|h, (v,i)| h[v]=i; h}
    objs.sort_by{|o| to_ind[o.id]}
  end
end

#set_referenced(obj, value) ⇒ Object



28
29
30
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 28

def set_referenced(obj, value)
  obj[field] = map_to_ids(value)
end

#val_to_id(val) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 103

def val_to_id(val)
  case val
  when ActiveRecord::Base then val.id
  when nil then nil
  else val.to_i
  end
end

#validate(obj) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ar_jdbc_pg_array/references_by.rb', line 33

def validate(obj)
  has_ids = klass.where(:id=>obj.read_attribute(field)).
            select('id').all.map(&:id)
  unless has_ids.sort == obj.read_attribute(field).sort
    obj.errors.add(relation, :wrong_array_reference)
  end
end