Class: Realize::Format::Substring

Inherits:
Object
  • Object
show all
Defined in:
lib/realize/format/substring.rb

Overview

Cut a string using a range (start and end index). You can also choose whether you wish the range selection to be inclusive (default) or exclusive.

Examples using the string: ‘012-345-6789’

Exclusive vs. Inclusive

start_index: 4, end_index: 6, exclusive: false

> ‘345’

start_index: 4, end_index: 6, exclusive: true

> ‘34’

Default Values / Omitted Options

end_index: 6

> ‘012-345’

start_index: 4

> ‘345-6789’

Constant Summary collapse

DEFAULT_END_INDEX =
-1
DEFAULT_START_INDEX =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_index: DEFAULT_END_INDEX, exclusive: false, start_index: DEFAULT_START_INDEX) ⇒ Substring

Returns a new instance of Substring.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/realize/format/substring.rb', line 34

def initialize(
  end_index: DEFAULT_END_INDEX,
  exclusive: false,
  start_index: DEFAULT_START_INDEX
)
  @end_index   = end_index.to_i
  @exclusive   = exclusive || false
  @start_index = start_index.to_i

  freeze
end

Instance Attribute Details

#end_indexObject (readonly)

Returns the value of attribute end_index.



32
33
34
# File 'lib/realize/format/substring.rb', line 32

def end_index
  @end_index
end

#exclusiveObject (readonly)

Returns the value of attribute exclusive.



32
33
34
# File 'lib/realize/format/substring.rb', line 32

def exclusive
  @exclusive
end

#start_indexObject (readonly)

Returns the value of attribute start_index.



32
33
34
# File 'lib/realize/format/substring.rb', line 32

def start_index
  @start_index
end

Instance Method Details

#transform(_resolver, value, _time, _record) ⇒ Object



46
47
48
# File 'lib/realize/format/substring.rb', line 46

def transform(_resolver, value, _time, _record)
  exclusive ? value.to_s[start_index...end_index] : value.to_s[start_index..end_index]
end