class Smartdict::Log
Public Class Methods
fetch(options = {})
click to toggle source
Options:
-
:since
-
:till
-
:from_lang
-
:to_lang
-
:driver
-
:limit
-
:order_desc
-
:unique
@return [Array] array of {Smartdict::Translation}.
# File lib/smartdict/log.rb, line 15 def self.fetch(options = {}) new(options).send(:fetch) end
new(options)
click to toggle source
# File lib/smartdict/log.rb, line 22 def initialize(options) @options = options end
Private Instance Methods
driver()
click to toggle source
# File lib/smartdict/log.rb, line 104 def driver Driver[@options[:driver]] end
fetch()
click to toggle source
# File lib/smartdict/log.rb, line 26 def fetch fetch_translations.map(&:to_struct) end
fetch_translations()
click to toggle source
Unfortunately it’s not possible to build necessary query with DataMapper TODO: Refactor in QueryBuilder class.
# File lib/smartdict/log.rb, line 34 def fetch_translations adapter = Translation.repository.adapter translations_table = Translation.storage_name queries_table = TranslationQuery.storage_name sql = "SELECT #{'DISTINCT' if @options[:unique]} #{translations_table}.id \n" \ "FROM #{translations_table} \n" \ "INNER JOIN #{queries_table} ON \n" \ " #{queries_table}.translation_id == #{translations_table}.id" if [:till, :since, :from_lang, :to_lang, :driver].any? {|opt| @options[opt] } sql << " WHERE " if @options[:till] till = @options[:till].is_a?(String) ? DateTime.parse(@options[:till]) : @options[:till] sql << " #{queries_table}.created_at < \"#{till.to_s(:db)}\" " where = true end if @options[:since] sql << " AND " if where since = @options[:since].is_a?(String) ? DateTime.parse(@options[:since]) : @options[:since] sql << " #{queries_table}.created_at > \"#{since.to_s(:db)}\" " where = true end if from_lang sql << " AND " if where sql << " #{translations_table}.from_lang_id = #{from_lang.id} " where = true end if to_lang sql << " AND " if where sql << " #{translations_table}.to_lang_id = #{to_lang.id} " where = true end if driver sql << " AND " if where sql << " #{translations_table}.driver_id = #{driver.id} " where = true end end if @options[:order_desc] sql << " ORDER BY #{queries_table}.created_at DESC " else sql << " ORDER BY #{queries_table}.created_at " end if @options[:limit] sql << " LIMIT #{@options[:limit]} " end ids = adapter.select(sql) trs = Translation.all(:id => ids) # Sort manually, since database doesn't guaranty required order trs.sort! { |a, b| ids.index(a.id) <=> ids.index(b.id) } end
from_lang()
click to toggle source
# File lib/smartdict/log.rb, line 96 def from_lang Language[@options[:from_lang]] end
to_lang()
click to toggle source
# File lib/smartdict/log.rb, line 100 def to_lang Language[@options[:to_lang]] end