Class: Libis::Tools::Metadata::FieldFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/libis/tools/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

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • parts (Array, Hash)

    whatever makes up the data to be formatted.



35
36
37
38
# File 'lib/libis/tools/metadata/field_format.rb', line 35

def initialize(*parts)
  @parts = []
  self[*parts]
end

Instance Attribute Details

#joinObject

String

the text used between the parts of the data



23
24
25
# File 'lib/libis/tools/metadata/field_format.rb', line 23

def join
  @join
end

#partsObject

Array

the list that makes up the data



14
15
16
# File 'lib/libis/tools/metadata/field_format.rb', line 14

def parts
  @parts
end

#postfixObject

String

the text that will be placed at the end of the generated text



20
21
22
# File 'lib/libis/tools/metadata/field_format.rb', line 20

def postfix
  @postfix
end

#prefixObject

String

the text that will be placed in front of the generated text



17
18
19
# File 'lib/libis/tools/metadata/field_format.rb', line 17

def prefix
  @prefix
end

Class Method Details

.from(*h) ⇒ Object

Shortcut class method for initializer



84
85
86
# File 'lib/libis/tools/metadata/field_format.rb', line 84

def self.from(*h)
  self.new(*h)
end

Instance Method Details

#[](*parts) ⇒ Object

Parses the arguments, stripping of an optional last Hash as options.

Parameters:

  • parts (Array, Hash)

    whatever makes up the data to be formatted.



42
43
44
45
46
47
48
# File 'lib/libis/tools/metadata/field_format.rb', line 42

def [](*parts)
  options = parts.last.is_a?(Hash) ? parts.pop : {}
  add parts
  x = options.delete(:parts)
  add x if x
  add_options options
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.

Parameters:

  • options (Hash) (defaults to: {})

    the options list



75
76
77
78
79
80
81
# File 'lib/libis/tools/metadata/field_format.rb', line 75

def add_default_options(options = {})
  options.delete(:prefix) if @prefix
  options.delete(:postfix) if @postfix
  options.delete(:fix) if @prefix or @postfix
  options.delete(:join) if @join
  add_options options
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.

Parameters:

  • options (Hash) (defaults to: {})

    the options list



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/libis/tools/metadata/field_format.rb', line 57

def add_options(options = {})
  if options[:fix]
    if options[:fix].size == 2
      @prefix, @postfix = options[:fix].split('')
    else
      @prefix, @postfix = options[:fix].split('|')
    end
  end
  @join = options[:join] if options[:join]
  @prefix = FieldFormat::from(options[:prefix]) if options[:prefix]
  @postfix = FieldFormat::from(options[:postfix]) if options[:postfix]
  self
end

#to_sString

The real formatter method. This method parses the data and applies the options to generate the formatted string.

Returns:

  • (String)

    the formatter string



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/libis/tools/metadata/field_format.rb', line 91

def to_s
  @parts.delete_if { |x|
    x.nil? or
        (x.is_a? String and x.empty?) or
        (x.is_a? Libis::Tools::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