Class: RailsDevtools::ImageAssets::ImageInfo

Inherits:
Object
  • Object
show all
Defined in:
app/models/rails_devtools/image_assets/image_info.rb

Constant Summary collapse

IMAGE_EXTENSIONS =
Set[
  ".jpg", ".jpeg", # JPEG
  ".png",          # PNG
  ".gif",          # GIF
  ".webp",         # WebP
  ".svg",          # SVG
  ".avif",         # AVIF
  ".ico"           # Favicon
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(image_path) ⇒ ImageInfo

Returns a new instance of ImageInfo.



16
17
18
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 16

def initialize(image_path)
  @image_path = image_path
end

Instance Method Details

#basenameObject



32
33
34
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 32

def basename
  @basename ||= File.basename(@image_path)
end

#devtools_image_pathObject



52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 52

def devtools_image_path
  return @devtools_image_path if defined?(@devtools_image_path)

  matching_path = asset_config.paths.find { |asset_path| @image_path.start_with?(asset_path) }
  matching_base = Pathname.new(matching_path).join(asset_config.implicit_path)
  asset_path = Pathname.new(@image_path)

  @devtools_image_path = asset_path
    .relative_path_from(matching_base)
    .to_s.sub("../", "")
end

#extensionObject



40
41
42
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 40

def extension
  File.extname(@image_path).downcase
end

#file_sizeObject



44
45
46
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 44

def file_size
  @file_size ||= FastImage.new(@image_path).content_length
end

#full_pathObject



20
21
22
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 20

def full_path
  @image_path
end

#heightObject



68
69
70
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 68

def height
  size[1]
end

#image?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 28

def image?
  FastImage.type(@image_path).present?
end

#image_helper_snippetObject



48
49
50
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 48

def image_helper_snippet
  "#{asset_config.helper_snippet}(\"#{devtools_image_path}\")"
end

#nameObject



36
37
38
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 36

def name
  basename.split(".").first
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 24

def valid?
  File.file?(@image_path) && image?
end

#widthObject



64
65
66
# File 'app/models/rails_devtools/image_assets/image_info.rb', line 64

def width
  size[0]
end