module Cooltrainer::DistorteD::Invoker

Public Instance Methods

basename() click to toggle source

Filename without the dot-and-extension.

# File lib/distorted/invoker.rb, line 37
def basename
  File.basename(@name, '.*')
end
lower_world() click to toggle source

Returns a Hash => Hash => Hash => Compound

# File lib/distorted/invoker.rb, line 16
def lower_world
  Cooltrainer::DistorteD::IMPLANTATION(:LOWER_WORLD).each_with_object(
    Hash.new { |pile, type| pile[type] = Hash[] }
  ) { |(key, types), pile|
    types.each { |type, elements| pile.update(type => {key.molecule => elements}) { |k,o,n| o.merge(n) }}
  }
end
method_missing(meth, *args, **kwargs, &block) click to toggle source

MediaMolecule file-type plugger. Any call to a MIME::Type's distorted_method will end up here unless the Molecule that defines it has been `prepend`ed to our instance.

Calls superclass method
# File lib/distorted/invoker.rb, line 52
def method_missing(meth, *args, **kwargs, &block)
  # Only consider method names with our prefixes.
  if MIME::Type::DISTORTED_METHOD_PREFIXES.values.map(&:to_s).include?(meth.to_s.split(MIME::Type::SUB_TYPE_SEPARATORS)[0])
    # TODO: Might need to handle cases here where the Set[Molecule]
    # exists but none of them defined our method.
    unless self.singleton_class.instance_variable_get(:@media_molecules)
      unless outer_limits.empty?
        self.singleton_class.instance_variable_set(
          :@media_molecules,
          outer_limits.keys.each_with_object(Set[]) { |molecule, molecules|
            self.singleton_class.prepend(molecule)
            molecules.add(molecule)
          }
        )
        # `return` to ensure we don't fall through to #method_missing:super
        # if we are going to do any work, otherwise a NoMethodError will
        # still be raised despite the distorted_method :sends suceeding.
        #
        # Use :__send__ in case a Molecule defines a `:send` method.
        # https://ruby-doc.org/core/Object.html#method-i-send
        return self.send(meth, *args, **kwargs, &block)
      end
    end
  end
  # …and I still haven't found it! — What I'm looking for, that is.
  # https://www.youtube.com/watch?v=xqse3vYcnaU
  super
end
outer_limits(all: false) click to toggle source

Returns a Hash => Hash => Hash => Compound

# File lib/distorted/invoker.rb, line 25
def outer_limits(all: false)
  Cooltrainer::DistorteD::IMPLANTATION(
    :OUTER_LIMITS,
    (all || type_mars.empty?) ? Cooltrainer::DistorteD::media_molecules : type_mars.each_with_object(Set[]) { |type, molecules|
      molecules.merge(lower_world[type].keys)
    },
  ).each_with_object(Hash.new { |pile, type| pile[type] = Hash[] }) { |(key, types), pile|
    types.each { |type, elements| pile.update(key.molecule => {type => elements}) { |k,o,n| o.merge(n) }}
  }
end
respond_to_missing?(meth, *a) click to toggle source

Make sure :respond_to? works for yet-unplugged distorted_methods. blog.marc-andre.ca/2010/11/15/methodmissing-politely/

Calls superclass method
# File lib/distorted/invoker.rb, line 83
def respond_to_missing?(meth, *a)
  # We can tell if a method looks like one of ours if it has at least 3 (maybe more!)
  # underscore-separated components with a valid prefix as the first component
  # and the media-type and sub-type as the rest, e.g.
  #
  # irb(main)> 'to_application_pdf'.split('_')
  # => ["to", "application", "pdf"]
  #
  # irb(main)> CHECKING::YOU::OUT('.docx').first.distorted_file_method.to_s.split('_')
  # => ["write", "application", "vnd", "openxmlformats", "officedocument", "wordprocessingml", "document"]
  parts = meth.to_s.split(MIME::Type::SUB_TYPE_SEPARATORS)
  MIME::Type::DISTORTED_METHOD_PREFIXES.values.map(&:to_s).include?(parts[0]) && parts.length > 2 || super(meth, *a)
end
type_mars() click to toggle source

Returns a Set of MIME::Types common to the source file and our supported MediaMolecules. Each of these Molecules will be plugged to the current instance.

# File lib/distorted/invoker.rb, line 43
def type_mars
  @type_mars ||= CHECKING::YOU::OUT(path, so_deep: true) & lower_world.keys.to_set
  raise MediaTypeNotImplementedError.new(@name) if @type_mars.empty?
  @type_mars
end