class DiffParams

require 'byebug'

Public Class Methods

call(params_list, renamed = nil) click to toggle source
# File lib/diff_params.rb, line 5
def call(params_list, renamed = nil)
  @params_list1 = params_list.first
  @params_list2 = params_list.last
  @renamed = renamed
  @diff_params = []

  manage_renamed if !renamed.nil?
  compare_params_lists

  @diff_params#.flatten!
end

Private Class Methods

compare() click to toggle source
# File lib/diff_params.rb, line 44
def compare
  new_methods_names,
  removed_methods_names,
  unchanged_methods_names = items_diff(@params1[:methods], @params2[:methods])

  new_methods = label_items(@params2[:methods], new_methods_names, 1)
  removed_methods = label_items(@params1[:methods], removed_methods_names, -1)

  @diff[:methods] << new_methods
  @diff[:methods] << removed_methods
  process_unchanged_methods(unchanged_methods_names)

  remove_empty_methods
  @diff[:methods].flatten!
end
compare_methods(method1, method2) click to toggle source

returns hash with method2 params and diff (e.g. args2 - args1)

# File lib/diff_params.rb, line 61
def compare_methods(method1, method2)
  args_diff = method2[:args] - method1[:args]
  length_diff = method2[:length] - method1[:length]
  conditions_diff = method2[:conditions] - method1[:conditions]
  { name: [method1[:name], 0],
    args: [method2[:args], args_diff],
    length: [method2[:length], length_diff],
    conditions: [method2[:conditions], conditions_diff] }
end
compare_params_lists() click to toggle source
# File lib/diff_params.rb, line 19
def compare_params_lists
  new_params_names,
  removed_params_names,
  unchanged_params_names = items_diff(@params_list1, @params_list2)

  @diff_params << label_items(@params_list2, new_params_names, 1)
  @diff_params << label_items(@params_list1, removed_params_names, -1)

  unchanged_params_names.each do |params_name|
    @diff = { name: params_name, :methods=> [] }
    @params1 = find_item(@params_list1, params_name)
    @params2 = find_item(@params_list2, params_name)

    compare

    @diff[:name] = [@diff[:name], 0]
    @diff_params << @diff
  end
  remove_empty_params
end
find_item(items, name) click to toggle source
# File lib/diff_params.rb, line 86
def find_item(items, name)
  item = items.select { |item| item[:name] == name }
  item.first unless item.nil?
end
items_diff(items1, items2) click to toggle source
# File lib/diff_params.rb, line 71
def items_diff(items1, items2)
  items_names1 = items_names(items1)
  items_names2 = items_names(items2)

  new_items = items_names2 - items_names1
  removed_items = items_names1 - items_names2
  unchanged_items = items_names2 - new_items

  [new_items, removed_items, unchanged_items]
end
items_names(items) click to toggle source
# File lib/diff_params.rb, line 82
def items_names(items)
  items.map { |item| item[:name] }
end
label_item(items, item_name, label) click to toggle source

0 - unchanged, -1 - removed, 1 - new

# File lib/diff_params.rb, line 92
def label_item(items, item_name, label)
  item = find_item(items, item_name)
  item[:name] = [item[:name], label]
  item
end
label_items(items, items_names, label) click to toggle source
# File lib/diff_params.rb, line 98
def label_items(items, items_names, label)
  items_names.map { |item_name| label_item(items, item_name, label) }
end
label_params(params, label) click to toggle source
# File lib/diff_params.rb, line 40
def label_params(params, label)
  params[:name] = [params[:name], label]
end
manage_renamed() click to toggle source
# File lib/diff_params.rb, line 120
def manage_renamed
  @renamed.each do |names|
    rename_items(@params_list1, names, :old)
    rename_items(@params_list2, names, :new)
  end
end
process_unchanged_methods(unchanged_methods_names) click to toggle source
# File lib/diff_params.rb, line 102
def process_unchanged_methods(unchanged_methods_names)
  unchanged_methods_names.each do |method_name|
    method1 = find_item(@params1[:methods], method_name)
    method2 = find_item(@params2[:methods], method_name)
    @diff[:methods] << compare_methods(method1, method2)
  end

  unchanged_methods = label_items(@params1[:methods], unchanged_methods_names, 0)
end
remove_empty_methods() click to toggle source
# File lib/diff_params.rb, line 112
def remove_empty_methods
  @diff[:methods].select! { |item| item != [] }
end
remove_empty_params() click to toggle source
# File lib/diff_params.rb, line 116
def remove_empty_params
  @diff_params.select! { |item| item != [] }
end
rename_items(params_list, names, type) click to toggle source
# File lib/diff_params.rb, line 139
def rename_items(params_list, names, type)
  return params_list.each { |item| rename_old_item(item, names) } if type == :old
  return params_list.each { |item| rename_new_item(item, names) } if type == :new
end
rename_new_item(param_list, names) click to toggle source
# File lib/diff_params.rb, line 135
def rename_new_item(param_list, names)
  param_list[:name] = renamed_name(names) if param_list[:name] == names[:new_name]
end
rename_old_item(param_list, names) click to toggle source
# File lib/diff_params.rb, line 131
def rename_old_item(param_list, names)
  param_list[:name] = renamed_name(names) if param_list[:name] == names[:old_name]
end
renamed_name(names) click to toggle source
# File lib/diff_params.rb, line 127
def renamed_name(names)
  "#{names[:old_name]} > #{names[:new_name]}"
end