Class: Caracal::Core::Models::RelationshipModel
- Inherits:
-
BaseModel
- Object
- BaseModel
- Caracal::Core::Models::RelationshipModel
show all
- Defined in:
- lib/caracal/core/models/relationship_model.rb
Overview
This class encapsulates the logic needed to store and manipulate
relationship data.
Constant Summary
collapse
- TYPE_MAP =
{
font: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable',
footer: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer',
image: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
link: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
numbering: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering',
setting: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings',
style: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles'
}
- IMAGE_SIGNATURES =
{
"\x89PNG\r\n\x1A\n".b => 'png',
"\xFF\xD8\xFF".b => 'jpeg',
'GIF8'.b => 'gif',
'BM'.b => 'bmp',
"II*\x00".b => 'tiff',
"MM\x00*".b => 'tiff'
}
- IMAGE_EXTENSIONS =
%w(png jpeg jpg gif bmp tif tiff svg)
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from BaseModel
#initialize
Instance Attribute Details
#relationship_data ⇒ Object
Returns the value of attribute relationship_data.
43
44
45
|
# File 'lib/caracal/core/models/relationship_model.rb', line 43
def relationship_data
@relationship_data
end
|
#relationship_id ⇒ Object
39
40
41
|
# File 'lib/caracal/core/models/relationship_model.rb', line 39
def relationship_id
@relationship_id
end
|
#relationship_key ⇒ Object
Returns the value of attribute relationship_key.
40
41
42
|
# File 'lib/caracal/core/models/relationship_model.rb', line 40
def relationship_key
@relationship_key
end
|
#relationship_target ⇒ Object
Returns the value of attribute relationship_target.
42
43
44
|
# File 'lib/caracal/core/models/relationship_model.rb', line 42
def relationship_target
@relationship_target
end
|
#relationship_type ⇒ Object
Returns the value of attribute relationship_type.
41
42
43
|
# File 'lib/caracal/core/models/relationship_model.rb', line 41
def relationship_type
@relationship_type
end
|
Instance Method Details
#data(value) ⇒ Object
100
101
102
|
# File 'lib/caracal/core/models/relationship_model.rb', line 100
def data(value)
@relationship_data = value.to_s
end
|
GETTERS =============================
53
54
55
|
# File 'lib/caracal/core/models/relationship_model.rb', line 53
def formatted_id
"rId#{ relationship_id }"
end
|
57
58
59
60
61
62
63
|
# File 'lib/caracal/core/models/relationship_model.rb', line 57
def formatted_target
if relationship_type == :image
"media/image#{ relationship_id }.#{ image_extension }"
else
relationship_target
end
end
|
80
81
82
|
# File 'lib/caracal/core/models/relationship_model.rb', line 80
def formatted_type
TYPE_MAP.fetch(relationship_type)
end
|
#id(value) ⇒ Object
SETTERS =============================
87
88
89
|
# File 'lib/caracal/core/models/relationship_model.rb', line 87
def id(value)
@relationship_id = value.to_i
end
|
#image_extension ⇒ Object
Prefers the type detected from the image bytes, then the
extension of the target's path (ignoring any query string),
and falls back to png so the part name is always valid.
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/caracal/core/models/relationship_model.rb', line 69
def image_extension
data = relationship_data.to_s.b
if (match = IMAGE_SIGNATURES.find { |sig, _| data.start_with?(sig) })
return match.last
end
path = relationship_target.to_s.split(/[?#]/).first.to_s
ext = File.extname(path).delete('.').downcase
IMAGE_EXTENSIONS.include?(ext) ? ext : 'png'
end
|
#matches?(str) ⇒ Boolean
STATE ===============================
107
108
109
|
# File 'lib/caracal/core/models/relationship_model.rb', line 107
def matches?(str)
relationship_key.downcase == str.to_s.downcase
end
|
#target(value) ⇒ Object
95
96
97
98
|
# File 'lib/caracal/core/models/relationship_model.rb', line 95
def target(value)
@relationship_target = value.to_s
@relationship_key = value.to_s.downcase
end
|
#target_mode? ⇒ Boolean
111
112
113
|
# File 'lib/caracal/core/models/relationship_model.rb', line 111
def target_mode?
relationship_type == :link
end
|
#type(value) ⇒ Object
91
92
93
|
# File 'lib/caracal/core/models/relationship_model.rb', line 91
def type(value)
@relationship_type = value.to_s.downcase.to_sym
end
|
#valid? ⇒ Boolean
VALIDATION ===========================
118
119
120
121
|
# File 'lib/caracal/core/models/relationship_model.rb', line 118
def valid?
required = [:id, :target, :type]
required.all? { |m| !send("relationship_#{ m }").nil? }
end
|