Class: MetaModel::Metafile

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/metamodel/metafile.rb,
lib/metamodel/metafile/dsl.rb

Defined Under Namespace

Modules: DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#attr, #belongs_to, #define, #has_many, #has_one, #metamodel_version

Constructor Details

#initialize(defined_in_file = nil, internal_hash = {}) ⇒ Metafile

Returns a new instance of Metafile.



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

def initialize(defined_in_file = nil, internal_hash = {})
  @defined_in_file = defined_in_file
  @models = []
  @associations = []

  evaluate_model_definition(defined_in_file)
  amend_association
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



11
12
13
# File 'lib/metamodel/metafile.rb', line 11

def associations
  @associations
end

#current_modelObject

Returns the value of attribute current_model.



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

def current_model
  @current_model
end

#defined_in_fileObject

Returns the value of attribute defined_in_file.



7
8
9
# File 'lib/metamodel/metafile.rb', line 7

def defined_in_file
  @defined_in_file
end

#modelsObject

Returns the value of attribute models.



10
11
12
# File 'lib/metamodel/metafile.rb', line 10

def models
  @models
end

Class Method Details

.from_file(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/metamodel/metafile.rb', line 34

def self.from_file(path)
  path = Pathname.new(path)
  unless path.exist?
    raise Informative, "No Metafile exists at path `#{path}`."
  end

  case path.extname
  when '', '.metafile'
    Metafile.new(path)
  else
    raise Informative, "Unsupported Metafile format `#{path}`."
  end
end

Instance Method Details

#amend_associationObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/metamodel/metafile.rb', line 48

def amend_association
  name_model_hash = Hash[@models.collect { |model| [model.name, model] }]
  @associations.map! do |association|
    major_model = name_model_hash[association.major_model]
    major_model.associations << association
    association.major_model = major_model
    association.secondary_model = name_model_hash[association.secondary_model]
    raise Informative, "Associations not satisfied in `Metafile`" unless [association.major_model, association.secondary_model].compact.size == 2
    association
  end
  self
end

#evaluate_model_definition(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/metamodel/metafile.rb', line 22

def evaluate_model_definition(path)
  UI.section "Analyzing Metafile" do
    contents ||= File.open(path, 'r:utf-8', &:read)

    if contents.respond_to?(:encoding) && contents.encoding.name != 'UTF-8'
      contents.encode!('UTF-8')
    end

    eval(contents, nil, path.to_s)
  end
end