Module: Trailblazer::Operation::Worker::FileMarshaller::ClassMethods

Defined in:
lib/trailblazer/operation/worker.rb

Instance Method Summary collapse

Instance Method Details

#file_marshaller_representerObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/trailblazer/operation/worker.rb', line 63

def file_marshaller_representer
  @file_marshaller_representer ||= contract_class.schema.apply do |dfn|
    dfn.delete!(:prepare)

    dfn.merge!(
      :getter => lambda { |*| self[dfn.name.to_sym] },
      :setter => lambda { |fragment, *| self[dfn.name.to_s] = fragment }
    ) # FIXME: allow both sym and str.

    dfn.merge!(:class => Hash) and next if dfn[:form] # nested properties need a class for deserialization.
    next unless dfn[:file]

    # TODO: where do we set /tmp/uploads?
    dfn.merge!(
      :serialize   => lambda { |file, *| Trailblazer::Operation::UploadedFile.new(file, :tmp_dir => "/tmp/uploads").to_hash },
      :deserialize => lambda { |object, hash, *| Trailblazer::Operation::UploadedFile.from_hash(hash) },
      :class       => Hash
    )
  end
end

#serializable(params) ⇒ Object



84
85
86
# File 'lib/trailblazer/operation/worker.rb', line 84

def serializable(params)
  file_marshaller_representer.new(params).to_hash
end