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
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #field(name, type, *args, **options) ⇒ Object
-
#initialize(model, **options) ⇒ Dsl
constructor
A new instance of Dsl.
-
#method_missing(*args, **options) ⇒ Object
TODO: make [:required] just another option.
- #optimistic_lock ⇒ Object
- #timestamps(**options) ⇒ Object
Constructor Details
#initialize(model, **options) ⇒ Dsl
Returns a new instance of Dsl.
13 14 15 16 |
# File 'lib/declare_schema/dsl.rb', line 13 def initialize(model, **) @model = model @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’?
34 35 36 37 38 |
# File 'lib/declare_schema/dsl.rb', line 34 def method_missing(*args, **) args.count(&:itself) >= 2 or raise ::ArgumentError, "fields in declare_schema block must be declared as: type name, [:required], options (got #{args.inspect}, #{.inspect})" type, name, *required = args field(name, type, *required, **) end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
18 19 20 |
# File 'lib/declare_schema/dsl.rb', line 18 def model @model end |
Instance Method Details
#field(name, type, *args, **options) ⇒ Object
29 30 31 |
# File 'lib/declare_schema/dsl.rb', line 29 def field(name, type, *args, **) @model.declare_field(name, type, *args, **@options.merge()) end |
#optimistic_lock ⇒ Object
25 26 27 |
# File 'lib/declare_schema/dsl.rb', line 25 def optimistic_lock field(:lock_version, :integer, default: 1, null: false) end |
#timestamps(**options) ⇒ Object
20 21 22 23 |
# File 'lib/declare_schema/dsl.rb', line 20 def (**) field(:created_at, :datetime, null: false, **) field(:updated_at, :datetime, null: false, **) end |