Class: Ppl::Format::Custom

Inherits:
Object
  • Object
show all
Defined in:
lib/ppl/format/custom.rb

Direct Known Subclasses

Contact, EmailAddress, PhoneNumber

Defined Under Namespace

Classes: Contact, EmailAddress, PhoneNumber

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format = nil) ⇒ Custom

Returns a new instance of Custom.



19
20
21
# File 'lib/ppl/format/custom.rb', line 19

def initialize(format = nil)
  @format = format
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



3
4
5
# File 'lib/ppl/format/custom.rb', line 3

def format
  @format
end

#preset_formatsObject

Returns the value of attribute preset_formats.



4
5
6
# File 'lib/ppl/format/custom.rb', line 4

def preset_formats
  @preset_formats
end

Class Method Details

.format(symbol, &block) ⇒ Object



6
7
8
9
# File 'lib/ppl/format/custom.rb', line 6

def self.format(symbol, &block)
  @format_blocks ||= {}
  @format_blocks[symbol] = block
end

.process(key, object) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ppl/format/custom.rb', line 11

def self.process(key, object)
  if @format_blocks[key]
    @format_blocks[key][object]
  else
    key
  end
end

Instance Method Details

#process(object) ⇒ Object



23
24
25
26
27
# File 'lib/ppl/format/custom.rb', line 23

def process(object)
  @object = object
  string_pieces = @format.scan(/[^%]+|%-?\d*./)
  string_pieces.map(&method(:process_piece)).join
end

#use_preset(format_name) ⇒ Object



29
30
31
# File 'lib/ppl/format/custom.rb', line 29

def use_preset(format_name)
  @format = @preset_formats[format_name]
end