Class: Libis::Metadata::FieldFormat

Inherits:
Object
  • Object
show all
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

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.



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

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

Instance Attribute Details

#joinObject

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

#partsObject

Array

the list that makes up the data



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

def parts
  @parts
end

#postfixObject

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

#prefixObject

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.

Parameters:

  • parts (Array, Hash)

    whatever makes up the data to be formatted.



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

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



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

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



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

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



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