Class: Jbuilder::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/jbuilder/serializer.rb,
lib/jbuilder/serializer/version.rb

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Serializer

Returns a new instance of Serializer.



62
63
64
# File 'lib/jbuilder/serializer.rb', line 62

def initialize(*args)
  renderer.instance_exec *args, &locals_block if locals_block
end

Class Method Details

.inherited(base) ⇒ Object



55
56
57
58
59
60
# File 'lib/jbuilder/serializer.rb', line 55

def self.inherited(base)
  base.class_eval do
    @locals_block = superclass.locals_block
    @template_path = superclass.template_path
  end
end

.locals(&block) ⇒ Object

Sets locals (instance variables) to be used within the jbuilder template.

Raises:

  • (ArgumentError)


44
45
46
47
48
# File 'lib/jbuilder/serializer.rb', line 44

def self.locals(&block)
  raise ArgumentError, 'no block given' unless block_given?

  self.locals_block = block
end

.set_template_path(path) ⇒ Object

Sets path to look for jbuilder templates.



51
52
53
# File 'lib/jbuilder/serializer.rb', line 51

def self.set_template_path(path)
  self.template_path = path
end

Instance Method Details

#template_nameObject

Jbuilder view name. By default, it has the same name as the serializer.



67
68
69
# File 'lib/jbuilder/serializer.rb', line 67

def template_name
  self.class.name.demodulize.underscore.gsub '_serializer', ''
end

#to_jsonObject

Returns resulting json as string.



72
73
74
# File 'lib/jbuilder/serializer.rb', line 72

def to_json
  renderer.render_to_string template: template_name, handler: :jbuilder
end