Module: RailsMatching

Defined in:
lib/rails_matching.rb,
lib/rails_matching/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.against_itself(model, key = "id", exclude_attrs = []) ⇒ Object

Matchs one model instance against all model instances by default it excludes common table fields as id, created at, updated at runs one model against another twice



29
30
31
32
33
34
35
36
37
38
39
40
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
66
67
# File 'lib/rails_matching.rb', line 29

def self.against_itself(model, key = "id", exclude_attrs = [])
  
  validates_model(model)
  
  exclude_attrs += %w[ id created_at updated_at ]
  model_attrs = model.new.attributes.keys - exclude_attrs

  # For each instance of a model
  # Gets its atributes
  # See if it matches with the same attribute of others instances

  # Returns model key and it's percentage of match against another model like
  # [ "a1", "a2", 99.9 ]
  # [ a1, a3, 99.9 ]
  # [ a1, a4, 45.9 ]
  # [ a3, a1, 99.9 ]

  result = model.all.map{
    |a|
    mathed = model.all.map{
      |b|
      #do not run againts same instance
      if a.id != b.id then
        matched_attrs = model_attrs.map{
          |k|
          a[k] == b[k] ? true : false
        }
        count = matched_attrs.count{ |e| e == true }
        percentage = ( count * 100 ) / model_attrs.count
        [ a[key], b[key], percentage ]
      else
      end
    }
    # return without nil values
    mathed.compact
  }

  result
end

.instance_against_all(instance, model, required_match_fields = [], key = "id", exclude_attrs = []) ⇒ Object

Matchs one instance against all instances from a model by default it excludes common table fields as id, created at, updated at



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rails_matching.rb', line 91

def self.instance_against_all(instance, model, required_match_fields = [], key = "id", exclude_attrs = [])
	
	validates_model(model)
	
	exclude_attrs += %w[ id created_at updated_at ]
  model_attrs = model.new.attributes.keys - exclude_attrs

  # For each instance of a model
  # Gets its atributes
  # See if it matches with the same attribute instance

  # Returns instances keys and it's percentage of match
  # [ a1, a3, 99.9 ]
  # [ a1, a4, 45.9 ]
  # [ a3, a1, 99.9 ]

  result = model.all.map{
    |a|
    #do not run againts same instance
    if a.id != instance.id then
      matched_attrs = model_attrs.map{
        |attribute|
        a[attribute] == instance[attribute] ? true : false
      }
      count = matched_attrs.count{ |a| a == true }
      percentage = ( count * 100 ) / model_attrs.count
      [ instance[key], a[key], percentage ]
    else
    end
  }

  result.compact

end

.parametrize(key = "id", exclude_attrs = [], map_params = []) ⇒ Object

Sets common parameters between diferent methods of matchs By default excludes common table fields as id, created at, updated at Sets id of an object as default response You can set map one param to another as:

[“model1_attr1”, “model2_attr1”], [“model1_attr2”, “model2_attr4”]
[“band”, “favorite_band”], [“age”, “years”]

This is gonna be nice with one model has different attrinbute name from another model



13
14
15
# File 'lib/rails_matching.rb', line 13

def self.parametrize(key = "id", exclude_attrs = [], map_params = [])
  exclude_attrs += %w[ id created_at updated_at ] if exclude_attrs.count == 0  
end

.validates_model(model) ⇒ Object



17
18
19
20
21
# File 'lib/rails_matching.rb', line 17

def self.validates_model(model)
  raise "Error, first param must be an object" if !model.first.is_a? Object
  raise "Error, first params must have at least one attribute" if model.first.attributes.count <= 0
  raise "Error, first param must have and id" if model.first.id.nil?  
end