Class: DataMapper::Adapters::ImapAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::ImapAdapter
- Defined in:
- lib/dm-imap-adapter.rb
Instance Method Summary collapse
- #create(resources) ⇒ Object
- #delete(collection) ⇒ Object
- #read(query) ⇒ Object
-
#update(attributes, collection) ⇒ Object
read.
Instance Method Details
#create(resources) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dm-imap-adapter.rb', line 23 def create(resources) resources.map do |resource| with_connection(resource.model) do |connection| last_email = connection.uid_fetch(-1, "UID") if last_email next_uid = last_email.first.attr[:UID].to_i else next_uid = 1 end initialize_serial(resource, next_uid) # email_text = <<EOT.gsub(/\n/, "\r\n") # Subject: #{resource.subject} # From: #{resource.from} # To: #{resource.to} # # #{resource.body} # EOT # email_text = "" email_text << "Subject: #{resource.subject}\n" if resource.respond_to?(:subject) email_text << "From: #{resource.from}\n" if resource.respond_to?(:from) email_text << "To: #{resource.to}\n" if resource.respond_to?(:to) email_text << "\n#{resource.body}" if resource.respond_to?(:body) email_text = email_text.gsub(/\n/, "\r\n") if resource.respond_to?(:date) date = resource.date else date = Time.now end if resource.respond_to?(:flags) flags = resources.flags else flags = [] end connection.append(@options[:path].gsub(%r{^/}, ""), email_text, flags, date) end end.size end |
#delete(collection) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/dm-imap-adapter.rb', line 104 def delete(collection) with_connection(collection.query.model) do |connection| connection.uid_store(collection.collect { |record| record.uid }, "+FLAGS", [:Deleted]) connection.expunge end end |
#read(query) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dm-imap-adapter.rb', line 71 def read(query) with_connection(query.model) do |connection| attr_props = query.model.properties.select { |prop| prop.type.attr? } attrs = attr_props.collect { |prop| prop.type.attr_name }.compact.uniq # TODO: don't do 1..-1, but instead do a search and then fetch the uids returned uids = 1..-1 # if query.conditions # query_array = [] # debugger # query.conditions.each do |op, property, value| # query_array += (property.type.query_details[op] + [value]) # end # uids = connection.uid_search(query_array) # else # uids = 1..-1 # end mails = connection.uid_fetch(uids, attrs) || [] results = materialize_records_for(query.model, mails) query.filter_records(results.dup) # results.dup end#with_connection end |
#update(attributes, collection) ⇒ Object
read
100 101 102 |
# File 'lib/dm-imap-adapter.rb', line 100 def update(attributes, collection) raise NotImplemented end |