Sha256: 35c07fb32a9283a7da23d3d28fcfa1faaf5222bd361f5ed52274cd2cbf5d87c0

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "findable/schema/conversion"

module Findable
  module Schema
    extend ActiveSupport::Concern

    included do
      include ActiveModel::AttributeMethods
      include ActiveModel::Dirty

      attribute_method_suffix "="
      attribute_method_suffix "?"
    end

    module ClassMethods
      def column_names
        @_column_names ||= [:id]
      end

      def indexes
        @_indexes ||= [:id]
      end

      def index_defined?
        indexes.size > 1
      end

      def define_field(*args)
        options = args.extract_options!
        name = args.first
        if !public_method_defined?(name) || options.present?
          define_attribute_methods name
          conversion = Findable::Schema::Conversion.to(options[:type])
          define_method(name) { conversion.call(attributes[name.to_sym]) }
          indexes << name.to_sym if options[:index]
          column_names << name.to_sym
        end
      end
    end

    def attribute=(attr, value)
      public_send("#{attr}_will_change!")
      attributes[attr] = value
    end

    def attribute?(attr)
      attributes[attr].present?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
findable-0.2.2 lib/findable/schema.rb