Module: Cooltrainer::DistorteD::Invoker
- Included in:
- ClickAgain
- Defined in:
- lib/distorted/invoker.rb
Instance Method Summary collapse
-
#basename ⇒ Object
Filename without the dot-and-extension.
- #lower_world ⇒ Object
-
#method_missing(meth, *args, **kwargs, &block) ⇒ Object
MediaMolecule file-type plugger.
- #outer_limits(all: false) ⇒ Object
-
#respond_to_missing?(meth, *a) ⇒ Boolean
Make sure :respond_to? works for yet-unplugged distorted_methods.
-
#type_mars ⇒ Object
Returns a Set of MIME::Types common to the source file and our supported MediaMolecules.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, **kwargs, &block) ⇒ Object
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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 |
Instance Method Details
#basename ⇒ Object
Filename without the dot-and-extension.
37 38 39 |
# File 'lib/distorted/invoker.rb', line 37 def basename File.basename(@name, '.*') end |
#lower_world ⇒ Object
16 17 18 19 20 21 22 |
# 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 |
#outer_limits(all: false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# 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) ⇒ Boolean
Make sure :respond_to? works for yet-unplugged distorted_methods. blog.marc-andre.ca/2010/11/15/methodmissing-politely/
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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 ⇒ Object
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.
43 44 45 46 47 |
# 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 |