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

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

Instance Method Summary collapse

Instance Method Details

#file_marshaller_representerObject


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/trailblazer/1.1/operation/worker.rb', line 81

def file_marshaller_representer
  @file_marshaller_representer ||= contract_class.schema(include: [Representable::Hash]).apply do |dfn|
    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] or dfn[:twin] # 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


100
101
102
# File 'lib/trailblazer/1.1/operation/worker.rb', line 100

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