Module: TokyoWrapper::TableMethods::Associations

Included in:
TokyoWrapper::Table
Defined in:
lib/tokyo_wrapper/table_methods/associations.rb

Instance Method Summary collapse

Instance Method Details

#add_has_many_association_id(id, association_id_name, association_id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tokyo_wrapper/table_methods/associations.rb', line 7

def add_has_many_association_id(id, association_id_name, association_id)
  if !@table[id.to_s].nil? && !@table[id.to_s].empty?
    association_ids_string = @table[id.to_s]["#{association_id_name}s"]
    if association_ids_string.nil?
      @table[id.to_s] = @table[id.to_s].merge({"#{association_id_name}s" => association_id.to_s})
    elsif !association_ids_string.split(",").include?(association_id.to_s)
      @table[id.to_s] = @table[id.to_s].merge({"#{association_id_name}s" => "#{association_ids_string},#{association_id}"})
    end
    true
  else
    false
  end      
end

#all_by_has_many_association_id(association_id_name, association_id, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/tokyo_wrapper/table_methods/associations.rb', line 21

def all_by_has_many_association_id(association_id_name, association_id, options = {})
  result = @table.query do | query | 
    query.add "#{association_id_name}s", :stror, association_id.to_s
  end
  
  keys_for_has_many_association = ["#{association_id_name}s"]
  keys_for_has_many_association.concat(options[:keys_for_has_many_association]).uniq! if !options[:keys_for_has_many_association].nil? && !options[:keys_for_has_many_association].empty?
  convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, keys_for_has_many_association)
end

#keys_for_belongs_to_association_id(association_id_name, association_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tokyo_wrapper/table_methods/associations.rb', line 40

def keys_for_belongs_to_association_id(association_id_name, association_id)
  table_result_set = @table.query do |query|
    query.add association_id_name, :equals, association_id.to_s
    query.no_pk
  end
  keys = []
  table_result_set.each do |row|
    keys += row.keys
    keys.uniq!
  end
  keys.delete(association_id_name)
  keys.sort
end

#set_belongs_to_association_id(id, association_id_name, association_id) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/tokyo_wrapper/table_methods/associations.rb', line 31

def set_belongs_to_association_id(id, association_id_name, association_id)
  if !@table[id.to_s].nil? && !@table[id.to_s].empty?
    @table[id.to_s] = @table[id.to_s].merge({association_id_name => association_id.to_s})
    true
  else
    false
  end
end