module Shift::Transformable
Mixin for things that should be transformable by Shift
. Classes using the mixin should implement two methods:
-
‘#data` the data to be processed
-
‘#name` the file or extension name of the data
This is needed for the mappings to work, even if the type is not a string, and/or is not persisted to disk. In this respect, the name is actually the name of the type.
Attributes
Public Instance Methods
# File lib/shift/string.rb, line 43 def can?(*args) begin interface(*args) rescue LookupError return false end true end
Get the default interface class for this transformable.
@param [Symbol] action The action to perform.
Sample: `:compress`
@param [String] name_override Use the specified file name or
extension rather than `#name`
@return [Shift::Interface] The preferred available default
interface
@raise [UnknownFormatError] when a type is needed but cannot
be determined.
@raise [UnknownActionError] when no such action exists. @raise [DependencyError] when required interfaces are
not available.
# File lib/shift/string.rb, line 38 def interface(action=:default, name_override=nil) return action if action.is_a?(Shift::Interface) iface = Shift[name_override || name, action] || name_error iface.new end
# File lib/shift/string.rb, line 64 def method_missing(*args) can?(*args) ? process(*args) : super end
Process the string with one of the Shift
interfaces. @return [Shift::String] transformed Shift
string. (see Shift::String#interface
)
# File lib/shift/string.rb, line 56 def process(action=:default, name_override=nil) iface = interface(action, name_override) self.class.new( iface.process(data), iface.rename(name_override || name) ) end
# File lib/shift/string.rb, line 68 def respond_to?(method) super || can?(method) end
Private Instance Methods
# File lib/shift/string.rb, line 74 def name_error raise(UnknownFormatError, 'cannot determine format without clues.') end