Class: Uniword::Themes::MediaFile
- Inherits:
-
Object
- Object
- Uniword::Themes::MediaFile
- 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
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Binary content data.
-
#content_type ⇒ Object
readonly
MIME content type (e.g., ‘image/jpeg’, ‘image/png’).
-
#filename ⇒ Object
readonly
Media filename (e.g., ‘image1.jpeg’).
-
#source_path ⇒ Object
readonly
Original path in theme package (e.g., ‘theme/media/image1.jpeg’).
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Value-based equality.
-
#extension ⇒ String?
Get file extension.
-
#hash ⇒ Integer
Hash code for value-based hashing.
-
#image? ⇒ Boolean
Check if this is an image file.
-
#initialize(attributes = {}) ⇒ MediaFile
constructor
Create a new MediaFile.
-
#size ⇒ Integer
Get file size in bytes.
-
#validate! ⇒ void
Validate required attributes.
Constructor Details
#initialize(attributes = {}) ⇒ MediaFile
Create a new MediaFile
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
#content ⇒ Object (readonly)
Binary content data
25 26 27 |
# File 'lib/uniword/theme/media_file.rb', line 25 def content @content end |
#content_type ⇒ Object (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 |
#filename ⇒ Object (readonly)
Media filename (e.g., ‘image1.jpeg’)
22 23 24 |
# File 'lib/uniword/theme/media_file.rb', line 22 def filename @filename end |
#source_path ⇒ Object (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
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 |
#extension ⇒ String?
Get file extension
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 |
#hash ⇒ Integer
Hash code for value-based hashing
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
61 62 63 |
# File 'lib/uniword/theme/media_file.rb', line 61 def image? content_type&.start_with?("image/") end |
#size ⇒ Integer
Get file size 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
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 |