Class: Konjac::Office::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/konjac/office/base.rb,
lib/konjac/office/item.rb

Overview

This is a basic class that contains all the methods that are universal across OSes, file formats, applications, etc.

Direct Known Subclasses

Mac::Shared

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil) ⇒ Base

Creates a new Base object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/konjac/office/base.rb', line 13

def initialize(location = nil)
  @document = open(File.expand_path(location)) unless location.nil?
  @document = active_document
  @item_opts = {
    :application => @application,
    :document    => @document,
    :delimiter   => "\r"
  }
  @shape_opts = {
    :application => @application,
    :document    => @document,
    :delimiter   => "\v"
  }
end

Instance Attribute Details

#documentObject (readonly)

The active document



10
11
12
# File 'lib/konjac/office/base.rb', line 10

def document
  @document
end

Instance Method Details

#[](*args) ⇒ Object Also known as: item_at

Finds the item at the specified indices. This method accepts both variadic inputs (i.e. 1, 2, 3) or the equivalent hash (i.e. :sheet => 1, :row => 2, :cell => 3. The following pairs are equivalent:

doc[1]
doc[:paragraph => 1]
doc.item_at 1
doc.item_at :paragraph => 1

xl[1, 1, 1]
xl.item_at :sheet => 1, :row => 1, :cell => 1

pp[1, 1]
pp.item_at :slide => 1, :shape => 1

doc[1, :type => :shape]
doc :shape => 1, :type => :shape
doc.shape_at 1

along with all the obvious permutations.



49
50
51
52
53
54
# File 'lib/konjac/office/base.rb', line 49

def [](*args)
  opts = parse_args(*args)
  return shape_at(opts) if opts[:type] == :shape

  Item.new @item_opts.merge(opts)
end

#[]=(*args) ⇒ Object

Sets the item at the specified indices to the value of the first argument or the :text member of the supplied hash



59
60
61
# File 'lib/konjac/office/base.rb', line 59

def []=(*args)
  write args.pop, *args
end

#exportObject

Exports Tags to the specified path



87
88
89
# File 'lib/konjac/office/base.rb', line 87

def export
  Tag.dump data, path
end

#importObject

Imports Tags from the specified path into the document



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/konjac/office/base.rb', line 92

def import
  tags.each do |tag|
    if tag.changed? && !tag.blank?
      if tag.type == :shape
        added = tag.added.join(@shape_opts[:delimiter])
      else
        added = tag.added.join(@item_opts[:delimiter])
      end
      write added, *tag.indices, :type => tag.type
    end
  end
end

#read(*args) ⇒ Object

Reads from the item at the specified indices



77
78
79
# File 'lib/konjac/office/base.rb', line 77

def read(*args)
  item_at(*args).read
end

#shape_at(*args) ⇒ Object

Retrieves the shape at the specified indices



64
65
66
67
68
69
# File 'lib/konjac/office/base.rb', line 64

def shape_at(*args)
  last_item = args.last
  last_item = { :type => :shape }.merge(last_item) if last_item.is_a?(Hash)
  opts = parse_args(*args)
  Item.new @shape_opts.merge(opts)
end

#tagsObject

Loads Tags from the supplied path



82
83
84
# File 'lib/konjac/office/base.rb', line 82

def tags
  Tag.load path
end

#write(text, *args) ⇒ Object

Writes to the item at the specified indices



72
73
74
# File 'lib/konjac/office/base.rb', line 72

def write(text, *args)
  item_at(*args).write text
end