Sha256: eab7bee56f19bcc0762d987502ea08aced77a981dd80c8320cd87ee11194b980

Contents?: true

Size: 1.24 KB

Versions: 10

Compression:

Stored size: 1.24 KB

Contents

require 'vedeu/support/common'

module Vedeu

  # Provides means to convert attributes into the correct model.
  #
  module Coercions

    include Vedeu::Common

    # Produces new objects of the correct class from the value, ignores objects
    # that have already been coerced.
    #
    # @param value [Object|NilClass]
    # @return [Object]
    def coerce(value)
      if value.nil?
        new

      elsif value.is_a?(self)
        value

      else
        new(value)

      end
    end

    # Produces new objects of the correct class from attributes hashes,
    # ignores objects that have already been coerced.
    #
    # @param values [Array|Hash]
    # @return [Array]
    def coercer(values)
      return [] unless defined_value?(values)

      [values].flatten.map do |value|
        if value.is_a?(self)
          value

        else
          new(value)

        end
      end
    end

    # Whenever this module {Vedeu::Coercions} is included in another class or
    # module, make its methods into class methods, so they may be called
    # directly.
    #
    # @param receiver [Class] The class in which this module is included.
    # @return [void]
    def self.included(receiver)
      receiver.extend(self)
    end

  end # Coercions

end # Vedeu

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
vedeu-0.4.13 lib/vedeu/support/coercions.rb
vedeu-0.4.12 lib/vedeu/support/coercions.rb
vedeu-0.4.11 lib/vedeu/support/coercions.rb
vedeu-0.4.10 lib/vedeu/support/coercions.rb
vedeu-0.4.9 lib/vedeu/support/coercions.rb
vedeu-0.4.8 lib/vedeu/support/coercions.rb
vedeu-0.4.7 lib/vedeu/support/coercions.rb
vedeu-0.4.6 lib/vedeu/support/coercions.rb
vedeu-0.4.5 lib/vedeu/support/coercions.rb
vedeu-0.4.4 lib/vedeu/support/coercions.rb