class Marta::PageArithmetic::MethodMerger

This class is used to merge hashes of elementsmethods

@note It is believed that no user will use it Now it has only one way to merge hashes This method is getting common only of two methods in order to generate a correct hash for collection element.

Public Class Methods

new(main_hash, second_hash) click to toggle source

Class is taking two hashes. Sometimes order is valuable

# File lib/marta/page_arithmetic.rb, line 25
def initialize(main_hash, second_hash)
  @main_hash = main_hash
  @second_hash = second_hash
end

Public Instance Methods

do_arithmetic(first, second, what) click to toggle source

Recursive operations with method.

# File lib/marta/page_arithmetic.rb, line 63
def do_arithmetic(first, second, what)
  what == '+' ? result = second : result = Hash.new
  first.each_pair do |key, value|
    if value.is_a? Hash
      result[key] = do_arithmetic(first[key], second[key], what)
    elsif !second[key].nil? and !value.nil?
      if what == '+'
        result[key] = (first[key] + second[key]).uniq
      elsif what == '&'
        result[key] = first[key] & second[key]
      elsif what == '-'
        result[key] = first[key] - second[key]
      elsif what == '*'
        if (second[key] != [])
          result[key] = first[key] & second[key]
        end
      end
    end
    if (second[key] == [] or second[key].nil?) and
          ((what == '+') or (what == '*'))
      result[key] = first[key]
    end
  end
  result
end
do_collection() click to toggle source

Main method for adding two elements into a large-wide collection

# File lib/marta/page_arithmetic.rb, line 31
def do_collection
  result = method_structure
  # Everything is simple for now with options)
  result['options'] = @main_hash['options']
  # If we are adding to collection
  if @second_hash['positive']['self']['tag'] != []
    result['positive'] = multiply(@main_hash['positive'],
                                  @second_hash['positive'])
    result['negative'] = extract(@main_hash['negative'],
                                 @second_hash['positive'])
  else # If we are excluding from collection
    result['positive'] = @main_hash['positive']
    uniqs = extract(@second_hash['negative'], @main_hash['positive'])
    result['negative'] = summarize(uniqs, @main_hash['negative'])
  end
  result
end
extract(first, second) click to toggle source

That will take out of the result all options of second

Idea extract({a:,b:,c:},{a:,c:,d:}) #=> {a:,b:}

# File lib/marta/page_arithmetic.rb, line 118
def extract(first, second)
  do_arithmetic(first, second, '-')
end
forget_unstable() click to toggle source

When black magic finds something she's not trusting dynamic attribute anymore. So she's forgetting unstable attributes and remembering stable and new ones

# File lib/marta/page_arithmetic.rb, line 52
def forget_unstable
  result = method_structure
  result['options'] = @main_hash['options']
  result['positive'] = merge(@main_hash['positive'],
                                @second_hash['positive'])
  result['negative'] = extract(@main_hash['negative'],
                                @second_hash['positive'])
  result
end
merge(first, second) click to toggle source

That is not a real merge. We are leaving everything that is the same or new and deleting everyting that is not the same

Idea: merge({a:,b:,c:},{a:,b:}) #=> {a:,b:,c:}

# File lib/marta/page_arithmetic.rb, line 94
def merge(first, second)
  do_arithmetic(second, first, '*')
end
multiply(first, second) click to toggle source

That will leave only the same options in the result

Idea: multiply({a:,b:,c:},{a:,b:,d:}) #=> {a:,b:}

# File lib/marta/page_arithmetic.rb, line 110
def multiply(first, second)
  do_arithmetic(first, second, '&')
end
summarize(first, second) click to toggle source

Simple adding everyting to everything

Idea: summarize({a:,c:},{a:,b:}) #=> {a:,b:,c:}

# File lib/marta/page_arithmetic.rb, line 102
def summarize(first, second)
  do_arithmetic(second, do_arithmetic(first, second, '+'), '+')
end