Class: Cecil::Placeholder

Inherits:
Struct
  • Object
show all
Defined in:
lib/cecil/placeholder.rb

Overview

Represents the name and location of a placeholder in a string.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#identString

Returns the name of this placeholder. E.g. the ident of ${my_field} would be my_field.

Returns:

  • (String)

    the name of this placeholder. E.g. the ident of ${my_field} would be my_field



3
4
5
# File 'lib/cecil/placeholder.rb', line 3

def ident
  @ident
end

#offset_endInteger

Returns the offset where this placeholder ends in the string. This number is usually taken from a Regexp match.

Returns:

  • (Integer)

    the offset where this placeholder ends in the string. This number is usually taken from a Regexp match.



3
4
5
# File 'lib/cecil/placeholder.rb', line 3

def offset_end
  @offset_end
end

#offset_startInteger

Returns the offset where this placeholder starts in the string. This number is usually taken from a Regexp match.

Returns:

  • (Integer)

    the offset where this placeholder starts in the string. This number is usually taken from a Regexp match.



3
4
5
# File 'lib/cecil/placeholder.rb', line 3

def offset_start
  @offset_start
end

Instance Method Details

#rangeRange(Integer)

Return the range that this placeholder occupies in the string

Returns:

  • (Range(Integer))


17
# File 'lib/cecil/placeholder.rb', line 17

def range = offset_start...offset_end

#transform_key(member) ⇒ Placeholder

Create a new Cecil::Placeholder with one member transformed by the given block

Examples:

Make a new placeholder with ident in uppercase

placeholder.transform_key(:ident, &:upcase)

Parameters:

  • member (Symbol)

Returns:



29
# File 'lib/cecil/placeholder.rb', line 29

def transform_key(member) = with(**{ member => yield(self[member]) })

#with(**kwargs) ⇒ Object

Mimicks Data#with, introduced in Ruby 3.2



20
# File 'lib/cecil/placeholder.rb', line 20

def with(**kwargs) = self.class.new(*to_h.merge(kwargs).values_at(*members))