Class: Ivy::Serializers::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/ivy/serializers/mapping.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Mapping

Returns a new instance of Mapping.



7
8
9
10
11
# File 'lib/ivy/serializers/mapping.rb', line 7

def initialize(klass)
  @attributes = {}
  @relationships = {}
  @klass = klass
end

Instance Method Details

#attribute(name, &block) ⇒ Object



13
14
15
# File 'lib/ivy/serializers/mapping.rb', line 13

def attribute(name, &block)
  @attributes[name] = Attribute.new(name, &block)
end

#attributes(*names) ⇒ Object



17
18
19
# File 'lib/ivy/serializers/mapping.rb', line 17

def attributes(*names)
  names.each { |name| attribute(name) }
end

#belongs_to(name, options = {}, &block) ⇒ Object



21
22
23
# File 'lib/ivy/serializers/mapping.rb', line 21

def belongs_to(name, options={}, &block)
  @relationships[name] = Relationships::BelongsTo.new(name, options, &block)
end

#generate_attributes(generator, resource) ⇒ Object



25
26
27
# File 'lib/ivy/serializers/mapping.rb', line 25

def generate_attributes(generator, resource)
  @attributes.each_value { |attribute| attribute.generate(generator, resource) }
end

#has_many(name, options = {}, &block) ⇒ Object



29
30
31
# File 'lib/ivy/serializers/mapping.rb', line 29

def has_many(name, options={}, &block)
  @relationships[name] = Relationships::HasMany.new(name, options, &block)
end

#relationships(generator, resource) ⇒ Object



33
34
35
# File 'lib/ivy/serializers/mapping.rb', line 33

def relationships(generator, resource)
  @relationships.each_value { |relationship| relationship.generate(generator, resource) }
end

#resource(generator, resource) ⇒ Object



37
38
39
40
# File 'lib/ivy/serializers/mapping.rb', line 37

def resource(generator, resource)
  generator.attributes(resource) unless @attributes.empty?
  generator.relationships(resource) unless @relationships.empty?
end