Class: DataMapper::Reflection::Builders::Source::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-reflection/builders/source_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#modelObject

Returns the value of attribute model.



12
13
14
# File 'lib/dm-reflection/builders/source_builder.rb', line 12

def model
  @model
end

Class Method Details

.to_ruby(model) ⇒ Object

Raises:

  • (DataMapper::IncompleteModelError)


14
15
16
17
18
# File 'lib/dm-reflection/builders/source_builder.rb', line 14

def self.to_ruby(model)
  builder = new(model)
  raise DataMapper::IncompleteModelError unless builder.complete_model?
  builder.to_ruby
end

Instance Method Details

#complete_model?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/dm-reflection/builders/source_builder.rb', line 93

def complete_model?
  key_properties.any?
end

#foreign_key_propertiesObject



58
59
60
# File 'lib/dm-reflection/builders/source_builder.rb', line 58

def foreign_key_properties
  []
end

#key_propertiesObject



54
55
56
# File 'lib/dm-reflection/builders/source_builder.rb', line 54

def key_properties
  model.key
end

#many_to_many_relationships(repository = :default) ⇒ Object



86
87
88
89
90
# File 'lib/dm-reflection/builders/source_builder.rb', line 86

def many_to_many_relationships(repository = :default)
  model.relationships(repository).select do |_, relationship|
    relationship.is_a?(DataMapper::Associations::ManyToMany::Relationship)
  end
end

#many_to_one_relationships(repository = :default) ⇒ Object



67
68
69
70
71
# File 'lib/dm-reflection/builders/source_builder.rb', line 67

def many_to_one_relationships(repository = :default)
  model.relationships(repository).select do |_, relationship|
    relationship.is_a?(DataMapper::Associations::ManyToOne::Relationship)
  end
end

#one_to_many_relationships(repository = :default) ⇒ Object



79
80
81
82
83
84
# File 'lib/dm-reflection/builders/source_builder.rb', line 79

def one_to_many_relationships(repository = :default)
  model.relationships(repository).select do |_, relationship|
    relationship.is_a?(DataMapper::Associations::OneToMany::Relationship) &&
    !relationship.is_a?(DataMapper::Associations::ManyToMany::Relationship)
  end
end

#one_to_one_relationships(repository = :default) ⇒ Object



73
74
75
76
77
# File 'lib/dm-reflection/builders/source_builder.rb', line 73

def one_to_one_relationships(repository = :default)
  model.relationships(repository).select do |_, relationship|
    relationship.is_a?(DataMapper::Associations::OneToOne::Relationship)
  end
end

#regular_propertiesObject



62
63
64
# File 'lib/dm-reflection/builders/source_builder.rb', line 62

def regular_properties
  model.properties - model.key - foreign_key_properties
end

#to_rubyObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dm-reflection/builders/source_builder.rb', line 21

def to_ruby
  ruby  = "class #{model}\n"
  ruby += "\n"
  ruby += "  include DataMapper::Resource\n"
  property_definitions(key_properties).each do |key_definition|
    ruby += "\n  #{key_definition}"
  end
  ruby += "\n"
  property_definitions(foreign_key_properties).each do |foreign_key_definition|
    ruby += "\n  #{foreign_key_definition}"
  end
  property_definitions(regular_properties).each do |property_definition|
    ruby += "\n  #{property_definition}"
  end
  ruby += "\n"
  relationship_definitions(many_to_one_relationships).each do |relationship_definition|
    ruby += "\n  #{relationship_definition}"
  end
  relationship_definitions(one_to_one_relationships).each do |relationship_definition|
    ruby += "\n  #{relationship_definition}"
  end
  relationship_definitions(one_to_many_relationships).each do |relationship_definition|
    ruby += "\n  #{relationship_definition}"
  end
  relationship_definitions(many_to_many_relationships).each do |relationship_definition|
    ruby += "\n  #{relationship_definition}"
  end
  ruby += "\n"
  ruby += "\nend\n"
end