Module: Zebris::Document::ClassMethods
- Defined in:
- lib/zebris/document.rb
Instance Method Summary collapse
- #collection(name, klass) ⇒ Object
- #collections ⇒ Object
- #const_missing(const) ⇒ Object
- #deserialize(data) ⇒ Object
- #find(key) ⇒ Object
- #key(&block) ⇒ Object
- #keygen ⇒ Object
- #properties ⇒ Object
- #property(name, type_or_serializer, deserializer = nil) ⇒ Object
- #scrub ⇒ Object
- #serialize(object, embed = false) ⇒ Object
Instance Method Details
#collection(name, klass) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/zebris/document.rb', line 112 def collection(name, klass) collections[name] = klass self.send(:attr_writer, name) self.send(:define_method, :"#{name}") { self.send(:instance_variable_get, :"@#{name}") || self.send(:instance_variable_set, :"@#{name}", []) } end |
#collections ⇒ Object
108 109 110 |
# File 'lib/zebris/document.rb', line 108 def collections @collections ||= {} end |
#const_missing(const) ⇒ Object
24 25 26 |
# File 'lib/zebris/document.rb', line 24 def const_missing(const) const end |
#deserialize(data) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/zebris/document.rb', line 71 def deserialize(data) scrub self.new.tap do |instance| data.each do |property, value| property = property.to_sym if self.properties[property] if self.properties[property].kind_of?(Zebris::Types::GenericConverter) instance.send(:"#{property}=", self.properties[property].deserializer.call(value)) else instance.send(:"#{property}=", self.properties[property].deserialize(value)) end elsif self.collections[property] && value.instance_of?(Array) instance.send(:"instance_variable_set", :"@#{property}", value.map {|row| self.collections[property].deserialize(row)}) end end end end |
#find(key) ⇒ Object
28 29 30 31 |
# File 'lib/zebris/document.rb', line 28 def find(key) stored = Zebris.redis.get key stored ? deserialize(Zebris.serializer.deserialize(stored)) : nil end |
#key(&block) ⇒ Object
90 91 92 |
# File 'lib/zebris/document.rb', line 90 def key(&block) @keygen = block end |
#keygen ⇒ Object
94 95 96 |
# File 'lib/zebris/document.rb', line 94 def keygen @keygen end |
#properties ⇒ Object
98 99 100 |
# File 'lib/zebris/document.rb', line 98 def properties @properties ||= {} end |
#property(name, type_or_serializer, deserializer = nil) ⇒ Object
102 103 104 105 106 |
# File 'lib/zebris/document.rb', line 102 def property(name, type_or_serializer, deserializer = nil) properties[name] = resolve_type(type_or_serializer, deserializer) self.send(:attr_accessor, name) end |
#scrub ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/zebris/document.rb', line 41 def scrub unless @scrubbed self.properties.each do |property, type| unless type.kind_of?(Zebris::Types::GenericConverter) resolved_type = case type when Symbol if zebris_type?(type) Zebris::Types.const_get(type) else klass = lookup_class(type) zebris_document?(klass) ? klass : Zebris::Types::BasicObject end else zebris_document?(type) || zebris_type?(type) ? type : Zebris::Types::BasicObject end self.properties[property] = resolved_type end end self.collections.each do |property, type| if self.collections[property].kind_of?(Symbol) self.collections[property] = self.const_get(self.collections[property]) end end @scrubbed = true end end |
#serialize(object, embed = false) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/zebris/document.rb', line 33 def serialize(object, = false) scrub ( ? {} : {"key" => object.key}).tap do |attributes| attributes.merge!(serialize_properties(object)) attributes.merge!(serialize_collections(object)) end end |