Module: MetaModel::Metafile::DSL

Included in:
MetaModel::Metafile
Defined in:
lib/metamodel/metafile/dsl.rb

Instance Method Summary collapse

Instance Method Details

#attr(key, type = :string, **args) ⇒ Object



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

def attr(key, type = :string, **args)
  @current_model.properties << Record::Property.new(key, type, args)
end

#belongs_to(name, model_name = nil, **args) ⇒ Object



37
38
39
40
41
# File 'lib/metamodel/metafile/dsl.rb', line 37

def belongs_to(name, model_name = nil, **args)
  model_name = name.to_s.singularize.camelize if model_name.nil?
  association = Record::Association.new(name, current_model.name, model_name, :belongs_to, args)
  @associations << association
end

#define(model_name) ⇒ Object



13
14
15
16
17
18
# File 'lib/metamodel/metafile/dsl.rb', line 13

def define(model_name)
  UI.message '-> '.green + "Resolving `#{model_name.to_s.camelize}`"
  @current_model = Record::Model.new(model_name)
  yield if block_given?
  @models << @current_model
end

#has_many(name, model_name = nil, **args) ⇒ Object

Raises:



30
31
32
33
34
35
# File 'lib/metamodel/metafile/dsl.rb', line 30

def has_many(name, model_name = nil, **args)
  model_name = name.to_s.singularize.camelize if model_name.nil?
  raise Informative, "has_many relation can't be created with optional model name" if model_name.end_with? "?"
  association = Record::Association.new(name, current_model.name, model_name, :has_many, args)
  @associations << association
end

#has_one(name, model_name = nil, **args) ⇒ Object



24
25
26
27
28
# File 'lib/metamodel/metafile/dsl.rb', line 24

def has_one(name, model_name = nil, **args)
  model_name = name.to_s.singularize.camelize if model_name.nil?
  association = Record::Association.new(name, current_model.name, model_name, :has_one, args)
  @associations << association
end

#metamodel_version(version) ⇒ Object

Raises:



8
9
10
11
# File 'lib/metamodel/metafile/dsl.rb', line 8

def metamodel_version(version)
  raise Informative,
    "Meta file #{version} not matched with current metamodel version #{VERSION}" if version != VERSION
end