Sha256: 9bd0e4385d4f1e592eee143520c89ab6a18554f00680716d2871cc07da83a002

Contents?: true

Size: 1013 Bytes

Versions: 5

Compression:

Stored size: 1013 Bytes

Contents

class RestModel
  class Property
    module Sender
      def to_source!(value, resource, options = {})
        source_value = nil

        begin
          if translations
            source_value = translate_to_source(value, resource)
          else
            source_value = serializer.desserialize(value)
          end
        rescue => exception
          raise exception if options[:fail]
        end

        source = {}
        path = source_path

        if path.any?
          last = path.pop
          key_source = path.inject(source) {|buffer, key| buffer[key] = {}; buffer[key]}
          key_source[last] = source_value
        else
          source.merge!(source_value)
        end

        source
      end

      def translate_to_source(value, resource)
        case translations
        when Proc then resource.instance_eval(&translations)
        when Hash
          fail "to input error" unless translations.has_key?(value)
          translations[value]
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rest_model-0.1.4 lib/rest_model/key/property/sender.rb
rest_model-0.1.3 lib/rest_model/key/property/sender.rb
rest_model-0.1.2 lib/rest_model/key/property/sender.rb
rest_model-0.1.1 lib/rest_model/key/property/sender.rb
rest_model-0.1.0 lib/rest_model/key/property/sender.rb