Class: Gon::Sinatra::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/gon/sinatra/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables) ⇒ Store

Returns a new instance of Store.



7
8
9
# File 'lib/gon/sinatra/store.rb', line 7

def initialize(variables)
  @env = variables
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



19
20
21
22
23
24
25
26
27
28
# File 'lib/gon/sinatra/store.rb', line 19

def method_missing(m, *args, &block)
  if ( m.to_s =~ /=$/ )
    if public_methods.include? m.to_s[0..-2].to_sym
      raise "You can't use Gon public methods for storing data"
    end
    set_variable(m.to_s.delete('='), args[0])
  else
    get_variable(m.to_s)
  end
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



5
6
7
# File 'lib/gon/sinatra/store.rb', line 5

def request
  @request
end

Instance Method Details

#all_variablesObject



11
12
13
# File 'lib/gon/sinatra/store.rb', line 11

def all_variables
  @env
end

#clearObject



15
16
17
# File 'lib/gon/sinatra/store.rb', line 15

def clear
  @env.clear
end

#get_variable(name) ⇒ Object Also known as: get



30
31
32
# File 'lib/gon/sinatra/store.rb', line 30

def get_variable(name)
  @env[name]
end

#jbuilder(view_path, options = {}) ⇒ Object

Raises:

  • (NoMethodError)


62
63
64
# File 'lib/gon/sinatra/store.rb', line 62

def jbuilder(view_path, options = {})
  raise NoMethodError.new("Not available for sinatra")
end

#rabl(view_path, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gon/sinatra/store.rb', line 40

def rabl(view_path, options = {})
  unless defined?(::Rabl)
    raise Exception.new("You must require rabl and register Gon::Sinatra::Rabl to use rabl")
  end

  unless options[:instance]
    raise ArgumentError.new("You should pass :instance in options: :instance => self")
  end

  rabl_data = Gon::Sinatra::Rabl.parse_rabl(view_path, options[:instance])

  if options[:as]
    set_variable(options[:as].to_s, rabl_data)
  elsif rabl_data.is_a? Hash
    rabl_data.each do |key, value|
      set_variable(key, value)
    end
  else
    set_variable('rabl', rabl_data)
  end
end

#set_variable(name, value) ⇒ Object Also known as: set



35
36
37
# File 'lib/gon/sinatra/store.rb', line 35

def set_variable(name, value)
  @env[name] = value
end