From 59af2144dacfe6381c4f8cb5b84101ada0a8dc15 Mon Sep 17 00:00:00 2001
From: Mateus Rambo Strey <mars11@inf.ufpr.br>
Date: Mon, 9 Nov 2015 10:47:57 -0200
Subject: [PATCH] add institution builder

---
 app/builders/institution_builder.rb | 48 +++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100644 app/builders/institution_builder.rb

diff --git a/app/builders/institution_builder.rb b/app/builders/institution_builder.rb
new file mode 100644
index 00000000..294c4ffd
--- /dev/null
+++ b/app/builders/institution_builder.rb
@@ -0,0 +1,48 @@
+class InstitutionBuilder
+  extend RepositoriesProxy
+
+  ##
+  # receive a list of ids and return a list of institutions
+  #
+  def self.build(institutions = [])
+    array = []
+    institutions = [institutions] if institutions.class == String
+    institutions.each do |institution|
+      unless institution['@rid'].blank?
+        o = Rails.cache.fetch(cache_key(institution['@rid'], institution['last_modified'])) unless institution['last_modified'].blank?
+
+        o = institution_repository.find institution['@rid'] if o.nil?
+
+        array << o
+      end
+    end
+    array
+  end
+
+  def self.build_from_orientdb(args = {})
+    institution = nil
+
+    unless args.nil?
+      # cache object when build
+      institution = Rails.cache.fetch(cache_key(args['@rid'], args['last_modified']), expires_in: 12.hours) do
+        Institution.new(
+          id: args['@rid'],
+          name: args['name'],
+          created_at: args['created_at'],
+          last_modified: args['last_modified'],
+          country: args['country'] || '',
+          city: args['city'] || '',
+          description: args['description'] || '',
+          thumbnail: args['thumbnail'] || '',
+          address: args['address'] || ''
+        )
+      end
+    end
+
+    institution
+  end
+
+  def self.cache_key(rid, last_modified)
+    rid + '/' + last_modified
+  end
+end
-- 
GitLab