Class: ModalFields::DeclarationsDsl

Inherits:
DslBase
  • Object
show all
Defined in:
lib/modalfields/modalfields.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ DeclarationsDsl

Returns a new instance of DeclarationsDsl.



92
93
94
# File 'lib/modalfields/modalfields.rb', line 92

def initialize(model)
  @model = model
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, type, *args) ⇒ Object



110
111
112
# File 'lib/modalfields/modalfields.rb', line 110

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

Instance Method Details

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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/modalfields/modalfields.rb', line 95

def field(name, type, *args)
  declaration = FieldDeclaration.declare(name, type, *args)
  specific_hook = ModalFields.hooks[type.to_sym]
  general_hook = ModalFields.hooks[:all_fields]
  [specific_hook, general_hook].compact.each do |hook|
    hook[@model, declaration] if hook
  end
  if ModalFields.validate(declaration)
    @model.fields_info << declaration
  end
end

#timestampsObject



106
107
108
109
# File 'lib/modalfields/modalfields.rb', line 106

def timestamps
  field :created_at, :datetime
  field :updated_at, :datetime
end