Class: Rabl::Builder

Inherits:
Object
  • Object
show all
Includes:
Helpers, Partials
Defined in:
lib/rabl/builder.rb

Constant Summary collapse

SETTING_TYPES =
{
  :attributes => :name,
  :node       => :name,
  :child      => :data,
  :glue       => :data,
  :extends    => :file
}

Instance Method Summary collapse

Methods included from Partials

#partial_as_engine

Methods included from Sources

#fetch_source

Methods included from Helpers

#collection_root_name, #context_scope, #data_name, #data_object, #data_object_attribute, #determine_object_root, #fetch_result_from_cache, #is_collection?, #is_name_value?, #is_object?, #object_root_name, #object_to_engine, #template_cache_configured?, #view_path, #write_result_to_cache

Constructor Details

#initialize(object, settings = {}, options = {}) ⇒ Builder

Constructs a new rabl hash based on given object and options options = { :format => “json”, :root => true, :child_root => true,

:attributes, :node, :child, :glue, :extends }


20
21
22
23
24
25
26
27
# File 'lib/rabl/builder.rb', line 20

def initialize(object, settings = {}, options = {})
  @_object = object

  @settings       = settings
  @options        = options
  @_context_scope = options[:scope]
  @_view_path     = options[:view_path]
end

Instance Method Details

#enginesObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rabl/builder.rb', line 29

def engines
  return @_engines if defined?(@_engines)

  @_engines = []

  # Append onto @_engines
  compile_settings(:extends)
  compile_settings(:child)
  compile_settings(:glue)

  @_engines
end

#replace_engine(engine, value) ⇒ Object



42
43
44
# File 'lib/rabl/builder.rb', line 42

def replace_engine(engine, value)
  engines[engines.index(engine)] = value
end

#to_hash(object = nil, settings = nil, options = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rabl/builder.rb', line 46

def to_hash(object = nil, settings = nil, options = nil)
  @_object = object           if object
  @options.merge!(options)    if options
  @settings.merge!(settings)  if settings

  cache_results do
    @_result = {}

    # Merges directly into @_result
    compile_settings(:attributes)

    merge_engines_into_result

    # Merges directly into @_result
    compile_settings(:node)

    replace_nil_values          if Rabl.configuration.replace_nil_values_with_empty_strings
    replace_empty_string_values if Rabl.configuration.replace_empty_string_values_with_nil_values
    remove_nil_values           if Rabl.configuration.exclude_nil_values

    result = @_result
    result = { @options[:root_name] => result } if @options[:root_name].present?
    result
  end
end