Class: Libis::Metadata::FieldFormat
- Inherits:
-
Object
- Object
- Libis::Metadata::FieldFormat
- Defined in:
- lib/libis/metadata/field_format.rb
Overview
Helper class for formatting field data.
The FieldFormat class can omit prefix and or postfix if no data is present and omits the join string if only one data element is present.
Instance Attribute Summary collapse
-
#join ⇒ Object
- String
-
the text used between the parts of the data.
-
#parts ⇒ Object
- Array
-
the list that makes up the data.
-
#postfix ⇒ Object
- String
-
the text that will be placed at the end of the generated text.
-
#prefix ⇒ Object
- String
-
the text that will be placed in front of the generated text.
Class Method Summary collapse
-
.from(*h) ⇒ Object
Shortcut class method for initializer.
Instance Method Summary collapse
-
#[](*parts) ⇒ Object
Parses the arguments, stripping of an optional last Hash as options.
-
#add_default_options(options = {}) ⇒ Object
Add default options.
-
#add_options(options = {}) ⇒ Object
Set options.
-
#initialize(*parts) ⇒ FieldFormat
constructor
Create new formatter.
-
#to_s ⇒ String
The real formatter method.
Constructor Details
#initialize(*parts) ⇒ FieldFormat
Create new formatter
The method takes any number of arguments and processes them as data parts. If the last one is a Hash, it is interpreted as options hash. The data parts can either be given as an Array or set of arguments or within the options hash with key :parts
.
On each element in the data set the formatter will call the #to_s method to give each data object the opportunity to process it’s data.
34 35 36 37 |
# File 'lib/libis/metadata/field_format.rb', line 34 def initialize(*parts) @parts = [] self[*parts] end |
Instance Attribute Details
#join ⇒ Object
- String
-
the text used between the parts of the data
22 23 24 |
# File 'lib/libis/metadata/field_format.rb', line 22 def join @join end |
#parts ⇒ Object
- Array
-
the list that makes up the data
13 14 15 |
# File 'lib/libis/metadata/field_format.rb', line 13 def parts @parts end |
#postfix ⇒ Object
- String
-
the text that will be placed at the end of the generated text
19 20 21 |
# File 'lib/libis/metadata/field_format.rb', line 19 def postfix @postfix end |
#prefix ⇒ Object
- String
-
the text that will be placed in front of the generated text
16 17 18 |
# File 'lib/libis/metadata/field_format.rb', line 16 def prefix @prefix end |
Class Method Details
.from(*h) ⇒ Object
Shortcut class method for initializer
83 84 85 |
# File 'lib/libis/metadata/field_format.rb', line 83 def self.from(*h) self.new(*h) end |
Instance Method Details
#[](*parts) ⇒ Object
Parses the arguments, stripping of an optional last Hash as options.
41 42 43 44 45 46 47 |
# File 'lib/libis/metadata/field_format.rb', line 41 def [](*parts) = parts.last.is_a?(Hash) ? parts.pop : {} add parts x = .delete(:parts) add x if x end |
#add_default_options(options = {}) ⇒ Object
Add default options. (see #add_options) None of these options will be set if they are already set. If you need to overwrite them, use #add_options.
74 75 76 77 78 79 80 |
# File 'lib/libis/metadata/field_format.rb', line 74 def ( = {}) .delete(:prefix) if @prefix .delete(:postfix) if @postfix .delete(:fix) if @prefix or @postfix .delete(:join) if @join end |
#add_options(options = {}) ⇒ Object
Set options.
Besides the tree options :prefix
, :postfix
and :join
it also accepts the option :fix
. This combines both :prefix
and :postfix
options by specifying “<prefix>|<postfix>”. If both prefix and postfix are only 1 character wide the format “<prefix><postfix>” is also allowed.
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/libis/metadata/field_format.rb', line 56 def ( = {}) if [:fix] if [:fix].size == 2 @prefix, @postfix = [:fix].split('') else @prefix, @postfix = [:fix].split('|') end end @join = [:join] if [:join] @prefix = FieldFormat::from([:prefix]) if [:prefix] @postfix = FieldFormat::from([:postfix]) if [:postfix] self end |
#to_s ⇒ String
The real formatter method. This method parses the data and applies the options to generate the formatted string.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/libis/metadata/field_format.rb', line 90 def to_s @parts.delete_if { |x| x.nil? or (x.is_a? String and x.empty?) or (x.is_a? Libis::Metadata::FieldFormat and x.to_s.empty?) } result = @parts.join(@join) unless result.empty? result = (@prefix || '').to_s + result + (@postfix || '').to_s end result end |