Module: RelationBuilder::ClassMethods

Defined in:
lib/redisant/relation_builder.rb

Instance Method Summary collapse

Instance Method Details

#add_relation_definition(name, klass) ⇒ Object



29
30
31
32
# File 'lib/redisant/relation_builder.rb', line 29

def add_relation_definition name, klass
  raise Redisant::InvalidArgument.new("Relation #{name} already exists") if relation_definitions[name.to_s]
  relation_definitions[name.to_s] = klass
end

#belongs_to(name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redisant/relation_builder.rb', line 41

def belongs_to name
  add_relation_definition name, BelongsTo
  send :define_method, name do
    relation = find_relation(name) { BelongsTo.new(name, self) }
    relation.owner
  end
  send :define_method, "#{name}=" do |item, reprocitate=true|
    relation = find_relation(name) { BelongsTo.new( name, self ) }
    relation.set_owner item, reprocitate
  end

end

#has_many(name) ⇒ Object



34
35
36
37
38
39
# File 'lib/redisant/relation_builder.rb', line 34

def has_many name
  add_relation_definition name, HasMany
  send :define_method, name do
    relation = find_relation(name) { HasMany.new(name, self) }
  end
end

#relation_definitionsObject



25
26
27
# File 'lib/redisant/relation_builder.rb', line 25

def relation_definitions
  @relation_definitions ||= {}
end