Class: Schema::Compare::Comparison

Inherits:
Object
  • Object
show all
Includes:
Initializer
Defined in:
lib/schema/schema/compare/comparison.rb,
lib/schema/schema/compare/comparison/entry.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

Error =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assure_schemas(control, compare) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/schema/schema/compare/comparison.rb', line 29

def self.assure_schemas(control, compare)
  if not control.is_a?(Schema)
    raise Error, "Control object is not an implementation of Schema (Control Class: #{control.class.name})"
  end

  if not compare.is_a?(Schema)
    raise Error, "Compare object is not an implementation of Schema (Compare Class: #{compare.class.name})"
  end
end

.build(control, compare, attribute_names = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/schema/schema/compare/comparison.rb', line 15

def self.build(control, compare, attribute_names=nil)
  assure_schemas(control, compare)

  attribute_names ||= control.class.attribute_names

  if not attribute_names.is_a?(Array)
    attribute_names = [attribute_names]
  end

  entries = build_entries(control, compare, attribute_names)

  new(control.class, compare.class, entries)
end

.build_entries(control, compare, attribute_names) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/schema/schema/compare/comparison.rb', line 39

def self.build_entries(control, compare, attribute_names)
  attribute_names.map do |attribute_name|
    if attribute_name.is_a?(Hash)
      control_name, compare_name = attribute_name.keys.first, attribute_name.values.first
    else
      control_name, compare_name = attribute_name, attribute_name
    end

    build_entry(control_name, control, compare_name, compare)
  end
end

.build_entry(control_name, control, compare_name, compare) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/schema/schema/compare/comparison.rb', line 51

def self.build_entry(control_name, control, compare_name, compare)
  control_class = control.class
  if not control_class.all_attribute_names.include?(control_name)
    raise Error, "Attribute is not defined (Attribute Name: #{control_name.inspect}, Schema Class: #{control_class})"
  end

  compare_class = compare.class
  if not compare_class.all_attribute_names.include?(compare_name)
    raise Error, "Attribute is not defined (Attribute Name: #{compare_name.inspect}, Schema Class: #{compare_class})"
  end

  control_value = control.public_send(control_name)
  compare_value = compare.public_send(compare_name)

  entry = Entry.new(
    control_name,
    control_value,
    compare_name,
    compare_value
  )

  entry
end

Instance Method Details

#attribute_different?(attribute_name) ⇒ Boolean

Returns:



104
105
106
107
108
109
110
111
112
# File 'lib/schema/schema/compare/comparison.rb', line 104

def attribute_different?(attribute_name)
  entry = self[attribute_name]

  if entry.nil?
    raise Error, "No attribute difference entry (Attribute Name: #{attribute_name.inspect})"
  end

  entry.different?
end

#classes_different?Boolean

Returns:



100
101
102
# File 'lib/schema/schema/compare/comparison.rb', line 100

def classes_different?
  control_class != compare_class
end

#different?(attribute_name = nil, ignore_class: nil) ⇒ Boolean

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/schema/schema/compare/comparison.rb', line 82

def different?(attribute_name=nil, ignore_class: nil)
  if not attribute_name.nil?
    return attribute_different?(attribute_name)
  end

  ignore_class ||= false

  if not ignore_class
    return true if classes_different?
  end

  attribute_names.each do |attribute_name|
    return true if attribute_different?(attribute_name)
  end

  false
end

#entries_attribute_namesObject Also known as: attribute_names



8
9
10
# File 'lib/schema/schema/compare/comparison.rb', line 8

def entries_attribute_names
  entries.map { |entry| entry.control_name }
end

#entry(attribute_name) ⇒ Object Also known as: []



75
76
77
78
79
# File 'lib/schema/schema/compare/comparison.rb', line 75

def entry(attribute_name)
  entries.find do |entry|
    entry.control_name == attribute_name
  end
end