Class: Uniword::Themes::MediaFile

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/theme/media_file.rb

Overview

Value object representing a media file (image, etc.) from a theme package

Responsibility: Hold immutable media file data with proper metadata

This class follows the Value Object pattern:

  • Immutable (frozen after initialization)

  • Value-based equality

  • Self-validating

Examples:

Create media file from theme

media = MediaFile.new(
  filename: 'image1.jpeg',
  content: binary_data,
  content_type: 'image/jpeg'
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ MediaFile

Create a new MediaFile

Parameters:

  • attributes (Hash) (defaults to: {})

    Media file attributes

Options Hash (attributes):

  • :filename (String)

    Required filename

  • :content (String)

    Required binary content

  • :content_type (String)

    Optional MIME type (auto-detected if not provided)

  • :source_path (String)

    Optional original path

Raises:

  • (ArgumentError)

    if required attributes missing



41
42
43
44
45
46
47
48
49
# File 'lib/uniword/theme/media_file.rb', line 41

def initialize(attributes = {})
  @filename = attributes[:filename]
  @content = attributes[:content]
  @content_type = attributes[:content_type] || detect_content_type(@filename)
  @source_path = attributes[:source_path]

  validate!
  freeze # Immutable
end

Instance Attribute Details

#contentObject (readonly)

Binary content data



25
26
27
# File 'lib/uniword/theme/media_file.rb', line 25

def content
  @content
end

#content_typeObject (readonly)

MIME content type (e.g., ‘image/jpeg’, ‘image/png’)



28
29
30
# File 'lib/uniword/theme/media_file.rb', line 28

def content_type
  @content_type
end

#filenameObject (readonly)

Media filename (e.g., ‘image1.jpeg’)



22
23
24
# File 'lib/uniword/theme/media_file.rb', line 22

def filename
  @filename
end

#source_pathObject (readonly)

Original path in theme package (e.g., ‘theme/media/image1.jpeg’)



31
32
33
# File 'lib/uniword/theme/media_file.rb', line 31

def source_path
  @source_path
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Value-based equality

Parameters:

  • other (Object)

    Object to compare

Returns:

  • (Boolean)

    true if equal



78
79
80
81
82
83
84
# File 'lib/uniword/theme/media_file.rb', line 78

def ==(other)
  return false unless other.is_a?(self.class)

  filename == other.filename &&
    content == other.content &&
    content_type == other.content_type
end

#extensionString?

Get file extension

Returns:

  • (String, nil)

    File extension without dot



68
69
70
71
72
# File 'lib/uniword/theme/media_file.rb', line 68

def extension
  return nil unless filename

  File.extname(filename)[1..]
end

#hashInteger

Hash code for value-based hashing

Returns:

  • (Integer)

    hash code



91
92
93
# File 'lib/uniword/theme/media_file.rb', line 91

def hash
  [filename, content, content_type].hash
end

#image?Boolean

Check if this is an image file

Returns:

  • (Boolean)

    true if image



61
62
63
# File 'lib/uniword/theme/media_file.rb', line 61

def image?
  content_type&.start_with?("image/")
end

#sizeInteger

Get file size in bytes

Returns:

  • (Integer)

    Size of content in bytes



54
55
56
# File 'lib/uniword/theme/media_file.rb', line 54

def size
  content&.bytesize || 0
end

#validate!void

This method returns an undefined value.

Validate required attributes

Raises:

  • (ArgumentError)

    if invalid



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/uniword/theme/media_file.rb', line 99

def validate!
  if filename.nil? || filename.empty?
    raise ArgumentError,
          "filename is required"
  end
  raise ArgumentError, "content is required" if content.nil?
  unless filename.is_a?(String)
    raise ArgumentError,
          "filename must be a string"
  end
end