Sha256: 98569c4cc24ed91d7619165189832240a2c256e7cd3d17a05c8161e391a32a4d

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

require 'querybuilder'

module Zena
  module Use
    class QueryLink

      module ModelMethods
        def self.included(base)
          base.send(:include, ::QueryBuilder)
          base.extend ClassMethods
          base.query_compiler = Zena::Use::QueryLink::Compiler
        end
      end

      module ClassMethods
        # Find a node and propagate visitor
        def do_find(count, query)
          case count
          when :all
            res = find_by_sql(query)
            res.empty? ? nil : res
          when :first
            find_by_sql(query).first
          when :count
            count_by_sql(query)
          else
            nil
          end
        end
      end

      
      class Compiler < ::QueryBuilder::Processor
        attr_reader :node_name
        set_main_table 'links'
        set_main_class 'Link'
        set_default :order,   'comment ASC'

        # Overwrite this and take car to check for valid fields.
        def process_field(field_name)
          if %w{comment status date name}.include?(field_name)
            field_name = 'comment' if field_name == 'name'
            "#{table}.#{field_name}"
          else
            super # raise an error
          end
        end

        def map_attr(fld)
          # error
          nil
        end
      end # Compiler
      
    end # QueryLink
  end # Use
end # Zena

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zena-1.2.8 lib/zena/use/query_link.rb
zena-1.2.7 lib/zena/use/query_link.rb
zena-1.2.6 lib/zena/use/query_link.rb
zena-1.2.5 lib/zena/use/query_link.rb
zena-1.2.4 lib/zena/use/query_link.rb
zena-1.2.3 lib/zena/use/query_link.rb