module FeduxOrgStdlib::Roles::Typable

Typable

If need to determine the file type of a object, you can include `FeduxOrgStdlib::Roles::Typeable`. You only need to define a method `source_path`, which is used by the included methods. It needs to return a `Pathname`.

@example

class MyClass
  include FeduxOrgStdlib::Roles::Typeable

  private

  attr_reader :source_path

  public

  def initialize(source_path:)
    @source_path = Pathname.new(source_path)
  end
end

object = MyClass.new('images/image.png')
object.type # => :image

object = MyClass.new('images/image.css')
object.type        # => :stylesheet
object.image?      # => false
object.stylesheet? # => true

Public Instance Methods

extnames() click to toggle source

Return extensions

# File lib/fedux_org_stdlib/roles/typable.rb, line 83
def extnames
  source_path.basename.to_s.scan(/(\.[^.]+)/).flatten
end
extnames?(*exts) click to toggle source

Has file extensions

@param [Array] exts

The extensions to check
# File lib/fedux_org_stdlib/roles/typable.rb, line 78
def extnames?(*exts)
  !(extnames & exts.flatten).empty?
end
file?() click to toggle source

Is file?

# File lib/fedux_org_stdlib/roles/typable.rb, line 63
def file?
  source_path.file?
end
font?() click to toggle source

Is font?

# File lib/fedux_org_stdlib/roles/typable.rb, line 128
def font?
  font_by_path? || font_by_extension?
end
image?() click to toggle source

Is image?

# File lib/fedux_org_stdlib/roles/typable.rb, line 88
def image?
  image_by_path? || (image_by_extension? && !font_by_path?)
end
script?() click to toggle source

Is script?

# File lib/fedux_org_stdlib/roles/typable.rb, line 147
def script?
  script_by_path? || script_by_extension?
end
source_path() click to toggle source
# File lib/fedux_org_stdlib/roles/typable.rb, line 35
def source_path
  fail NoMethodError, :source_path
end
stylesheet?() click to toggle source

Is stylesheet?

# File lib/fedux_org_stdlib/roles/typable.rb, line 108
def stylesheet?
  stylesheet_by_path? || stylesheet_by_extension?
end
type() click to toggle source

Determine type of object

# File lib/fedux_org_stdlib/roles/typable.rb, line 48
def type
  @type ||= if image?
              :image
            elsif script?
              :script
            elsif stylesheet?
              :stylesheet
            elsif font?
              :font
            else
              :unknown
            end
end
type?(t) click to toggle source

Check on file type

@return [true, false]

Is true if has type
# File lib/fedux_org_stdlib/roles/typable.rb, line 43
def type?(t)
  type == t
end
valid?() click to toggle source

Is it a valid @return [true, false]

If it is valid return true
# File lib/fedux_org_stdlib/roles/typable.rb, line 70
def valid?
  file?
end

Private Instance Methods

font_by_extension?() click to toggle source

Is font by extension?

# File lib/fedux_org_stdlib/roles/typable.rb, line 140
def font_by_extension?
  extnames?(*%w(.ttf .woff .eot .otf .svg .svgz))
end
font_by_path?() click to toggle source

Is font by path?

# File lib/fedux_org_stdlib/roles/typable.rb, line 135
def font_by_path?
  source_path.dirname.basename.to_s == 'fonts'
end
image_by_extension?() click to toggle source

Is image by extension?

# File lib/fedux_org_stdlib/roles/typable.rb, line 101
def image_by_extension?
  extnames?(*%w(.gif .png .jpg .jpeg .webp .svg .svgz))
end
image_by_path?() click to toggle source

Is image by path?

# File lib/fedux_org_stdlib/roles/typable.rb, line 95
def image_by_path?
  source_path.dirname.basename.to_s == 'images' ||
    source_path.dirname.basename.to_s == 'img'
end
script_by_extension?() click to toggle source

Is script by extension?

# File lib/fedux_org_stdlib/roles/typable.rb, line 160
def script_by_extension?
  extnames?(*%w(.js .coffee))
end
script_by_path?() click to toggle source

Is script by path?

# File lib/fedux_org_stdlib/roles/typable.rb, line 154
def script_by_path?
  source_path.dirname.basename.to_s == 'javascripts' ||
    source_path.dirname.basename.to_s == 'js'
end
stylesheet_by_extension?() click to toggle source

Is stylesheet by extension?

# File lib/fedux_org_stdlib/roles/typable.rb, line 115
def stylesheet_by_extension?
  extnames?(*%w(.css .sass .scss .styl .less))
end
stylesheet_by_path?() click to toggle source

Is stylesheet by path?

# File lib/fedux_org_stdlib/roles/typable.rb, line 120
def stylesheet_by_path?
  source_path.dirname.basename.to_s == 'stylesheets' ||
    source_path.dirname.basename.to_s == 'css'
end