Class: ActiveGist

Inherits:
Object
  • Object
show all
Extended by:
API, ClassMethods, ActiveModel::Callbacks, ActiveModel::Naming
Includes:
API, Attributes, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations
Defined in:
lib/active_gist.rb,
lib/active_gist/version.rb

Defined Under Namespace

Modules: API, Attributes, ClassMethods, Errors, Version Classes: Files

Constant Summary collapse

VERSION =
ActiveGist::Version::STRING

Constants included from Attributes

Attributes::GIST_ATTRIBUTES

Instance Attribute Summary

Attributes included from Attributes

#comments, #comments_url, #commits_url, #created_at, #description, #forks, #forks_url, #git_pull_url, #git_push_url, #history, #html_url, #id, #updated_at, #url, #user

Instance Method Summary collapse

Methods included from API

api, password, password, password=, username, username, username=

Methods included from ClassMethods

all, count, create, create!, find, first, last, load

Methods included from Attributes

#[], #[]=, #attributes, #attributes=, included, #public=, #public?

Constructor Details

#initialize(attributes = {}) ⇒ ActiveGist

Returns a new instance of ActiveGist.



36
37
38
39
40
# File 'lib/active_gist.rb', line 36

def initialize(attributes = {})
  run_callbacks :initialize do
    self.attributes = attributes
  end
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
# File 'lib/active_gist.rb', line 76

def ==(other)
  other.kind_of?(ActiveGist) && id == other.id
end

#_changed?Object

:nodoc:



31
# File 'lib/active_gist.rb', line 31

alias _changed? changed?

#changed?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/active_gist.rb', line 32

def changed?
  _changed? || files.changed?
end

#destroyObject



58
59
60
61
62
# File 'lib/active_gist.rb', line 58

def destroy
  api[id].delete :accept => 'application/json'
  @id = nil
  @destroyed = true
end

#destroyed?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/active_gist.rb', line 54

def destroyed?
  !!@destroyed
end

#filesObject



72
73
74
# File 'lib/active_gist.rb', line 72

def files
  @files ||= ActiveGist::Files.new
end

#files=(hash) ⇒ Object



68
69
70
# File 'lib/active_gist.rb', line 68

def files=(hash)
  files.replace_with hash.with_indifferent_access
end

#forkObject



64
65
66
# File 'lib/active_gist.rb', line 64

def fork
  self.class.load(JSON.parse api[id]['fork'].post("", :accept => 'application/json'))
end

#inspectObject



42
43
44
# 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?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/active_gist.rb', line 50

def new_record?
  !destroyed? && id.blank?
end

#persisted?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/active_gist.rb', line 46

def persisted?
  !destroyed? && !id.blank? && !changed?
end

#saveObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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!Object



108
109
110
# File 'lib/active_gist.rb', line 108

def save!
  raise ActiveGist::Errors::Invalid, "Gist is invalid: #{errors.full_messages.join('; ')}" unless save
end

#star!Object



125
126
127
128
# File 'lib/active_gist.rb', line 125

def star!
  api[id]['star'].put("", :accept => 'application/json')
  @star = true
end

#starred?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
# 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!Object



130
131
132
133
# File 'lib/active_gist.rb', line 130

def unstar!
  api[id]['star'].delete(:accept => 'application/json')
  @star = false
end