Module: Cerealize

Defined in:
lib/cerealize.rb,
lib/cerealize/version.rb,
lib/cerealize/attr_hash.rb,
lib/cerealize/codec/text.rb,
lib/cerealize/codec/yaml.rb,
lib/cerealize/codec/marshal.rb

Defined Under Namespace

Modules: ClassMethods, Codec, InstanceMethods Classes: NoSuchCodec, NoSuitableCodec

Constant Summary collapse

InternalName =
'CerealizeMethods'
VERSION =
'1.0.2'

Class Method Summary collapse

Class Method Details

.codec_detect(str) ⇒ Object



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

def codec_detect(str)
  codecs.find{ |codec| codec.yours?(str) }
end

.codec_get(codec_name) ⇒ Object



47
48
49
50
51
# File 'lib/cerealize.rb', line 47

def codec_get(codec_name)
  Codec.const_get(codec_name.to_s.capitalize)
rescue NameError
  raise NoSuchCodec.new(codec_name)
end

.codec_namesObject



39
40
41
# File 'lib/cerealize.rb', line 39

def codec_names
  @codec_names ||= [:yaml, :marshal, :text]
end

.codecsObject



33
34
35
36
37
# File 'lib/cerealize.rb', line 33

def codecs
  @codecs ||= codec_names.map{ |codec_name|
                codec_get(codec_name)
              }
end

.decode(str, codec = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/cerealize.rb', line 58

def decode(str, codec=nil)
  return nil unless str
  codec ||= codec_detect(str)

  if codec && codec.yours?(str)
    codec.decode(str)
  else
    raise NoSuitableCodec.new("#{str[0..46]}...")
  end
end

.encode(obj, codec) ⇒ Object



53
54
55
56
# File 'lib/cerealize.rb', line 53

def encode(obj, codec)
  return nil unless obj
  codec.encode(obj)
end

.included(base) ⇒ Object

Dirty functionality: note that *_changed? and changed? work, but *_was, *_change, and changes do NOT work.



25
26
27
28
29
30
# File 'lib/cerealize.rb', line 25

def self.included(base)
  base.send( :extend,    ClassMethods)
  base.send(:include, InstanceMethods)
  base.superclass_delegating_accessor :cerealize_option
  base.cerealize_option = {}
end