class SimpleMDM::App

Public Class Methods

all() click to toggle source
# File lib/simplemdm/app.rb, line 21
def self.all
  hash, code = fetch("apps")

  hash['data'].collect { |d| build d }
end
find(id) click to toggle source
# File lib/simplemdm/app.rb, line 27
def self.find(id)
  hash, code = fetch("apps/#{id}")

  build hash['data']
end
new(source_hash = nil, default = nil, &blk) click to toggle source

overwrite base class

Calls superclass method
# File lib/simplemdm/app.rb, line 5
def initialize(source_hash = nil, default = nil, &blk)
  if source_hash && source_hash.kind_of?(Hash) && binary = source_hash[:binary]
    self.binary = binary
    source_hash.delete(:binary)
  end

  super(source_hash, detault, &blk)
end

Public Instance Methods

binary=(val) click to toggle source
# File lib/simplemdm/app.rb, line 33
def binary=(val)
  raise "binary must be a File object" unless val.kind_of? File

  @dirty = true
  @binary_file = val
end
build(hash = nil) click to toggle source
Calls superclass method
# File lib/simplemdm/app.rb, line 14
def build(hash = nil)
  @dirty = false
  @binary_file = nil

  super
end
destroy() click to toggle source
# File lib/simplemdm/app.rb, line 74
def destroy
  raise "You cannot delete an app that hasn't been created yet." if new?

  hash, code = fetch("apps/#{self.id}", :delete)

  code == 204
end
name=(val) click to toggle source
# File lib/simplemdm/app.rb, line 40
def name=(val)
  if val != self.name
    @dirty = true
  end

  self['name'] = val
end
save(deploy_to) click to toggle source
# File lib/simplemdm/app.rb, line 48
def save(deploy_to)
  raise "binary must be set before saving" if new? && @binary_file.nil?

  if @dirty || new?
    params = {}
    params[:name]   = self.name unless self.name.nil?
    params[:binary] = @binary_file if @binary_file
    params[:deploy_to] = deploy_to

    if new?
      hash, code = fetch("apps", :post, params)

      self.id = hash['data']['id']
      self.merge!(hash['data']['attributes'])
    else
      hash, code = fetch("apps/#{self.id}", :patch, params)
      self.merge!(hash['data']['attributes'])
    end

    @dirty       = false
    @binary_file = nil
  end

  self
end