Module: RubySerializer::Dsl

Included in:
Base
Defined in:
lib/ruby_serializer/dsl.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(field, options = {}) ⇒ Object



16
17
18
19
# File 'lib/ruby_serializer/dsl.rb', line 16

def belongs_to(field, options = {})
  fields << Field.new("#{field}_id", namespace, options)
  fields << Association.new(field, namespace, options)
end

#expose(field, options = {}) ⇒ Object



12
13
14
# File 'lib/ruby_serializer/dsl.rb', line 12

def expose(field, options = {})
  fields << Field.new(field, namespace, options)
end

#fieldsObject



37
38
39
# File 'lib/ruby_serializer/dsl.rb', line 37

def fields
  @fields ||= []
end

#has_many(field, options = {}) ⇒ Object



21
22
23
# File 'lib/ruby_serializer/dsl.rb', line 21

def has_many(field, options = {})
  fields << Association.new(field, namespace, options)
end

#has_one(field, options = {}) ⇒ Object



25
26
27
# File 'lib/ruby_serializer/dsl.rb', line 25

def has_one(field, options = {})
  fields << Association.new(field, namespace, options)
end

#namespace(ns = nil, &block) ⇒ Object



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

def namespace(ns = nil, &block)
  @namespace ||= []
  return @namespace if ns.nil?  # this method acts as both getter (this line) and setter (subsequent)
  @namespace.push(ns)
  block.call(binding)
  @namespace.pop
end

#serializes(name) ⇒ Object




6
7
8
9
10
# File 'lib/ruby_serializer/dsl.rb', line 6

def serializes(name)
  define_method(name) do   # provide a friendly-name accessor to the underlying resource
    resource
  end
end