Module: Siren::Node

Defined in:
lib/siren/node.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



19
20
21
# File 'lib/siren/node.rb', line 19

def self.extended(base)
  @classes[base.name.split('::').last] = base
end

.find_class(name) ⇒ Object



30
31
32
33
# File 'lib/siren/node.rb', line 30

def self.find_class(name)
  name = name.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  @classes[name]
end

.from_json(hash) ⇒ Object



23
24
25
26
27
28
# File 'lib/siren/node.rb', line 23

def self.from_json(hash)
  hash = Siren.parse(hash) if String === hash
  return hash unless Hash === hash && hash[TYPE_FIELD]
  klass = find_class(hash[TYPE_FIELD])
  klass ? klass.from_json(hash) : hash
end

Instance Method Details

#from_json(hash) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/siren/node.rb', line 4

def from_json(hash)
  object = self.new
  hash.each do |key, value|
    object.instance_variable_set("@#{key}", value)
    if Reference === value
      value.on(:resolve) do |ref, root, symbols|
        object.instance_variable_set("@#{key}", ref.find(root, symbols, object))
      end
    end
  end
  object
end