Module: Clientside::Accessible

Defined in:
lib/clientside.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clientside.rb', line 13

def self.included(base)
  base.singleton_class.class_eval do
    attr_reader :js_allowed

    def js_allow(*args)
      (@js_allowed ||= []).concat args
    end
  end

  def self.reinflate(json, os)
    case json
    when Hash
      if json[:__clientside__]
        os.fetch json[:__clientside_id__]
      else
        Hash[json.map {|k, v| [k, reinflate(v, os)]}]
      end
    when Array
      json.map {|v| reinflate(v, os)}
    else
      json
    end
  end
end

.reinflate(json, os) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/clientside.rb', line 22

def self.reinflate(json, os)
  case json
  when Hash
    if json[:__clientside__]
      os.fetch json[:__clientside_id__]
    else
      Hash[json.map {|k, v| [k, reinflate(v, os)]}]
    end
  when Array
    json.map {|v| reinflate(v, os)}
  else
    json
  end
end

Instance Method Details

#to_json(*args) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/clientside.rb', line 38

def to_json(*args)
  name = self.class.name
  methods = self.class.js_allowed
  h = {__clientside__: true, __clientside_id__: object_id,
       methods: methods}
  h.to_json *args
end