Class: Vectory::Vector

Inherits:
Image
  • Object
show all
Defined in:
lib/vectory/vector.rb

Direct Known Subclasses

Emf, Eps, Ps, Svg

Instance Attribute Summary collapse

Attributes inherited from Image

#content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Image

from_content

Constructor Details

#initialize(content = nil, initial_path = nil) ⇒ Vector

Returns a new instance of Vector.



29
30
31
32
33
# File 'lib/vectory/vector.rb', line 29

def initialize(content = nil, initial_path = nil)
  super(content)

  @initial_path = initial_path
end

Instance Attribute Details

#initial_pathObject (readonly)

Returns the value of attribute initial_path.



27
28
29
# File 'lib/vectory/vector.rb', line 27

def initial_path
  @initial_path
end

Class Method Details

.default_extensionObject



17
18
19
20
# File 'lib/vectory/vector.rb', line 17

def self.default_extension
  raise Vectory::NotImplementedError,
        "#default_extension should be implemented in a subclass."
end

.from_datauri(uri) ⇒ Object



13
14
15
# File 'lib/vectory/vector.rb', line 13

def self.from_datauri(uri)
  Datauri.new(uri).to_vector
end

.from_path(path) ⇒ Object



8
9
10
11
# File 'lib/vectory/vector.rb', line 8

def self.from_path(path)
  content = File.read(path, mode: "rb")
  new(content, path)
end

.mimetypeObject



22
23
24
25
# File 'lib/vectory/vector.rb', line 22

def self.mimetype
  raise Vectory::NotImplementedError,
        "#mimetype should be implemented in a subclass."
end

Instance Method Details

#convert_with_inkscape(inkscape_options, target_class) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/vectory/vector.rb', line 61

def convert_with_inkscape(inkscape_options, target_class)
  with_file(self.class.default_extension) do |input_path|
    output_extension = target_class.default_extension
    output_path = InkscapeConverter.instance.convert(input_path,
                                                     output_extension,
                                                     inkscape_options)

    target_class.from_path(output_path)
  end
end

#file_sizeObject



43
44
45
46
47
# File 'lib/vectory/vector.rb', line 43

def file_size
  raise(NotWrittenToDiskError) unless @path

  File.size(@path)
end

#heightObject



49
50
51
# File 'lib/vectory/vector.rb', line 49

def height
  InkscapeConverter.instance.height(content, self.class.default_extension)
end

#mimeObject



35
36
37
# File 'lib/vectory/vector.rb', line 35

def mime
  self.class.mimetype
end

#pathObject



80
81
82
# File 'lib/vectory/vector.rb', line 80

def path
  @path || raise(NotWrittenToDiskError)
end

#sizeObject



39
40
41
# File 'lib/vectory/vector.rb', line 39

def size
  content.bytesize
end

#to_uriObject



57
58
59
# File 'lib/vectory/vector.rb', line 57

def to_uri
  Datauri.from_vector(self)
end

#widthObject



53
54
55
# File 'lib/vectory/vector.rb', line 53

def width
  InkscapeConverter.instance.width(content, self.class.default_extension)
end

#write(path = nil) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/vectory/vector.rb', line 72

def write(path = nil)
  target_path = path || @path || tmp_path
  File.binwrite(target_path, content)
  @path = File.expand_path(target_path)

  self
end