Class: MetaCommit::Extension::RubySupport::Diffs::Diff

Inherits:
Contracts::Diff
  • Object
show all
Defined in:
lib/meta_commit_ruby_support/diffs/diff.rb

Overview

Base class for diffs

Constant Summary collapse

TYPE_ADDITION =
:addition
TYPE_DELETION =
:deletion
TYPE_REPLACE =
:replace
SUPPORTED_PARSERS =
[MetaCommit::Extension::RubySupport::Parsers::Ruby]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commit_newString

Returns the current value of commit_new.

Returns:

  • (String)

    the current value of commit_new



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def commit_new
  @commit_new
end

#commit_oldString

Returns the current value of commit_old.

Returns:

  • (String)

    the current value of commit_old



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def commit_old
  @commit_old
end

#diff_typeSymbol

Returns the current value of diff_type.

Returns:

  • (Symbol)

    the current value of diff_type



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def diff_type
  @diff_type
end

#new_ast_pathMetaCommit::Models::ContextualAstNode

Returns the current value of new_ast_path.

Returns:

  • (MetaCommit::Models::ContextualAstNode)

    the current value of new_ast_path



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def new_ast_path
  @new_ast_path
end

#new_fileString

Returns the current value of new_file.

Returns:

  • (String)

    the current value of new_file



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def new_file
  @new_file
end

#new_linenoString

Returns the current value of new_lineno.

Returns:

  • (String)

    the current value of new_lineno



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def new_lineno
  @new_lineno
end

#old_ast_pathMetaCommit::Models::ContextualAstNode

Returns the current value of old_ast_path.

Returns:

  • (MetaCommit::Models::ContextualAstNode)

    the current value of old_ast_path



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def old_ast_path
  @old_ast_path
end

#old_fileString

Returns the current value of old_file.

Returns:

  • (String)

    the current value of old_file



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def old_file
  @old_file
end

#old_linenoString

Returns the current value of old_lineno.

Returns:

  • (String)

    the current value of old_lineno



12
13
14
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 12

def old_lineno
  @old_lineno
end

Instance Method Details

#inspectString

Returns:

  • (String)


42
43
44
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 42

def inspect
  string_representation
end

#is_in_context_of_class?(ast) ⇒ Boolean

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)

Returns:

  • (Boolean)


128
129
130
131
132
133
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 128

def is_in_context_of_class?(ast)
  ast.context_nodes.each do |parent|
    return true if parent.is_class?
  end
  false
end

#is_in_context_of_method?(ast) ⇒ Boolean

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)

Returns:

  • (Boolean)


137
138
139
140
141
142
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 137

def is_in_context_of_method?(ast)
  ast.context_nodes.each do |parent|
    return true if parent.is_method?
  end
  false
end

#is_in_context_of_module?(ast) ⇒ Boolean

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 119

def is_in_context_of_module?(ast)
  ast.context_nodes.each do |parent|
    return true if parent.is_module?
  end
  false
end

#is_name_of_class?(ast) ⇒ Boolean

on created class only first line goes to diff

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)

Returns:

  • (Boolean)


86
87
88
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 86

def is_name_of_class?(ast)
  (ast.target_node.ast.type == :const) and (ast.context_nodes.length > 1) and (ast.context_nodes[ast.context_nodes.length - 1 - 1].ast.type == :class)
end

#is_name_of_module?(ast) ⇒ Boolean

on created module only first line goes to diff

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)

Returns:

  • (Boolean)


92
93
94
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 92

def is_name_of_module?(ast)
  (ast.target_node.ast.type == :const) and (ast.context_nodes.length > 1) and (ast.context_nodes[ast.context_nodes.length - 1 - 1].ast.type == :module)
end

#name_of_context_class(ast) ⇒ Object

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)


104
105
106
107
108
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 104

def name_of_context_class(ast)
  ast.context_nodes.reverse.each do |parent|
    return parent.class_name if parent.is_class?
  end
end

#name_of_context_method(ast) ⇒ Object

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)


111
112
113
114
115
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 111

def name_of_context_method(ast)
  ast.context_nodes.reverse.each do |parent|
    return parent.method_name if parent.is_method?
  end
end

#name_of_context_module(ast) ⇒ Object

Parameters:

  • ast (MetaCommit::Model::ContextualAstNode)


97
98
99
100
101
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 97

def name_of_context_module(ast)
  ast.context_nodes.reverse.each do |parent|
    return parent.module_name if parent.is_module?
  end
end

#path_to_component(ast, depth = nil) ⇒ String

Parameters:

  • ast (Object)
  • depth (Integer) (defaults to: nil)

Returns:

  • (String)


75
76
77
78
79
80
81
82
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 75

def path_to_component(ast, depth=nil)
  depth = -1 if depth.nil?
  result = []
  result.concat([name_of_context_module(ast), is_in_context_of_class?(ast) && depth < 1 ? '::' : '']) if is_in_context_of_module?(ast) && depth < 2
  result.concat([name_of_context_class(ast)]) if is_in_context_of_class?(ast) && depth < 1
  result.concat(['#', name_of_context_method(ast)]) if is_in_context_of_method?(ast) && depth < 0
  result.join('')
end

#string_representationString

Returns:

  • (String)


52
53
54
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 52

def string_representation
  "#{diff_type} was performed"
end

#supports_change(type, old_file_name, new_file_name, old_ast_path, new_ast_path) ⇒ Boolean

Parameters:

  • type (Symbol)
  • old_file_name (String)
  • new_file_name (String)
  • old_ast_path (String)
  • new_ast_path (String)

Returns:

  • (Boolean)


37
38
39
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 37

def supports_change(type, old_file_name, new_file_name, old_ast_path, new_ast_path)
  true
end

#supports_parser?(parser) ⇒ Boolean

Parameters:

  • parser (Class)

Returns:

  • (Boolean)


27
28
29
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 27

def supports_parser?(parser)
  SUPPORTED_PARSERS.include?(parser)
end

#to_sString

Returns:

  • (String)


47
48
49
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 47

def to_s
  string_representation
end

#type_addition?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 57

def type_addition?
  @diff_type == TYPE_ADDITION
end

#type_deletion?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 62

def type_deletion?
  @diff_type == TYPE_DELETION
end

#type_replace?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/meta_commit_ruby_support/diffs/diff.rb', line 67

def type_replace?
  @diff_type == TYPE_REPLACE
end