Sha256: b99dbdb6c6956278a8955ceb26ffdc277278b6313bff8c11424d8e0f12605b95

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

class RestModel
  module Source
    module Sender
      def to_source!(options = {})
        to_source({fail: true}.merge(options))
      end

      def to_source(options = {})
        source = {}
        errors = {}
        root_options = {without_nil: options[:without_nil], fail: options[:fail]}

        keys_to_source(options).each do |key|
          value = __send__(key.name)
          key_options = options.fetch(key.name, {}).merge(root_options)

          begin
            source.merge! key.to_source!(value, self, key_options)
          rescue TranslationError, SerializationError => e
            errors[key.name] = e.message
          rescue SourceError => e
            errors.merge!(e.message)
          end
        end

        fail SourceError, errors unless errors.empty? if options[:fail]

        source.with_indifferent_access
      end

      alias :to_input! :to_source!
      alias :to_input  :to_source

      private

      def keys_to_source(options)
        self.class.keys.clone.tap do |keys|
          keys.reject! {|k| k.kind_of?(Relation)}
          keys.reject! {|k| Array(options[:without]).include?(k.name)}

          keys.reject! do |key|
            __send__(key.name).nil? and options[:without_nil]
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rest_model-0.3.1 lib/rest_model/source/sender.rb
rest_model-0.3.0 lib/rest_model/source/sender.rb
rest_model-0.2.3 lib/rest_model/source/sender.rb
rest_model-0.2.1 lib/rest_model/source/sender.rb