Sha256: 10ba7856d53dea50d1af1e9656ac603657af8ade38df57e9fae3b757e3d94c65

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require 'active_support/concern'
require 'active_support/dependencies/autoload'
require 'securerandom'

module MemoryModel
  class Base
    module Fields

      extend ConcernedInheritance
      extend ActiveSupport::Concern
      extend ActiveSupport::Autoload
      include ActiveModel::AttributeMethods

      autoload :FieldSet
      autoload :Field

      inherited do
        instance_variable_set :@fields, baseclass.fields
      end

      module ClassMethods
        def field(attr, options={})
          define_attribute_method attr unless instance_method_already_implemented? attr
          fields.add(attr.to_sym, options)
          class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{attr}
              read_attribute :#{attr}
            end

            def #{attr}=(value)
              write_attribute :#{attr}, value
            end
          RUBY
        end

        def fields
          return nil if self == MemoryModel::Base
          @fields ||= FieldSet.new
        end

      end

      def fields
        self.class.fields
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
memory_model-1.0.0 lib/memory_model/base/fields.rb
memory_model-0.1.0 lib/memory_model/base/fields.rb