Sha256: 2d0c5a6f71f73cd3a8f3e635a7ad6c1460701f5e569cbc753fa3b6db2a2da3be

Contents?: true

Size: 827 Bytes

Versions: 1

Compression:

Stored size: 827 Bytes

Contents

module Season
  class ScopeBuilder

    def initialize(klass)
      @klass = klass
      @query_builder = QueryBuilder.new(adapter_class_name)
    end

    def build(table_name, column_name, query_verb)
      unless method_exists?(column_name)
        query_str = @query_builder.build(table_name, column_name, query_verb)

        @klass.instance_eval %Q{
          def #{column_name}_#{query_verb}(*args)
            #{query_str}
          end
        }
      end
    end

    private

    def method_exists?(column_name)
      defined? @klass.column_name
    end

    def adapter_class_name
      return 'active_record' if @klass < ActiveRecord::Base
      raise "Database adapter not supported."
    end

    # def remove_old_suffix(text)
    #   text.sub(/(#{POPULAR_SUFFIXES.join('|')})$/, '') || text
    # end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
season-0.3 lib/season/scope_builder.rb