Module: Toji::Utils

Defined in:
lib/toji/utils.rb

Class Method Summary collapse

Class Method Details

.activemodel_defined?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/toji/utils.rb', line 3

def self.activemodel_defined?
  Object.const_defined?(:ActiveModel) && ActiveModel.const_defined?(:Validations)
end

.check_dup(obj) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/toji/utils.rb', line 7

def self.check_dup(obj)
  if activemodel_defined? && ActiveModel::Validations===obj
    # ok
  else
    if !obj.class.private_method_defined?(:initialize_copy, false)
      raise Error, "implementation required: #{obj.class}.initialize_copy"
    end
  end
end

.merge_ingredients(ingredients1, ingredients2) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/toji/utils.rb', line 17

def self.merge_ingredients(ingredients1, ingredients2)
  result = {}

  ingredients1&.each {|src|
    dst = result[src.ingredient_key]
    if dst
      dst.weight = dst.weight.to_f + src.weight.to_f
    else
      result[src.ingredient_key] = src
    end
  }

  ingredients2&.each {|src|
    dst = result[src.ingredient_key]
    if dst
      dst.weight = dst.weight.to_f + src.weight.to_f
    else
      result[src.ingredient_key] = src.dup
    end
  }

  result.values
end