Class: Posterous::Model

Inherits:
Object
  • Object
show all
Extended by:
Connection
Includes:
Inheritable
Defined in:
lib/posterous/model.rb

Constant Summary

Constants included from Connection

Connection::ConnectionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection

default_options, default_params, http, parse, wait_if_needed

Methods included from Inheritable

included

Constructor Details

#initialize(struct) ⇒ Model

Returns a new instance of Model.



123
124
125
# File 'lib/posterous/model.rb', line 123

def initialize struct
  @struct = struct
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/posterous/model.rb', line 139

def method_missing sym, *args, &block
  if struct.respond_to? sym
    changed_fields.push(sym.to_s.sub('=','').to_sym) if sym.to_s =~ /=/
    return struct.send(sym,*args)
  end
  super(sym, *args, &block)
end

Instance Attribute Details

#structObject (readonly)

Returns the value of attribute struct.



6
7
8
# File 'lib/posterous/model.rb', line 6

def struct
  @struct
end

Class Method Details

.all(params = {}) ⇒ Object

Get a collection for a model

Site.posts.all(:page => 1)



40
41
42
43
# File 'lib/posterous/model.rb', line 40

def self.all params={}
  result = get( parsed_resource_url, params )
  result.collect{|s| self.new(s) }
end

.create(params = {}) ⇒ Object

Creates a new model with the given params. Converts a :media array into a hash to make the Rails app happy.

Site.primary.posts.create(:title => 'Awesome!', :media => [File.open('../some/path')])



86
87
88
89
90
91
92
93
# File 'lib/posterous/model.rb', line 86

def self.create params={}
  media_array = params.delete(:media)
  media       = Hash[media_array.each_with_index.map{|v,i| [i,v] }] unless media_array.nil?

  params = self.escape_hash(params)
  finder_opts.merge!(params)
  new post(parsed_resource_url, param_scope => params, :media => media)
end

.escape_hash(params = {}) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/posterous/model.rb', line 100

def self.escape_hash(params = {})
  escaped = {}
  params.each do |k,v|
    escaped[k] = CGI.escape(v) 
  end
  params = escaped
  return params
end

.find(mid) ⇒ Object

Get a model from a collection by its id.

Site.primary.posts.find(123) Site.find(123)



49
50
51
# File 'lib/posterous/model.rb', line 49

def self.find mid
  new get( parsed_resource_url + "/#{mid}")
end

.loadObject

loads the model data from the server and instantiates a new instance of its class

Site.primary.profile.load



57
58
59
# File 'lib/posterous/model.rb', line 57

def self.load
  new get(parsed_resource_url) rescue self.new(OpenStruct.new)
end

.many(collection_name, klass) ⇒ Object



8
9
10
11
12
# File 'lib/posterous/model.rb', line 8

def self.many collection_name, klass
  define_method collection_name do |*args|
    AssociationProxy.new self, klass, :many, *args
  end
end

.one(collection_name, klass) ⇒ Object



14
15
16
17
18
# File 'lib/posterous/model.rb', line 14

def self.one collection_name, klass
  define_method collection_name do |*args|
    AssociationProxy.new self, klass, :one, *args
  end
end

.param_scopeObject

Used to scope query params for a given model

Posterous::ExternalSite => external_site



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

def self.param_scope
  underscore(self.to_s.split('::').last).to_sym
end

.parsed_resource_urlObject



20
21
22
# File 'lib/posterous/model.rb', line 20

def self.parsed_resource_url
  resource_path.gsub(/:\w+/) {|sym| finder_opts[sym.sub(/:/,'').to_sym] }
end

.resource_url_keysObject



28
29
30
# File 'lib/posterous/model.rb', line 28

def self.resource_url_keys
  resource_path.scan(/:(\w+)/).flatten.collect(&:to_sym)
end

.underscore(camel_cased_word) ⇒ Object

lifted from ActiveSupport.



73
74
75
76
77
78
79
# File 'lib/posterous/model.rb', line 73

def self.underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

Instance Method Details

#changed_fieldsObject



127
128
129
# File 'lib/posterous/model.rb', line 127

def changed_fields
  @changed_fields ||= []
end

#destroyObject



115
116
117
# File 'lib/posterous/model.rb', line 115

def destroy
  self.class.delete(instance_url)
end

#hash_for_updateObject



131
132
133
# File 'lib/posterous/model.rb', line 131

def hash_for_update
  Hash[changed_fields.collect{ |f| [f, CGI.escape(self.send(f))] }]
end

#idObject

hack for ruby 1.8.7



33
34
35
# File 'lib/posterous/model.rb', line 33

def id
  self.struct.send(:table)[:id]
end

#inspectObject



147
148
149
# File 'lib/posterous/model.rb', line 147

def inspect
  "<#{self} #{struct.send(:table).inspect}>"
end

#instance_urlObject

url used for the update & delete actions



96
97
98
# File 'lib/posterous/model.rb', line 96

def instance_url
  "#{parsed_resource_url}/#{self.id}"
end

#param_scopeObject



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

def param_scope
  self.class.param_scope
end

#parsed_resource_urlObject



24
25
26
# File 'lib/posterous/model.rb', line 24

def parsed_resource_url
  self.class.parsed_resource_url
end

#reloadObject



119
120
121
# File 'lib/posterous/model.rb', line 119

def reload
  self.class.find(self.id)
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/posterous/model.rb', line 135

def respond_to? *args
  struct.respond_to?(*args) || super
end

#saveObject



109
110
111
112
113
# File 'lib/posterous/model.rb', line 109

def save
  return if hash_for_update.empty?
  @struct = self.class.post(instance_url, { param_scope => hash_for_update, '_method' => 'put' } )
  changed_fields.clear
end