Module: FeduxOrgStdlib::Roles::Typable

Defined in:
lib/fedux_org_stdlib/roles/typable.rb

Overview

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`.

Examples:


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

Instance Method Summary collapse

Instance Method Details

#extnamesObject

Return extensions



83
84
85
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 83

def extnames
  source_path.basename.to_s.scan(/(\.[^.]+)/).flatten
end

#extnames?(*exts) ⇒ Boolean

Has file extensions

Parameters:

  • exts (Array)

    The extensions to check

Returns:

  • (Boolean)


78
79
80
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 78

def extnames?(*exts)
  !(extnames & exts.flatten).empty?
end

#file?Boolean

Is file?

Returns:

  • (Boolean)


63
64
65
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 63

def file?
  source_path.file?
end

#font?Boolean

Is font?

Returns:

  • (Boolean)


128
129
130
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 128

def font?
  font_by_path? || font_by_extension?
end

#image?Boolean

Is image?

Returns:

  • (Boolean)


88
89
90
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 88

def image?
  image_by_path? || (image_by_extension? && !font_by_path?)
end

#script?Boolean

Is script?

Returns:

  • (Boolean)


147
148
149
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 147

def script?
  script_by_path? || script_by_extension?
end

#source_pathObject



35
36
37
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 35

def source_path
  fail NoMethodError, :source_path
end

#stylesheet?Boolean

Is stylesheet?

Returns:

  • (Boolean)


108
109
110
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 108

def stylesheet?
  stylesheet_by_path? || stylesheet_by_extension?
end

#typeObject

Determine type of object



48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ true, false

Check on file type

Returns:

  • (true, false)

    Is true if has type



43
44
45
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 43

def type?(t)
  type == t
end

#valid?true, false

Is it a valid

Returns:

  • (true, false)

    If it is valid return true



70
71
72
# File 'lib/fedux_org_stdlib/roles/typable.rb', line 70

def valid?
  file?
end