Class: DeclareSchema::Dsl

Inherits:
BasicObject
Includes:
Kernel
Defined in:
lib/declare_schema/dsl.rb

Overview

avoid Object because that gets extended by lots of gems

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, **options) ⇒ Dsl

Returns a new instance of Dsl.



15
16
17
18
# File 'lib/declare_schema/dsl.rb', line 15

def initialize(model, **options)
  @model = model
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, **options) ⇒ Object

TODO: make [:required] just another option. Either ‘required: true] or ’optional: false’?



36
37
38
39
40
# File 'lib/declare_schema/dsl.rb', line 36

def method_missing(*args, **options)
  args.count(&:itself) >= 2 or raise ::ArgumentError, "fields in declare_schema block must be declared as: type name, [:required], options (got #{args.inspect}, #{options.inspect})"
  type, name, *required = args
  field(name, type, *required, **options)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



20
21
22
# File 'lib/declare_schema/dsl.rb', line 20

def model
  @model
end

Instance Method Details

#field(name, type, *args, **options) ⇒ Object



31
32
33
# File 'lib/declare_schema/dsl.rb', line 31

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

#optimistic_lockObject



27
28
29
# File 'lib/declare_schema/dsl.rb', line 27

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

#timestampsObject



22
23
24
25
# File 'lib/declare_schema/dsl.rb', line 22

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