Sha256: c2261cdf1f0050e39e4abe838c830e671298f1b0d4bbb1ffebfd9924c6596c1d

Contents?: true

Size: 1.46 KB

Versions: 24

Compression:

Stored size: 1.46 KB

Contents

module Ddr::Models
  module Metadata
    extend ActiveSupport::Concern

    ADMIN_METADATA = 'admin_metadata'.freeze
    DESC_METADATA  = 'desc_metadata'.freeze

    included do
      def self.property_term(term)
        property_terms[term.to_sym]
      end
    end

    def values(term)
      self.send(term)
    end

    # Update singular term with value
    def set_value(term, value)
      begin
        self.send("#{term}=", value)
      rescue NoMethodError
        raise ArgumentError, "No such term: #{term}"
      end
    end

    # Update term with values
    # Note that empty values (nil or "") are rejected from values array
    def set_values(term, values)
      if values.respond_to?(:reject!)
        values.reject! { |v| v.blank? }
      else
        values = nil if values.blank?
      end
      begin
        self.send("#{term}=", values)
      rescue NoMethodError
        raise ArgumentError, "No such term: #{term}"
      end
    end

    # Add value to term
    # Note that empty value (nil or "") is not added
    def add_value(term, value)
      begin
        unless value.blank?
          prop_term = self.class.property_term(term)
          if object.class.properties[prop_term.to_s].multiple?
            values = values(term).to_a << value
            set_values term, values
          else
            set_value term, value
          end
        end
      rescue NoMethodError
        raise ArgumentError, "No such term: #{term}"
      end
    end

  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ddr-models-3.0.8 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.7 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.6 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.5 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.4 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.3 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.2 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.1 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.rc6 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.rc5 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.rc4 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.rc3 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.rc2 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.rc1 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.beta.22 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.beta.21 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.beta.20 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.beta.18 lib/ddr/models/metadata/metadata.rb
ddr-models-3.0.0.beta.17 lib/ddr/models/metadata/metadata.rb