Class: Ppl::Format::Custom
- Inherits:
-
Object
- Object
- Ppl::Format::Custom
- Defined in:
- lib/ppl/format/custom.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Contact, EmailAddress, PhoneNumber
Instance Attribute Summary collapse
-
#format ⇒ Object
Returns the value of attribute format.
-
#preset_formats ⇒ Object
Returns the value of attribute preset_formats.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(format = nil) ⇒ Custom
constructor
A new instance of Custom.
- #process(object) ⇒ Object
- #use_preset(format_name) ⇒ Object
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
#format ⇒ Object
Returns the value of attribute format.
3 4 5 |
# File 'lib/ppl/format/custom.rb', line 3 def format @format end |
#preset_formats ⇒ Object
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 |