Class: JSONBuilder::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/json_builder/compiler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compiler

Public: Creates a new Compiler instance used to hold any and all JSONBuilder::Member objects.

options - Hash of options used to modify JSON output.

Examples

json = JSONBuilder::Compiler.new(:callback => false)
json.compile do
  name 'Garrett'
end
json.finalize
# => {"name": "Garrett"}

Returns instance of JSONBuilder::Compiler.



46
47
48
49
50
51
52
53
54
# File 'lib/json_builder/compiler.rb', line 46

def initialize(options={})
  @_members = []
  @_scope = options.fetch(:scope, nil)
  @_callback = options.fetch(:callback, true)
  @_pretty_print = options.fetch(:pretty, false)

  # Only copy instance variables if there is a scope and presence of Rails
  copy_instance_variables_from(@_scope) if @_scope
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key_name, *args, &block) ⇒ Object

Public: Called anytime the compiler is passed JSON keys, first checks to see if the parent object contains the method like a Rails helper.

key_name - The key for the JSON member. args - An array of values passed to JSONBuilder::Value. block - Yielding any block passed to the element.

Returns nothing.



83
84
85
86
87
88
89
# File 'lib/json_builder/compiler.rb', line 83

def method_missing(key_name, *args, &block)
  if @_scope.respond_to?(key_name) && !ignore_scope_methods.include?(key_name)
    @_scope.send(key_name, *args, &block)
  else
    key(key_name, *args, &block)
  end
end

Instance Attribute Details

#array(items, &block) ⇒ Object

Public: Takes a set number of items to generate a plain JSON array response.

Returns instance of JSONBuilder::Elements.



70
71
72
# File 'lib/json_builder/compiler.rb', line 70

def array
  @array
end

#callbackObject

Returns the value of attribute callback.



25
26
27
# File 'lib/json_builder/compiler.rb', line 25

def callback
  @callback
end

#membersObject

Returns the value of attribute members.



22
23
24
# File 'lib/json_builder/compiler.rb', line 22

def members
  @members
end

#pretty_print=(value) ⇒ Object

Sets the attribute pretty_print

Parameters:

  • value

    the value to set the attribute pretty_print to.



26
27
28
# File 'lib/json_builder/compiler.rb', line 26

def pretty_print=(value)
  @pretty_print = value
end

#scopeObject

Returns the value of attribute scope.



24
25
26
# File 'lib/json_builder/compiler.rb', line 24

def scope
  @scope
end

Class Method Details

.generate(*args, &block) ⇒ Object

Public: The helper that builds the JSON structure by calling the specific methods needed to build the JSON.

args - Any number of arguments needed for the JSONBuilder::Compiler. block - Yielding a block to generate the JSON.

Returns a String.



14
15
16
17
18
19
# File 'lib/json_builder/compiler.rb', line 14

def generate(*args, &block)
  options = args.extract_options!
  compiler = self.new(options)
  compiler.compile(*args, &block)
  compiler.finalize
end

Instance Method Details

#compile(*args, &block) ⇒ Object

Public: Takes a block to generate the JSON structure by calling method_missing on all members passed to it through the block.

args - An array of values passed to JSONBuilder::Value. block - Yielding a block to generate the JSON.

Returns nothing.



63
64
65
# File 'lib/json_builder/compiler.rb', line 63

def compile(*args, &block)
  instance_exec(*args, &block)
end

#finalizeObject

Public: Combines the output of the compiled members and the change there is a JSONP callback. This is what is returned in the response.

Returns a String.



117
118
119
# File 'lib/json_builder/compiler.rb', line 117

def finalize
  include_callback to_s
end

#key(key_name, *args, &block) ⇒ Object

Public: Generates the start of the JSON member. Useful if the key you are generating is dynamic.

key - Used to generate the JSON member’s key. Can be a String or Symbol. args - An array of values passed to JSONBuilder::Value. block - Yielding any block passed to the element.

Examples

key :hello, 'Hi'
# => "hello": "Hi"

key "item-#{rand(0, 500)}", "I'm random!"
# => "item-250": "I'm random!"

Returns instance of JSONBuilder::Member.



107
108
109
110
111
# File 'lib/json_builder/compiler.rb', line 107

def key(key_name, *args, &block)
  member = Member.new(key_name, @_scope, *args, &block)
  @_members << member
  member
end

#to_sObject

Public: Gathers the JSON structure and calls it’s compiler within each instance.

Returns a String.



125
126
127
# File 'lib/json_builder/compiler.rb', line 125

def to_s
  @_array ? @_array.to_s : "{#{@_members.collect(&:to_s).join(', ')}}"
end