Class: Gon

Inherits:
Object
  • Object
show all
Defined in:
lib/gon.rb,
lib/gon/base.rb,
lib/gon/rabl.rb,
lib/gon/watch.rb,
lib/gon/global.rb,
lib/gon/escaper.rb,
lib/gon/helpers.rb,
lib/gon/request.rb,
lib/gon/version.rb,
lib/gon/jbuilder.rb,
lib/gon/env_finder.rb,
lib/gon/json_dumper.rb,
lib/gon/spec_helpers.rb,
lib/gon/jbuilder/parser.rb,
lib/gon/compatibility/old_rails.rb

Direct Known Subclasses

Global, Watch

Defined Under Namespace

Modules: Base, ControllerHelpers, EnvFinder, Escaper, Jbuilder, JsonDumper, Rabl, SpecHelper, ViewHelpers Classes: Global, Request, Watch

Constant Summary collapse

VERSION =
'6.4.0'

Class Method Summary collapse

Class Method Details

.all_variablesObject



81
82
83
# File 'lib/gon.rb', line 81

def all_variables
  current_gon ? current_gon.gon : {}
end

.clearObject



85
86
87
# File 'lib/gon.rb', line 85

def clear
  current_gon.clear if current_gon
end

.get_variable(name) ⇒ Object



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

def get_variable(name)
  current_gon.gon[name]
end

.globalObject



28
29
30
# File 'lib/gon.rb', line 28

def global
  Gon::Global
end

.inspectObject



100
101
102
# File 'lib/gon.rb', line 100

def inspect
  'Gon'
end

.jbuilder(*args) ⇒ Object



94
95
96
97
98
# File 'lib/gon.rb', line 94

def jbuilder(*args)
  ensure_template_handler_is_defined
  data, options = Gon::Jbuilder.handler(args)
  store_builder_data 'jbuilder', data, options
end

.merge_variable(name, value) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/gon.rb', line 59

def merge_variable(name, value)
  old_value = all_variables[name]
  if value.is_a?(Hash) && old_value.is_a?(Hash)
    value = old_value.deep_merge(value)
  end
  set_variable(name, value)
end

.method_missing(method, *args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gon.rb', line 36

def method_missing(method, *args, &block)
  if method.to_s =~ /=$/
    if public_method_name?(method)
      raise "You can't use Gon public methods for storing data: #{method}"
    end
    if self == Gon && !current_gon
      raise 'Assign request-specific gon variables only through `gon` helper, not through Gon constant'
    end

    set_variable(method.to_s.delete('='), args[0])
  else
    get_variable(method.to_s)
  end
end

.push(data = {}, merge = false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gon.rb', line 67

def push(data = {}, merge = false)
  raise 'Object must have each_pair method' unless data.respond_to? :each_pair

  if merge
    data.each_pair do |name, value|
      merge_variable(name.to_s, value)
    end
  else
    data.each_pair do |name, value|
      set_variable(name.to_s, value)
    end
  end
end

.rabl(*args) ⇒ Object



89
90
91
92
# File 'lib/gon.rb', line 89

def rabl(*args)
  data, options = Gon::Rabl.handler(args)
  store_builder_data 'rabl', data, options
end

.set_variable(name, value) ⇒ Object



55
56
57
# File 'lib/gon.rb', line 55

def set_variable(name, value)
  current_gon.gon[name] = value
end

.watchObject



32
33
34
# File 'lib/gon.rb', line 32

def watch
  Gon::Watch
end