Module: OneTouch::Relations::ClassMethods

Defined in:
lib/one_touch/models/relations.rb

Instance Method Summary collapse

Instance Method Details

#array_hosts_to_hash(origin_relation, hosts, mark) ⇒ Object

Transfer ([User1, User2, User3], mark 5) to { User1 => { User2 => 5, User3 => 5 } …}

and combine the origin relation

the code is a little ugly



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/one_touch/models/relations.rb', line 32

def array_hosts_to_hash(origin_relation,hosts,mark)
  hosts.inject(origin_relation) do |memo,host|
    each_relations= hosts.each_relations_on(host,mark)
    # the old_relation and new_relation both are Hash
    memo.merge!(host => each_relations) do |key,old_r,new_r|
      old_r.merge!(new_r) do |key,old_mark,new_mark|
        old_mark + new_mark
      end
    end
  end
end

#build_relationsObject

build all favorable_to_host_relation results should be like Tag.host_relations



15
16
17
18
19
# File 'lib/one_touch/models/relations.rb', line 15

def build_relations
  favorable_klasses.inject({}) do |memo,klass|
    memo.merge(klass.to_s.constantize.host_relations(target_to_media, media_to_host))
  end
end

#favorable_relationsObject

TODO: add this function



56
57
# File 'lib/one_touch/models/relations.rb', line 56

def favorable_relations
end

#host_relationsObject

Transfer favorable_to_host_relation to host_to_host_relations TODO: use fiber to improve performance TODO: No test for it



47
48
49
50
51
52
53
# File 'lib/one_touch/models/relations.rb', line 47

def host_relations
  h_relation = {}
  build_relations.each do |key, hosts|
    h_relation = array_hosts_to_hash h_relation, hosts, matching_mark(key)
  end
  h_relation
end

#matching_mark(relation_key) ⇒ Object

match relation and relation_rules, if relation did not match any rule, return 0 point This methods can be override by user



23
24
25
26
27
# File 'lib/one_touch/models/relations.rb', line 23

def matching_mark(relation_key)
  @rule ||= relation_rules
  transfer_key = relation_key.map { |ele| ele.is_a?(ActiveRecord::Base) ? ele.class.name.to_sym : ele.to_sym }
  @rule[transfer_key].presence || 0
end