class ActiveGist

Constants

VERSION

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/active_gist.rb, line 36
def initialize(attributes = {})
  run_callbacks :initialize do
    self.attributes = attributes
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/active_gist.rb, line 76
def ==(other)
  other.kind_of?(ActiveGist) && id == other.id
end
_changed?()
Alias for: changed?
changed?() click to toggle source
# File lib/active_gist.rb, line 32
def changed?
  _changed? || files.changed?
end
Also aliased as: _changed?
destroy() click to toggle source
# File lib/active_gist.rb, line 58
def destroy
  api[id].delete :accept => 'application/json'
  @id = nil
  @destroyed = true
end
destroyed?() click to toggle source
# File lib/active_gist.rb, line 54
def destroyed?
  !!@destroyed
end
files() click to toggle source
# File lib/active_gist.rb, line 72
def files
  @files ||= ActiveGist::Files.new
end
files=(hash) click to toggle source
# File lib/active_gist.rb, line 68
def files=(hash)
  files.replace_with hash.with_indifferent_access
end
fork() click to toggle source
# File lib/active_gist.rb, line 64
def fork
  self.class.load(JSON.parse api[id]['fork'].post("", :accept => 'application/json'))
end
inspect() click to toggle source
# File lib/active_gist.rb, line 42
def inspect
  "#<#{self.class.name} #{attributes.collect { |a| [a[0], a[1].inspect].join('=') }.join(' ')}>"
end
new_record?() click to toggle source
# File lib/active_gist.rb, line 50
def new_record?
  !destroyed? && id.blank?
end
persisted?() click to toggle source
# File lib/active_gist.rb, line 46
def persisted?
  !destroyed? && !id.blank? && !changed?
end
save() click to toggle source
# File lib/active_gist.rb, line 80
def save
  valid = begin
    run_callbacks :validation do
      valid?
    end
  end
  
  return false unless valid
  
  create_or_update_callback = new_record? ? :create : :update
  run_callbacks create_or_update_callback do
    run_callbacks :save do
      if new_record?
        data = as_json(:only => [:description, :public, :files]).to_json
        response = api.post data, :content_type => 'application/json', :accept => 'application/json'
      else
        data = as_json(:only => [:description, :files]).to_json
        response = api[id].patch data, :content_type => 'application/json', :accept => 'application/json'
      end
      self.attributes = JSON.parse response
      @previously_changed = changes
      changed_attributes.clear
    end
  end

  true
end
save!() click to toggle source
# File lib/active_gist.rb, line 108
def save!
  raise ActiveGist::Errors::Invalid, "Gist is invalid: #{errors.full_messages.join('; ')}" unless save
end
star!() click to toggle source
# File lib/active_gist.rb, line 125
def star!
  api[id]['star'].put("", :accept => 'application/json')
  @star = true
end
starred?() click to toggle source
# File lib/active_gist.rb, line 112
def starred?
  if @star.nil?
    @star = begin
      @star = api[id]['star'].get :accept => 'application/json'
      true
    rescue RestClient::ResourceNotFound
      false
    end
  else
    @star
  end
end
unstar!() click to toggle source
# File lib/active_gist.rb, line 130
def unstar!
  api[id]['star'].delete(:accept => 'application/json')
  @star = false
end