Class: Transpec::Record

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/transpec/record.rb

Constant Summary collapse

TYPES =
[:conversion, :addition, :removal, :modification]
COMPARISON_ATTRIBUTES =
[:type_sort_key, :old_syntax, :new_syntax].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_syntax, new_syntax, options = {}) ⇒ Record

Returns a new instance of Record.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/transpec/record.rb', line 14

def initialize(old_syntax, new_syntax, options = {})
  # Keep these syntax data as symbols for:
  #   * Better memory footprint
  #   * Better summarizing performance in Report
  @old_syntax_type = old_syntax && old_syntax.to_sym
  @new_syntax_type = new_syntax && new_syntax.to_sym

  @type = options[:type]
  @annotation = options[:annotation]

  fail ArgumentError, "Invalid type: #{type}" unless TYPES.include?(type)
end

Instance Attribute Details

#annotationObject (readonly)

Returns the value of attribute annotation.



12
13
14
# File 'lib/transpec/record.rb', line 12

def annotation
  @annotation
end

#new_syntax_typeObject (readonly)

Returns the value of attribute new_syntax_type.



12
13
14
# File 'lib/transpec/record.rb', line 12

def new_syntax_type
  @new_syntax_type
end

#old_syntax_typeObject (readonly)

Returns the value of attribute old_syntax_type.



12
13
14
# File 'lib/transpec/record.rb', line 12

def old_syntax_type
  @old_syntax_type
end

Instance Method Details

#<=>(other) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/transpec/record.rb', line 70

def <=>(other)
  COMPARISON_ATTRIBUTES.each do |attribute|
    result = send(attribute) <=> other.send(attribute)
    return result unless result == 0
  end
  0
end

#==(other) ⇒ Object Also known as: eql?



45
46
47
48
49
# File 'lib/transpec/record.rb', line 45

def ==(other)
  self.class == other.class &&
    old_syntax_type == other.old_syntax_type &&
    new_syntax_type == other.new_syntax_type
end

#hashObject



53
54
55
# File 'lib/transpec/record.rb', line 53

def hash
  old_syntax_type.hash ^ new_syntax_type.hash
end

#new_syntaxObject



41
42
43
# File 'lib/transpec/record.rb', line 41

def new_syntax
  new_syntax_type && new_syntax_type.to_s
end

#old_syntaxObject



37
38
39
# File 'lib/transpec/record.rb', line 37

def old_syntax
  old_syntax_type && old_syntax_type.to_s
end

#to_sObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/transpec/record.rb', line 57

def to_s
  string = type.to_s.capitalize

  string << case type
            when :conversion, :modification
              " from `#{old_syntax_type}` to `#{new_syntax_type}`"
            when :addition
              " of `#{new_syntax_type}`"
            when :removal
              " of `#{old_syntax_type}`"
            end
end

#typeObject



27
28
29
30
31
32
33
34
35
# File 'lib/transpec/record.rb', line 27

def type
  @type ||= if old_syntax_type && new_syntax_type
              :conversion
            elsif new_syntax_type
              :addition
            else
              :removal
            end
end