Public Instance methods
derivatives(object)
Converts data into a Hash of derivatives.
Shrine.derivatives('{"thumb":{"id":"foo","storage":"store","metadata":{}}}') #=> { thumb: #<Shrine::UploadedFile id="foo" storage=:store metadata={}> } Shrine.derivatives({ "thumb" => { "id" => "foo", "storage" => "store", "metadata" => {} } }) #=> { thumb: #<Shrine::UploadedFile id="foo" storage=:store metadata={}> } Shrine.derivatives({ thumb: { id: "foo", storage: "store", metadata: {} } }) #=> { thumb: #<Shrine::UploadedFile id="foo" storage=:store metadata={}> }
[show source]
# File lib/shrine/plugins/derivatives.rb 572 def derivatives(object) 573 if object.is_a?(String) 574 derivatives JSON.parse(object) 575 elsif object.is_a?(Hash) || object.is_a?(Array) 576 map_derivative( 577 object, 578 transform_keys: :to_sym, 579 leaf: -> (value) { value.is_a?(Hash) && (value["id"] || value[:id]).is_a?(String) }, 580 ) { |_, value| uploaded_file(value) } 581 else 582 fail ArgumentError, "cannot convert #{object.inspect} to derivatives" 583 end 584 end
derivatives_options()
Returns derivatives plugin options.
[show source]
# File lib/shrine/plugins/derivatives.rb 617 def derivatives_options 618 opts[:derivatives] 619 end
map_derivative(object, path = [], transform_keys: :to_sym, leaf: nil, &block)
Iterates over a nested collection, yielding on each part of the path. If the block returns a truthy value, that branch is terminated
[show source]
# File lib/shrine/plugins/derivatives.rb 588 def map_derivative(object, path = [], transform_keys: :to_sym, leaf: nil, &block) 589 return enum_for(__method__, object) unless block_given? 590 591 if leaf && leaf.call(object) 592 yield path, object 593 elsif object.is_a?(Hash) 594 object.inject({}) do |hash, (key, value)| 595 key = key.send(transform_keys) 596 597 hash.merge! key => map_derivative( 598 value, [*path, key], 599 transform_keys: transform_keys, leaf: leaf, 600 &block 601 ) 602 end 603 elsif object.is_a?(Array) 604 object.map.with_index do |value, idx| 605 map_derivative( 606 value, [*path, idx], 607 transform_keys: transform_keys, leaf: leaf, 608 &block 609 ) 610 end 611 else 612 yield path, object 613 end 614 end