Class: Cerealizer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cerealizer.rb

Overview

Configure a serialize keys and deal with marshaling to and from JSON.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, scope) ⇒ Base

Returns a new instance of Base.



17
18
19
20
# File 'lib/cerealizer.rb', line 17

def initialize(object, scope)
  @object, @scope = object, scope
  self
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



15
16
17
# File 'lib/cerealizer.rb', line 15

def object
  @object
end

#scopeObject (readonly)

Returns the value of attribute scope.



15
16
17
# File 'lib/cerealizer.rb', line 15

def scope
  @scope
end

Class Method Details

.key(key) ⇒ Object

Registers a key with the class.



38
39
40
# File 'lib/cerealizer.rb', line 38

def self.key(key)
  keys.push Key.new(key)
end

.keysObject

Keys that are registered with the class.



43
44
45
# File 'lib/cerealizer.rb', line 43

def self.keys
  @keys ||= []
end

.read_keys(objects, user) ⇒ Object

Read the keys on each object in a collection of objects.



48
49
50
# File 'lib/cerealizer.rb', line 48

def self.read_keys(objects, user)
  objects.map { |object| self.new(object,user).read_keys }
end

Instance Method Details

#read_keysObject

Call methods on the object that’s being presented and create a flat hash for these mofos.



24
25
26
27
28
29
# File 'lib/cerealizer.rb', line 24

def read_keys
  self.class.keys.inject Hash.new do |hash, key|
    hash[key.name] = proxy_reader(key.name) if readable?(key.name)
    hash
  end
end

#write_keys(attrs) ⇒ Object

Update the attrs on zie model.



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

def write_keys(attrs)
  attrs.each { |key, value| proxy_writer(key, value) if writeable?(key) }
  self
end