Sha256: 77d128920dc5545a25d2c806e6084c57e684729450e346826ee33df8b8482ab9

Contents?: true

Size: 951 Bytes

Versions: 9

Compression:

Stored size: 951 Bytes

Contents

module Mabmapper
  module MabXml
    class Field
      attr_reader :name, :subfields

      def initialize(name)
        @name      = name
        @subfields = []
      end

      def add_subfield(subfield)
        raise if subfield.blank? or not subfield.is_a?(Subfield)

        @subfields << subfield
      end

      def values(options = {})
        options.reverse_merge!(join_subfields: nil)
        values = @subfields.map(&:value)

        join = options[:join_subfields]
        join ? values.join((!!join==join) ? nil : join.to_s) : values
      end

      def value(options = {})
        options.reverse_merge!(join_subfields: ' ')
        values(options)
      end

      def has_subfield?(subfield)
        subfields.any?{|s| s.name.to_s == subfield.to_s}
      end

      def get_subfield(subfield)
        subfields.select{|s| s.name.to_s == subfield.to_s}.try(:first)
      end

      def to_s
        value
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mabmapper-2.0.4 lib/mabmapper/mab_xml/field.rb
mabmapper-2.0.3 lib/mabmapper/mab_xml/field.rb
mabmapper-2.0.2 lib/mabmapper/mab_xml/field.rb
mabmapper-2.0.1 lib/mabmapper/mab_xml/field.rb
mabmapper-2.0.0 lib/mabmapper/mab_xml/field.rb
mabmapper-1.0.0.pre18 lib/mabmapper/mab_xml/field.rb
mabmapper-1.0.0.pre17 lib/mabmapper/mab_xml/field.rb
mabmapper-1.0.0.pre16 lib/mabmapper/mab_xml/field.rb
mabmapper-1.0.0.pre15 lib/mabmapper/mab_xml/field.rb