cereal_eyes 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cereal_eyes.rb +7 -0
- data/lib/cereal_eyes/attribute_error.rb +8 -0
- data/lib/cereal_eyes/document.rb +104 -0
- data/lib/cereal_eyes/version.rb +5 -0
- data/spec/spec_helper.rb +1 -0
- metadata +61 -0
data/lib/cereal_eyes.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/attribute_error'
|
2
|
+
|
3
|
+
module CerealEyes
|
4
|
+
|
5
|
+
module Document
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
base.send :include, InstanceMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
# Details of the serialized attributes on this class
|
15
|
+
# @return Array the serialized attributes
|
16
|
+
def serialized_attributes
|
17
|
+
@serialized_attributes ||= []
|
18
|
+
end
|
19
|
+
|
20
|
+
# Define an attribute on this model
|
21
|
+
# @param name [String] - the name of this field
|
22
|
+
# @option options [Object] :default - A default value for this attribute if it not given
|
23
|
+
# @option options [Boolean] :deserialize - Whether or not to deserialize this field (default: true)
|
24
|
+
# @option options [Boolean] :serialize - Whether or not to serialize this field (default: true)
|
25
|
+
# @option options [String|Symbol] :name - An alternate name for the serialized form of this field
|
26
|
+
# @option options [Class] :type - A nested type for this segment
|
27
|
+
# @option options [Boolean] :squash_nil - Whether or not to output nil values (default: true)
|
28
|
+
def attribute(field, options = {})
|
29
|
+
# set defaults
|
30
|
+
options[:deserialize] = true unless options.has_key?(:deserialize)
|
31
|
+
options[:serialize] = true unless options.has_key?(:serialize)
|
32
|
+
options[:squash_nil] = true unless options.has_key?(:squash_nil)
|
33
|
+
options[:name] ||= field
|
34
|
+
options[:name] = options[:name].to_sym unless Symbol === options[:name]
|
35
|
+
options[:field] = field
|
36
|
+
# make sure we're not breaking the serialization rule
|
37
|
+
if serialized_attributes.any? { |o| o[:serialize] && options[:serialize] && o[:name] == options[:name] }
|
38
|
+
raise CerealEyes::AttributeError.new "Unable to set duplicate attribute for serialization with name: #{options[:name]}"
|
39
|
+
end
|
40
|
+
if serialized_attributes.any? { |o| o[:deserialize] && options[:deserialize] && o[:field] == options[:field] }
|
41
|
+
raise CerealEyes::AttributeError.new "Unable to set duplicate attribute for deserialization on field: #{options[:field]}"
|
42
|
+
end
|
43
|
+
# add to hash
|
44
|
+
key = options[:name].is_a?(Symbol) ? options[:name] : options[:name].to_sym
|
45
|
+
serialized_attributes << options
|
46
|
+
# create the reader if deserializable
|
47
|
+
attr_reader(field) if options[:deserialize]
|
48
|
+
attr_writer(field) if options[:serialize]
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
# Perform the deserialization on a hash, returning the resultant object
|
53
|
+
def deserialize(data)
|
54
|
+
return nil if data.nil?
|
55
|
+
obj = new
|
56
|
+
# go through what we have and use it
|
57
|
+
serialized_attributes.each do |options|
|
58
|
+
next unless options[:deserialize]
|
59
|
+
name = options[:name]
|
60
|
+
value = data[name]
|
61
|
+
value ||= data[name.to_s] unless String === name
|
62
|
+
if value && options[:type] # nesting
|
63
|
+
if value.is_a?(Array)
|
64
|
+
value = value.map { |v| puts v.inspect; options[:type].deserialize(v) }
|
65
|
+
else
|
66
|
+
value = options[:type].deserialize(value)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
obj.instance_variable_set(:"@#{options[:field]}", value || options[:default])
|
70
|
+
end
|
71
|
+
obj
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
module InstanceMethods
|
77
|
+
|
78
|
+
# Serialize the given document
|
79
|
+
# @return Hash representing the serialized format of the document
|
80
|
+
def serialize
|
81
|
+
data = {}
|
82
|
+
self.class.serialized_attributes.each do |options|
|
83
|
+
next unless options[:serialize]
|
84
|
+
value = instance_variable_get(:"@#{options[:field]}") || options[:default]
|
85
|
+
if value && options[:type]
|
86
|
+
if value.kind_of?(CerealEyes::Document)
|
87
|
+
value = value.serialize
|
88
|
+
elsif value.is_a?(Array)
|
89
|
+
value = value.map(&:serialize)
|
90
|
+
else
|
91
|
+
raise ArgumentError.new("Don't know how to serialize type: #{options[:type]}")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
value = value.serialize if value.kind_of?(CerealEyes::Document)
|
95
|
+
data[options[:name]] = value if value || !options[:squash_nil]
|
96
|
+
end
|
97
|
+
data
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../lib/cereal_eyes'
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cereal_eyes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Crepezzi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70342542869680 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70342542869680
|
25
|
+
description: a proof of concept serialization format based on GSON
|
26
|
+
email: john.crepezzi@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- lib/cereal_eyes/attribute_error.rb
|
32
|
+
- lib/cereal_eyes/document.rb
|
33
|
+
- lib/cereal_eyes/version.rb
|
34
|
+
- lib/cereal_eyes.rb
|
35
|
+
- spec/spec_helper.rb
|
36
|
+
homepage: http://seejohnrun.github.com/cereal_eyes
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.17
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: GSON-like Serialization
|
60
|
+
test_files:
|
61
|
+
- spec/spec_helper.rb
|