Class: Vines::Storage::Sql
- Inherits:
-
Vines::Storage
- Object
- Vines::Storage
- Vines::Storage::Sql
- Defined in:
- lib/vines/storage/sql.rb
Defined Under Namespace
Classes: Contact, Fragment, Group, User
Class Method Summary collapse
-
.with_connection(method, args = {}) ⇒ Object
Wrap the method with ActiveRecord connection pool logic, so we properly return connections to the pool when we’re finished with them.
Instance Method Summary collapse
-
#create_schema(args = {}) ⇒ Object
Create the tables and indexes used by this storage engine.
- #find_fragment(jid, node) ⇒ Object
- #find_user(jid) ⇒ Object
- #find_vcard(jid) ⇒ Object
-
#initialize(&block) ⇒ Sql
constructor
A new instance of Sql.
- #save_fragment(jid, node) ⇒ Object
- #save_user(user) ⇒ Object
- #save_vcard(jid, card) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Sql
Returns a new instance of Sql.
45 46 47 48 49 50 51 52 53 |
# File 'lib/vines/storage/sql.rb', line 45 def initialize(&block) @config = {} instance_eval(&block) required = [:adapter, :database] required << [:host, :port] unless @config[:adapter] == 'sqlite3' required.flatten.each {|key| raise "Must provide #{key}" unless @config[key] } [:username, :password].each {|key| @config.delete(key) if empty?(@config[key]) } establish_connection end |
Class Method Details
.with_connection(method, args = {}) ⇒ Object
Wrap the method with ActiveRecord connection pool logic, so we properly return connections to the pool when we’re finished with them. This also defers the original method by pushing it onto the EM thread pool because ActiveRecord uses blocking IO.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/vines/storage/sql.rb', line 24 def self.with_connection(method, args={}) deferrable = args.key?(:defer) ? args[:defer] : true old = instance_method(method) define_method method do |*args| ActiveRecord::Base.connection_pool.with_connection do old.bind(self).call(*args) end end defer(method) if deferrable end |
Instance Method Details
#create_schema(args = {}) ⇒ Object
Create the tables and indexes used by this storage engine.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/vines/storage/sql.rb', line 150 def create_schema(args={}) args[:force] ||= false ActiveRecord::Schema.define do create_table :users, force: args[:force] do |t| t.string :jid, limit: 512, null: false t.string :name, limit: 256, null: true t.string :password, limit: 256, null: true t.text :vcard, null: true end add_index :users, :jid, unique: true create_table :contacts, force: args[:force] do |t| t.integer :user_id, null: false t.string :jid, limit: 512, null: false t.string :name, limit: 256, null: true t.string :ask, limit: 128, null: true t.string :subscription, limit: 128, null: false end add_index :contacts, [:user_id, :jid], unique: true create_table :groups, force: args[:force] do |t| t.string :name, limit: 256, null: false end add_index :groups, :name, unique: true create_table :contacts_groups, id: false, force: args[:force] do |t| t.integer :contact_id, null: false t.integer :group_id, null: false end add_index :contacts_groups, [:contact_id, :group_id], unique: true create_table :fragments, force: args[:force] do |t| t.integer :user_id, null: false t.string :root, limit: 256, null: false t.string :namespace, limit: 256, null: false t.text :xml, null: false end add_index :fragments, [:user_id, :root, :namespace], unique: true end end |
#find_fragment(jid, node) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/vines/storage/sql.rb', line 128 def find_fragment(jid, node) jid = JID.new(jid)..to_s return if jid.empty? if fragment = fragment_by_jid(jid, node) Nokogiri::XML(fragment.xml).root rescue nil end end |
#find_user(jid) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/vines/storage/sql.rb', line 55 def find_user(jid) jid = JID.new(jid)..to_s return if jid.empty? xuser = user_by_jid(jid) return Vines::User.new(jid: jid).tap do |user| user.name, user.password = xuser.name, xuser.password xuser.contacts.each do |contact| groups = contact.groups.map {|group| group.name } user.roster << Vines::Contact.new( jid: contact.jid, name: contact.name, subscription: contact.subscription, ask: contact.ask, groups: groups) end end if xuser end |
#find_vcard(jid) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/vines/storage/sql.rb', line 110 def find_vcard(jid) jid = JID.new(jid)..to_s return if jid.empty? if xuser = user_by_jid(jid) Nokogiri::XML(xuser.vcard).root rescue nil end end |
#save_fragment(jid, node) ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/vines/storage/sql.rb', line 137 def save_fragment(jid, node) jid = JID.new(jid)..to_s fragment = fragment_by_jid(jid, node) || Sql::Fragment.new( user: user_by_jid(jid), root: node.name, namespace: node.namespace.href) fragment.xml = node.to_xml fragment.save end |
#save_user(user) ⇒ Object
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 99 100 101 102 103 104 105 106 107 |
# File 'lib/vines/storage/sql.rb', line 74 def save_user(user) xuser = user_by_jid(user.jid) || Sql::User.new(jid: user.jid..to_s) xuser.name = user.name xuser.password = user.password # remove deleted contacts from roster xuser.contacts.delete(xuser.contacts.select do |contact| !user.contact?(contact.jid) end) # update contacts xuser.contacts.each do |contact| fresh = user.contact(contact.jid) contact.update_attributes( name: fresh.name, ask: fresh.ask, subscription: fresh.subscription, groups: groups(fresh)) end # add new contacts to roster jids = xuser.contacts.map {|c| c.jid } user.roster.select {|contact| !jids.include?(contact.jid..to_s) } .each do |contact| xuser.contacts.build( user: xuser, jid: contact.jid..to_s, name: contact.name, ask: contact.ask, subscription: contact.subscription, groups: groups(contact)) end xuser.save end |
#save_vcard(jid, card) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/vines/storage/sql.rb', line 119 def save_vcard(jid, card) xuser = user_by_jid(jid) if xuser xuser.vcard = card.to_xml xuser.save end end |