Sha256: 1c5707731e04c203805d529ba3cb7368ac3a424ad798cd495340c7cf132d8074

Contents?: true

Size: 952 Bytes

Versions: 8

Compression:

Stored size: 952 Bytes

Contents

# frozen_string_literal: true

require 'active_support/proxy_object'

module DeclareSchema
  class Dsl < BasicObject # avoid Object because that gets extended by lots of gems
    include ::Kernel      # but we need the basic class methods

    instance_methods.each do |m|
      unless m.to_s.starts_with?('__') || m.in?([:object_id, :instance_eval])
        undef_method(m)
      end
    end

    def initialize(model, options = {})
      @model = model
      @options = options
    end

    attr_reader :model

    def timestamps
      field(:created_at, :datetime, null: true)
      field(:updated_at, :datetime, null: true)
    end

    def optimistic_lock
      field(:lock_version, :integer, default: 1, null: false)
    end

    def field(name, type, *args, **options)
      @model.declare_field(name, type, *(args + [@options.merge(options)]))
    end

    def method_missing(type, name, *args)
      field(name, type, *args)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
declare_schema-0.12.1 lib/declare_schema/dsl.rb
declare_schema-0.12.0 lib/declare_schema/dsl.rb
declare_schema-0.12.0.pre.2 lib/declare_schema/dsl.rb
declare_schema-0.12.0.pre.1 lib/declare_schema/dsl.rb
declare_schema-0.11.1 lib/declare_schema/dsl.rb
declare_schema-0.11.0 lib/declare_schema/dsl.rb
declare_schema-0.10.1 lib/declare_schema/dsl.rb
declare_schema-0.10.0 lib/declare_schema/dsl.rb