Ravn::

DatadirUtilities module

When extended in a class, automatically set the path to a DATA_DIR constant, derived from the class name. Prefers environmental override, Gem path, then local filesystem pathing.

This can also be called manually if the including class name doesn’t match the gem, or something else esoteric.

DATA_DIR is a Pathname object.

Public Class Methods

extended( obj )

Extend hook: Set the DATA_DIR constant in the extending class.

# File lib/ravn/mixins.rb, line 777
def self::extended( obj )
        name = obj.name.downcase.gsub( '::', '-' )
        dir = self.find_datadir( name )

        obj.const_set( :DATA_DIR, dir )
        obj.singleton_class.attr_accessor :data_dir
        obj.data_dir = dir
end
find_datadir( gemname, env: nil )

Return a pathname object for the extended class DATA_DIR. This allows for the DATA_DIR constant to be used transparently between development (local checkout) and production (gem installation) environments.

# File lib/ravn/mixins.rb, line 792
def self::find_datadir( gemname, env: nil )
        unless env
                comps = gemname.split( '-' )
                env = comps.size > 1 ? comps.last : comps.first
                env = "%s_DATADIR" % [ env.upcase ]
        end

        # rubocop:disable Layout/IndentationWidth
        dir = if ENV[ env ]
                        Pathname( ENV[ env ] )
                elsif Gem.loaded_specs[ gemname ] && File.exist?( Gem.loaded_specs[ gemname ].datadir )
                        Pathname( Gem.loaded_specs[ gemname ].datadir )
                else
                        caller_path = caller_locations( 2, 1 ).first.absolute_path
                        Pathname( caller_path ).dirname.parent.parent + "data/#{gemname}"
                end
                # rubocop:enable Layout/IndentationWidth

        return dir
end