Sha256: 9aac1c61ecd916a80800978b39e96591c16d902d49a8954caa0fa291e3eafa70

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module Vedeu

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

    # Contains class methods which are accessible as such to classes and modules
    # which include {Vedeu::Coercions}.
    module ClassMethods

      include Vedeu::Common

      # Produces new objects of the correct class from attributes hashes,
      # ignores objects that have already been coerced.
      # When provided with a parent argument, will allow the new object
      # to know the colour and style of its parent.
      #
      # @param values [Array|Hash]
      # @param parent [Hash|Nil]
      # @return [Array]
      def coercer(values, parent = nil)
        return [] unless defined_value?(values)

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

          else
            self.new(value.merge!({ parent: parent }))

          end
        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.
    def self.included(receiver)
      receiver.extend(ClassMethods)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.2.0 lib/vedeu/models/attributes/coercions.rb
vedeu-0.1.19 lib/vedeu/models/attributes/coercions.rb