Module: Shift::Transformable

Included in:
String
Defined in:
lib/shift/string.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



64
65
66
# File 'lib/shift/string.rb', line 64

def method_missing(*args)
  can?(*args) ? process(*args) : super
end

Instance Attribute Details

#nameObject Also known as: path, format

Returns the value of attribute name.



18
19
20
# File 'lib/shift/string.rb', line 18

def name
  @name
end

Instance Method Details

#can?(*args) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'lib/shift/string.rb', line 43

def can?(*args)
  begin
    interface(*args)
  rescue LookupError
    return false
  end
  true
end

#interface(action = :default, name_override = nil) ⇒ Shift::Interface

Get the default interface class for this transformable.

Parameters:

  • action (Symbol) (defaults to: :default)

    The action to perform. Sample: :compress

  • name_override (String) (defaults to: nil)

    Use the specified file name or extension rather than #name

Returns:

Raises:



38
39
40
41
42
# 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

#process(action = :default, name_override = nil) ⇒ Shift::String

Process the string with one of the Shift interfaces. (see Shift::String#interface)

Returns:



56
57
58
59
60
61
62
# 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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/shift/string.rb', line 68

def respond_to?(method)
  super || can?(method)
end