Created
March 11, 2019 07:35
-
-
Save dmitryck/bd6d4e7372cf8921a7d5ad48f48c3b07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MirandaService::ActualData | |
attr_reader :contacts | |
attr_reader :new_data | |
attr_reader :renamed_data | |
attr_reader :old_data | |
attr_reader :actual_contacts | |
attr_reader :new_contacts | |
attr_reader :renamed_contacts | |
attr_reader :old_contacts | |
def initialize model: Credential, default_attrs: {} | |
miranda_connect! | |
@model = model | |
@default_attrs = default_attrs | |
make_data | |
end | |
private | |
def miranda_connect! | |
if defined? BlatherInitializer | |
Object.send(:remove_const, :BlatherInitializer) | |
end | |
load File.expand_path('blather/initializer.rb', __dir__) | |
BlatherInitializer.run | |
end | |
def make_data | |
@contacts = contacts_by_key "contact" | |
@actual_data = actual_data_by_key "contact" | |
@new_data = get_new_data | |
@renamed_data = get_renamed_data | |
@old_data = get_old_data | |
@actual_contacts = get_actual_contacts | |
@new_contacts = get_new_contacts | |
@renamed_contacts = get_renamed_contacts | |
@old_contacts = get_old_contacts | |
end | |
def get_actual_contacts | |
to_contacts @actual_data, @default_attrs | |
end | |
def get_new_contacts | |
to_contacts @new_data, @default_attrs | |
end | |
def get_renamed_contacts | |
to_contacts @renamed_data, @default_attrs | |
end | |
def get_old_contacts | |
to_contacts @old_data, @default_attrs | |
end | |
def get_new_data | |
@actual_data.slice(*(@actual_data.keys - @contacts.keys)).select do |contact, data| | |
old?(data) | |
end | |
end | |
def get_renamed_data | |
@actual_data.select do |contact, data| | |
@contacts[contact] && ( | |
diff_fio?(contact, data) && ! old?(data) | |
) | |
end | |
end | |
def get_old_data | |
@actual_data.select do |contact, data| | |
@contacts[contact] && ( | |
diff_fio?(contact, data) && old?(data) | |
) | |
end | |
end | |
def contacts_by_key key | |
to_keyed_hash @model.all.map{|cr| cr.attributes }, key | |
end | |
def actual_data_by_key key | |
to_keyed_hash to_data(BlatherService.users), key | |
end | |
def to_data data_rows, default={} | |
attrs = [] | |
data_rows.each do |data| | |
name = data.keys.first | |
contact = data.values.first[:contact] | |
if name && contact | |
attrs << default.merge({ "fio" => name, "contact" => contact }) | |
end | |
end | |
attrs | |
end | |
def to_contacts data_rows, default={} | |
data_rows.merge(data_rows) do |contact, data| | |
default.merge data | |
end | |
end | |
def to_keyed_hash arr, key | |
arr.map{|e| [e[key], e] }.to_h | |
end | |
def old? data | |
data["fio"] =~ /^old_/ | |
end | |
def diff_fio? contact, data | |
@contacts[contact]["fio"] != data["fio"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment