Class: Moura::Model::Diff
- Inherits:
-
Object
- Object
- Moura::Model::Diff
- Defined in:
- lib/moura/model/diff.rb
Instance Attribute Summary collapse
-
#diff ⇒ Object
readonly
Returns the value of attribute diff.
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(local_file) ⇒ Diff
constructor
A new instance of Diff.
- #merge_role_apps(role, add, remove) ⇒ Object
- #parse_hash_diff(data) ⇒ Object
- #validate_diff_data ⇒ Object
Constructor Details
#initialize(local_file) ⇒ Diff
Returns a new instance of Diff.
12 13 14 15 16 17 18 19 20 |
# File 'lib/moura/model/diff.rb', line 12 def initialize(local_file) @local = Local.new(local_file) @remote = Remote.new hash_diff = Hashdiff.diff(@remote.dump, @local.dump) @diff = parse_hash_diff(hash_diff) validate_diff_data end |
Instance Attribute Details
#diff ⇒ Object (readonly)
Returns the value of attribute diff.
10 11 12 |
# File 'lib/moura/model/diff.rb', line 10 def diff @diff end |
Instance Method Details
#apply ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/moura/model/diff.rb', line 53 def apply @diff.each do |role, attr| case attr[:action] when :add @remote.create_role(role) when :remove @remote.delete_role(role) next end apps = attr[:apps] add_apps = apps[:add] remove_apps = apps[:remove] if add_apps.present? || remove_apps.present? merged_app_ids = merge_role_apps(role, apps[:add], apps[:remove]) @remote.set_role_apps(role, merged_app_ids) end users = attr[:users] @remote.add_role_users(role, users[:add]) @remote.remove_role_users(role, users[:remove]) end end |
#merge_role_apps(role, add, remove) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/moura/model/diff.rb', line 77 def merge_role_apps(role, add, remove) current_apps = @remote.role(role).apps add_apps = add.map { |name| @remote.find_app_id_by_name(name) } remove_apps = remove.map { |name| @remote.find_app_id_by_name(name) } current_apps + add_apps - remove_apps end |
#parse_hash_diff(data) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/moura/model/diff.rb', line 22 def parse_hash_diff(data) data.each_with_object({}) do |(mark, target, diff), result| (role_name, child) = target.match(/^(.+?)(?:\.(apps|users)(?:\[\d+\])?)?$/).captures action = mark == "+" ? :add : :remove unless result[role_name] result[role_name] = { action: :none, apps: { add: [], remove: [] }, users: { add: [], remove: [] } } end if child result[role_name][child.to_sym][action] <<= diff else result[role_name][:action] = action result[role_name][:apps][action] += diff.fetch("apps", []) result[role_name][:users][action] += diff.fetch("users", []) end end end |
#validate_diff_data ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/moura/model/diff.rb', line 45 def validate_diff_data apps = @diff.map { |_, v| v[:apps][:add] }.flatten.sort.uniq @remote.validate_apps(apps) emails = @diff.map { |_, v| v[:users][:add] }.flatten.sort.uniq @remote.validate_emails(emails) end |