class Keynote::Document

Constants

DEFAULT_HEIGHT
DEFAULT_WIDTH
WIDE_HEIGHT
WIDE_WIDTH

Attributes

auto_loop[RW]
auto_play[RW]
auto_restart[RW]
current_slide[RW]
document_theme[RW]
file_path[RW]
height[RW]
master_slides[RW]
maximum_idle_duration[RW]
name[RW]
slide_numbers_showing[RW]
slides[RW]
width[RW]

Public Class Methods

all() click to toggle source
# File lib/keynote/document.rb, line 149
def self.all
  self.find_with_conditions
end
create(arguments = {}) click to toggle source
# File lib/keynote/document.rb, line 132
    def self.create(arguments = {})
      theme = arguments[:theme] || Theme.default
      width = arguments[:wide] ? WIDE_WIDTH : DEFAULT_WIDTH
      height = arguments[:wide] ? WIDE_HEIGHT : DEFAULT_HEIGHT
      file_path = arguments[:file_path]

      result = eval_script <<-APPLE.unindent
        var Keynote = Application("Keynote")
        var theme = Keynote.themes.whose({ id: "#{theme.id}" }).first
        var doc = Keynote.Document({ documentTheme: theme, width: #{width}, height: #{height} });
        Keynote.documents.push(doc);
        JSON.stringify(doc.properties());
      APPLE

      self.new(symbolize_keys(result).merge(theme: theme, width: width, height: height, file_path: file_path))
    end
current() click to toggle source
# File lib/keynote/document.rb, line 165
def self.current
  self.all.first
end
find_by(args) click to toggle source
# File lib/keynote/document.rb, line 153
def self.find_by(args)
  raise ArgumentError.new('nil argument is given') unless args

  if args.is_a?(Hash) && args.has_key?(:id)
    conditions = ".whose({ id: '#{args[:id]}' })"
  else
    raise ArgumentError.new('Unsupported argument is given')
  end

  find_with_conditions(conditions)
end
new(arguments = {}) click to toggle source
# File lib/keynote/document.rb, line 31
def initialize(arguments = {})
  @document_theme = arguments[:theme] || Theme.default
  @width = arguments.has_key?(:wide) && arguments[:wide] ? WIDE_WIDTH : DEFAULT_WIDTH
  @height = arguments.has_key?(:wide) && arguments[:wide] ? WIDE_HEIGHT : DEFAULT_HEIGHT
  @file_path = arguments[:file_path]
  @id = arguments[:id]
  @maximum_idle_duration = arguments[:maximumIdleDuration]
  @current_slide = arguments[:currentSlide]
  @slide_numbers_showing = arguments[:slideNumbersShowing]
  @auto_loop = arguments[:autoLoop]
  @auto_play = arguments[:autoPlay]
  @auto_restart = arguments[:autoRestart]
  @maximum_idle_duration = arguments[:maximumIdleDuration]
  @name = arguments[:name]
end

Private Class Methods

find_with_conditions(conditions = '') click to toggle source
# File lib/keynote/document.rb, line 175
    def self.find_with_conditions(conditions = '')
      results = eval_script <<-APPLE.unindent
        var documents = Application("Keynote").documents#{conditions};
        var results = [];
        for(var i = 0, len = documents.length; i < len; i++) {
          results.push(documents[i].properties());
        }
        JSON.stringify(results);
      APPLE

      return [] unless results

      results.map do |result|
        self.new(symbolize_keys(result))
      end
    end
symbolize_keys(hash) click to toggle source
# File lib/keynote/document.rb, line 171
def self.symbolize_keys(hash)
  Hash[hash.map { |k, v| [k.to_sym, v] }]
end

Public Instance Methods

export() click to toggle source
# File lib/keynote/document.rb, line 128
def export
  # TBD
end
save() click to toggle source
# File lib/keynote/document.rb, line 108
    def save
      return false unless @id
      return false unless @file_path
      eval_script <<-APPLE.unindent
        var Keynote = Application("Keynote")
        var doc = Keynote.documents.byId("#{@id}")
        var path = Path("#{@file_path}")
        doc.save({ in: path })
      APPLE
      true
    rescue => e
      false
    end
save!() click to toggle source
# File lib/keynote/document.rb, line 124
def save!
  raise DocumentInvalid unless save
end