module Shrine::Plugins::Derivatives::ClassMethods
Public Instance Methods
Source
# File lib/shrine/plugins/derivatives.rb 574 def derivatives(object) 575 if object.is_a?(String) 576 derivatives JSON.parse(object) 577 elsif object.is_a?(Hash) || object.is_a?(Array) 578 map_derivative( 579 object, 580 transform_keys: :to_sym, 581 leaf: -> (value) { value.is_a?(Hash) && (value["id"] || value[:id]).is_a?(String) }, 582 ) { |_, value| uploaded_file(value) } 583 else 584 fail ArgumentError, "cannot convert #{object.inspect} to derivatives" 585 end 586 end
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={}> }
Source
# File lib/shrine/plugins/derivatives.rb 619 def derivatives_options 620 opts[:derivatives] 621 end
Returns derivatives plugin options.
Source
# File lib/shrine/plugins/derivatives.rb 590 def map_derivative(object, path = [], transform_keys: :to_sym, leaf: nil, &block) 591 return enum_for(__method__, object) unless block_given? 592 593 if leaf && leaf.call(object) 594 yield path, object 595 elsif object.is_a?(Hash) 596 object.inject({}) do |hash, (key, value)| 597 key = key.send(transform_keys) 598 599 hash.merge! key => map_derivative( 600 value, [*path, key], 601 transform_keys: transform_keys, leaf: leaf, 602 &block 603 ) 604 end 605 elsif object.is_a?(Array) 606 object.map.with_index do |value, idx| 607 map_derivative( 608 value, [*path, idx], 609 transform_keys: transform_keys, leaf: leaf, 610 &block 611 ) 612 end 613 else 614 yield path, object 615 end 616 end
Iterates over a nested collection, yielding on each part of the path. If the block returns a truthy value, that branch is terminated