Class: Ace::Search::Models::SearchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/search/models/search_result.rb

Overview

SearchResult represents a single search result item This is a model - pure data structure with no behavior

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, path:, line_number: nil, content: nil, column: nil, match_start: nil, match_end: nil, submatches: nil, metadata: {}) ⇒ SearchResult

Returns a new instance of SearchResult.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ace/search/models/search_result.rb', line 12

def initialize(type:, path:, line_number: nil, content: nil, column: nil,
  match_start: nil, match_end: nil, submatches: nil, metadata: {})
  @type = type
  @path = path
  @line_number = line_number
  @content = content
  @column = column
  @match_start = match_start
  @match_end = match_end
  @submatches = submatches || []
  @metadata =  || {}
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def column
  @column
end

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def content
  @content
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def line_number
  @line_number
end

#match_endObject (readonly)

Returns the value of attribute match_end.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def match_end
  @match_end
end

#match_startObject (readonly)

Returns the value of attribute match_start.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def match_start
  @match_start
end

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def 
  @metadata
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def path
  @path
end

#submatchesObject (readonly)

Returns the value of attribute submatches.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def submatches
  @submatches
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/ace/search/models/search_result.rb', line 9

def type
  @type
end

Class Method Details

.directory(path, metadata = {}) ⇒ Object



87
88
89
# File 'lib/ace/search/models/search_result.rb', line 87

def self.directory(path,  = {})
  new(type: :directory, path: path, metadata: )
end

.file(path, metadata = {}) ⇒ Object



69
70
71
# File 'lib/ace/search/models/search_result.rb', line 69

def self.file(path,  = {})
  new(type: :file, path: path, metadata: )
end

.match(path, line_number, content, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ace/search/models/search_result.rb', line 73

def self.match(path, line_number, content, options = {})
  new(
    type: :match,
    path: path,
    line_number: line_number,
    content: content,
    column: options[:column],
    match_start: options[:match_start],
    match_end: options[:match_end],
    submatches: options[:submatches],
    metadata: options[:metadata] || {}
  )
end

Instance Method Details

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



91
92
93
94
95
96
97
98
99
# File 'lib/ace/search/models/search_result.rb', line 91

def ==(other)
  return false unless other.is_a?(SearchResult)

  @type == other.type &&
    @path == other.path &&
    @line_number == other.line_number &&
    @content == other.content &&
    @column == other.column
end

#basenameObject



37
38
39
# File 'lib/ace/search/models/search_result.rb', line 37

def basename
  File.basename(@path)
end

#directory?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ace/search/models/search_result.rb', line 33

def directory?
  @type == :directory
end

#dirnameObject



41
42
43
# File 'lib/ace/search/models/search_result.rb', line 41

def dirname
  File.dirname(@path)
end

#extensionObject



45
46
47
48
# File 'lib/ace/search/models/search_result.rb', line 45

def extension
  ext = File.extname(@path)
  ext.empty? ? "" : ext[1..]
end

#file?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ace/search/models/search_result.rb', line 25

def file?
  @type == :file
end

#hashObject



101
102
103
# File 'lib/ace/search/models/search_result.rb', line 101

def hash
  [@type, @path, @line_number, @content, @column].hash
end

#match?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ace/search/models/search_result.rb', line 29

def match?
  @type == :match
end

#to_hObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ace/search/models/search_result.rb', line 50

def to_h
  {
    type: @type,
    path: @path,
    line_number: @line_number,
    content: @content,
    column: @column,
    match_start: @match_start,
    match_end: @match_end,
    submatches: @submatches,
    metadata: @metadata
  }
end

#to_json(*args) ⇒ Object



64
65
66
67
# File 'lib/ace/search/models/search_result.rb', line 64

def to_json(*args)
  require "json"
  to_h.to_json(*args)
end