Class: Ast::Crispr::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/crispr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_line:, end_line:, target: nil, node: nil, metadata: {}, **options) ⇒ Match

Returns a new instance of Match.

Raises:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ast/crispr.rb', line 126

def initialize(start_line:, end_line:, target: nil, node: nil, metadata: {}, **options)
  @target = target
  @node = node
  @start_line = Integer(start_line)
  @end_line = Integer(end_line)
  @metadata = .merge(options)
  return unless @end_line < @start_line

  raise Error.new('CRISPR match end_line must be >= start_line',
                  details: { start_line: @start_line, end_line: @end_line })
end

Instance Attribute Details

#end_lineObject (readonly)

Returns the value of attribute end_line.



124
125
126
# File 'lib/ast/crispr.rb', line 124

def end_line
  @end_line
end

#metadataObject (readonly)

Returns the value of attribute metadata.



124
125
126
# File 'lib/ast/crispr.rb', line 124

def 
  @metadata
end

#nodeObject (readonly)

Returns the value of attribute node.



124
125
126
# File 'lib/ast/crispr.rb', line 124

def node
  @node
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



124
125
126
# File 'lib/ast/crispr.rb', line 124

def start_line
  @start_line
end

#targetObject (readonly)

Returns the value of attribute target.



124
125
126
# File 'lib/ast/crispr.rb', line 124

def target
  @target
end

Instance Method Details

#line_rangeObject



150
151
152
# File 'lib/ast/crispr.rb', line 150

def line_range
  start_line..end_line
end

#match_profileObject



161
162
163
164
165
166
167
168
# File 'lib/ast/crispr.rb', line 161

def match_profile
  MatchProfile.new(
    start_boundary: .fetch(:start_boundary, :owner_start),
    end_boundary: .fetch(:end_boundary, :owner_end),
    payload_kind: .fetch(:payload_kind, :structural_owner_body),
    metadata: 
  )
end

#slice_from(content) ⇒ Object



154
155
156
157
158
159
# File 'lib/ast/crispr.rb', line 154

def slice_from(content)
  lines = content.to_s.lines
  return '' if lines.empty?

  lines[(start_line - 1)..(end_line - 1)].to_a.join
end

#with_target(target) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ast/crispr.rb', line 138

def with_target(target)
  return self if self.target.equal?(target)

  self.class.new(
    target: target,
    node: node,
    start_line: start_line,
    end_line: end_line,
    metadata: 
  )
end