Class: Marta::PageArithmetic::MethodMerger
- Inherits:
-
Object
- Object
- Marta::PageArithmetic::MethodMerger
- Includes:
- ElementInformation
- Defined in:
- lib/marta/page_arithmetic.rb
Overview
It is believed that no user will use it
This class is used to merge hashes of elementsmethods
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.
Instance Method Summary collapse
-
#do_arithmetic(first, second, what) ⇒ Object
Recursive operations with method.
-
#do_collection ⇒ Object
Main method for adding two elements into a large-wide collection.
-
#extract(first, second) ⇒ Object
That will take out of the result all options of second.
-
#forget_unstable ⇒ Object
When black magic finds something she’s not trusting dynamic attribute anymore.
-
#initialize(main_hash, second_hash) ⇒ MethodMerger
constructor
Class is taking two hashes.
-
#merge(first, second) ⇒ Object
That is not a real merge.
-
#multiply(first, second) ⇒ Object
That will leave only the same options in the result.
-
#summarize(first, second) ⇒ Object
Simple adding everyting to everything.
Constructor Details
#initialize(main_hash, second_hash) ⇒ MethodMerger
Class is taking two hashes. Sometimes order is valuable
25 26 27 28 |
# File 'lib/marta/page_arithmetic.rb', line 25 def initialize(main_hash, second_hash) @main_hash = main_hash @second_hash = second_hash end |
Instance Method Details
#do_arithmetic(first, second, what) ⇒ Object
Recursive operations with method.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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 ⇒ Object
Main method for adding two elements into a large-wide collection
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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) ⇒ Object
That will take out of the result all options of second
Idea extract(1,2,2) #=> 1
118 119 120 |
# File 'lib/marta/page_arithmetic.rb', line 118 def extract(first, second) do_arithmetic(first, second, '-') end |
#forget_unstable ⇒ Object
When black magic finds something she’s not trusting dynamic attribute anymore. So she’s forgetting unstable attributes and remembering stable and new ones
52 53 54 55 56 57 58 59 60 |
# 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) ⇒ Object
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(1,1) #=> 1
94 95 96 |
# File 'lib/marta/page_arithmetic.rb', line 94 def merge(first, second) do_arithmetic(second, first, '*') end |
#multiply(first, second) ⇒ Object
That will leave only the same options in the result
Idea: multiply(1,2,1,3) #=> 1
110 111 112 |
# File 'lib/marta/page_arithmetic.rb', line 110 def multiply(first, second) do_arithmetic(first, second, '&') end |
#summarize(first, second) ⇒ Object
Simple adding everyting to everything
Idea: summarize(1,2) #=> 1,2
102 103 104 |
# File 'lib/marta/page_arithmetic.rb', line 102 def summarize(first, second) do_arithmetic(second, do_arithmetic(first, second, '+'), '+') end |