Class: Germinate::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/germinate/selector.rb

Constant Summary collapse

PATTERN =
/^([@$])?(\w+)?(:([^\|]+))?(\|([\w|]+))?$/
EXCERPT_OUTPUT_PATTERN =
/^([@$])?(\w+)?(\|([\w|]+))?(:([^\|]+))?$/
EXCERPT_PATTERN =
%r{((-?\d+)|(/[^/]*/))(((\.\.\.?)|(,))((-?\d+)|(/[^/]*/)))?}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, default_key = :unspecified, origin = "<Unknown>") ⇒ Selector

Returns a new instance of Selector.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/germinate/selector.rb', line 17

def initialize(string, default_key=:unspecified, origin="<Unknown>")
  @string      = string
  @default_key = default_key
  @origin      = origin
  match_data = case string
               when "", nil then {}
               else 
                 if data = PATTERN.match(string) 
                   @excerpt_output = false
                 elsif data = EXCERPT_OUTPUT_PATTERN.match(string)
                   @excerpt_output = true
                 else
                   raise "Could not parse selector '#{string}'"
                 end
                 data
               end

  subscript_index = @excerpt_output ? 6 : 3
  pipeline_index  = @excerpt_output ? 4 : 6

  @selector_type = 
    case match_data[1]
    when "$" then :special
    when "@" then :code
    when nil then default_key == :unspecified ? :special : :code
    else raise "Unknown selector type '#{match_data[1]}'"
    end
  @key = match_data[2] || (default_key == :unspecified ? "SOURCE" : default_key)
  if match_data[subscript_index]
    @slice = true
    parse_excerpt(match_data[subscript_index])
  else
    @slice        = false
    @delimiter    = '..'
    @start_offset = 1
    @end_offset   = -1
    @length       = nil
  end
  @pipeline = String(match_data[pipeline_index]).
    split("|").unshift('_transform').uniq
end

Instance Attribute Details

#default_keyObject (readonly)

Returns the value of attribute default_key.



10
11
12
# File 'lib/germinate/selector.rb', line 10

def default_key
  @default_key
end

#delimiterObject (readonly)

Returns the value of attribute delimiter.



8
9
10
# File 'lib/germinate/selector.rb', line 8

def delimiter
  @delimiter
end

#end_offsetObject (readonly)

Returns the value of attribute end_offset.



6
7
8
# File 'lib/germinate/selector.rb', line 6

def end_offset
  @end_offset
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/germinate/selector.rb', line 4

def key
  @key
end

#lengthObject (readonly)

Returns the value of attribute length.



7
8
9
# File 'lib/germinate/selector.rb', line 7

def length
  @length
end

#originObject (readonly)

Returns the value of attribute origin.



11
12
13
# File 'lib/germinate/selector.rb', line 11

def origin
  @origin
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



9
10
11
# File 'lib/germinate/selector.rb', line 9

def pipeline
  @pipeline
end

#selector_typeObject (readonly)

Returns the value of attribute selector_type.



3
4
5
# File 'lib/germinate/selector.rb', line 3

def selector_type
  @selector_type
end

#start_offsetObject (readonly)

Returns the value of attribute start_offset.



5
6
7
# File 'lib/germinate/selector.rb', line 5

def start_offset
  @start_offset
end

#stringObject (readonly)

Returns the value of attribute string.



2
3
4
# File 'lib/germinate/selector.rb', line 2

def string
  @string
end

Instance Method Details

#==(other) ⇒ Object



86
87
88
# File 'lib/germinate/selector.rb', line 86

def ==(other)
  string == other
end

#end_offset_for_sliceObject



63
64
65
# File 'lib/germinate/selector.rb', line 63

def end_offset_for_slice
  offset_for_slice(end_offset)
end

#excerpt_output?Boolean

Should excerpting be done on the output of the process?

Returns:

  • (Boolean)


68
69
70
# File 'lib/germinate/selector.rb', line 68

def excerpt_output?
  @excerpt_output
end

#slice?Boolean

Is it just a subset of the source hunk? (opposite of @whole?)

Returns:

  • (Boolean)


73
74
75
# File 'lib/germinate/selector.rb', line 73

def slice?
  @slice && !@excerpt_output
end

#start_offset_for_sliceObject



59
60
61
# File 'lib/germinate/selector.rb', line 59

def start_offset_for_slice
  offset_for_slice(start_offset)
end

#to_sObject



82
83
84
# File 'lib/germinate/selector.rb', line 82

def to_s
  string + "(#{origin})"
end

#whole?Boolean

Is it the entire hunk? (opposite of #slice?)

Returns:

  • (Boolean)


78
79
80
# File 'lib/germinate/selector.rb', line 78

def whole?
  !slice?
end