Skip to content

Commit

Permalink
Refactoring to support JRuby sorted instance_variables
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyObtiva committed Dec 27, 2020
1 parent c056445 commit bb30cb1
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 69 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
- Serialize struct member values as JSON
- Serialize classes/modules as JSON
- Serialize cycles by using object ID references
- Deserialize instance variables as JSON
- Deserialize Class occurence in variables as JSON
- Deserialize Module occurence in variables as JSON
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GEM
hashie (3.6.0)
highline (2.0.3)
json (1.8.6)
json (1.8.6-java)
juwelier (2.1.3)
builder
bundler (>= 1.13)
Expand All @@ -50,6 +51,7 @@ GEM
multipart-post (2.1.1)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
nokogiri (1.10.10-java)
oauth2 (1.4.4)
faraday (>= 0.8, < 2.0)
jwt (>= 1.0, < 3.0)
Expand Down Expand Up @@ -90,10 +92,12 @@ GEM
tins (~> 0.8)
thor (0.18.1)
thread_safe (0.3.6)
thread_safe (0.3.6-java)
tins (0.13.2)

PLATFORMS
ruby
universal-java-1.8

DEPENDENCIES
bundler (>= 1.0)
Expand All @@ -106,4 +110,4 @@ DEPENDENCIES
simplecov (~> 0.10.0)

BUNDLED WITH
2.1.4
2.2.2
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
[![Coverage Status](https://coveralls.io/repos/github/AndyObtiva/yasl/badge.svg?branch=master)](https://coveralls.io/github/AndyObtiva/yasl?branch=master)
[![Maintainability](https://api.codeclimate.com/v1/badges/e8d043b8c78c801f0aa3/maintainability)](https://codeclimate.com/github/AndyObtiva/yasl/maintainability)

A pure Ruby serialization library that works in Opal Ruby as an alternative to YAML/Marshal.
A pure Ruby serialization library that works across different Ruby implementations like Opal/JRuby as an alternative to YAML/Marshal.

## Assumptions
## Requirements

- Portablity across Ruby flavors (e.g. Opal)
- Portablity across different Ruby implementations, especially Opal and JRuby.
- Zero configuration. Developers are too busy solving business domain problems to worry about low-level serialization details.
- Silently ignore all that is not serializable like Proc, Binding, and IO objects
- Support serializing classes and modules, not just object instances
- JSON is good enough. No need for premature optimization.
- Silently ignore non-serializable objects like `Proc`, `Binding`, and `IO`.
- Support serializing classes and modules, not just object instances.
- JSON encoding is good enough. No need for premature optimization.

## Usage Instructions

Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Juwelier::Tasks.new do |gem|
gem.name = "yasl"
gem.homepage = "http://github.com/AndyObtiva/yasl"
gem.license = "MIT"
gem.summary = %Q{A pure Ruby serialization library that works in Opal Ruby as an alternative to YAML/Marshal.}
gem.description = %Q{A pure Ruby serialization library that works in Opal Ruby as an alternative to YAML/Marshal.}
gem.summary = %Q{A pure Ruby serialization library that works across different Ruby implementations like Opal/JRuby as an alternative to YAML/Marshal.}
gem.description = %Q{A pure Ruby serialization library that works across different Ruby implementations like Opal/JRuby as an alternative to YAML/Marshal.}
gem.email = "andy.am@gmail.com"
gem.authors = ["Andy Maleh"]

Expand Down
8 changes: 5 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

## Next

- Deserialize instance variables as JSON
- Deserialize classes and modules
- Deserialize Struct members as JSON
- Deserialize class variables as JSON
- Deserialize direct Class occurence in variables as JSON
- Deserialize direct Module occurence in variables as JSON
- Deserialize top-level Class occurence in variables as JSON
- Deserialize top-level Module occurence in variables as JSON
- Deal with bypassing constructor args (perhaps providing an alternative to new)
- Materialize a class matching the non-existing class
- Support `include_classes` option on both dump and load
- Support passing `whitelist_classes` to filter by upon load

## Soon

Expand Down
2 changes: 1 addition & 1 deletion lib/yasl/dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def dump_instance_variables(object)
structure = {}
structure
if !object.instance_variables.empty?
structure[:_instance_variables] = object.instance_variables.reduce({}) do |instance_vars, var|
structure[:_instance_variables] = object.instance_variables.sort.reduce({}) do |instance_vars, var|
value = object.instance_variable_get(var)
instance_vars.merge(var.to_s.sub('@', '') => dump_structure(value))
end
Expand Down
112 changes: 56 additions & 56 deletions spec/lib/yasl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,8 @@ def initialize
_class: car1.class.name,
_id: 1,
_instance_variables: {
make: car1.make,
model: car1.model,
year: car1.year,
registration_time: {
_class: 'Time',
_data: car1.registration_time.to_datetime.marshal_dump
},
registration_date: {
_class: 'Date',
_data: car1.registration_date.marshal_dump
},
registration_date_time: {
_class: 'DateTime',
_data: car1.registration_date_time.marshal_dump
class_attribute: {
_class: 'Car',
},
complex_number: {
_class: 'Complex',
Expand All @@ -344,6 +332,19 @@ def initialize
_class: 'Complex',
_data: car1.complex_polar_number.to_s
},
make: car1.make,
model: car1.model,
module_attribute: {
_class: 'Driving',
},
range: {
_class: 'Range',
_data: [car1.range.begin, car1.range.end, car1.range.exclude_end?]
},
range_exclusive: {
_class: 'Range',
_data: [car1.range_exclusive.begin, car1.range_exclusive.end, car1.range_exclusive.exclude_end?]
},
rational_number: {
_class: 'Rational',
_data: car1.rational_number.to_s
Expand All @@ -352,9 +353,17 @@ def initialize
_class: 'Regexp',
_data: car1.regex.to_s
},
symbol: {
_class: 'Symbol',
_data: car1.symbol.to_s
registration_date: {
_class: 'Date',
_data: car1.registration_date.marshal_dump
},
registration_date_time: {
_class: 'DateTime',
_data: car1.registration_date_time.marshal_dump
},
registration_time: {
_class: 'Time',
_data: car1.registration_time.to_datetime.marshal_dump
},
set: {
_class: 'Set',
Expand All @@ -367,20 +376,11 @@ def initialize
3.7
]
},
range: {
_class: 'Range',
_data: [car1.range.begin, car1.range.end, car1.range.exclude_end?]
},
range_exclusive: {
_class: 'Range',
_data: [car1.range_exclusive.begin, car1.range_exclusive.end, car1.range_exclusive.exclude_end?]
},
class_attribute: {
_class: 'Car',
},
module_attribute: {
_class: 'Driving',
symbol: {
_class: 'Symbol',
_data: car1.symbol.to_s
},
year: car1.year,
},
_classes: [
{
Expand All @@ -406,18 +406,18 @@ def initialize
_instance_variables: {
make: car2.make,
model: car2.model,
year: car2.year,
owner: {
_class: 'Driving::Person',
_id: 1,
_instance_variables: {
name: person1.name,
dob: {
_class: 'Time',
_data: person1.dob.to_datetime.marshal_dump
}
},
name: person1.name,
}
}
},
year: car2.year,
},
_classes: [
{
Expand Down Expand Up @@ -523,11 +523,11 @@ def initialize
_class: 'Driving::Person',
_id: 1,
_instance_variables: {
name: person1.name,
dob: {
_class: 'Time',
_data: person1.dob.to_datetime.marshal_dump
}
},
name: person1.name,
}
}
},
Expand Down Expand Up @@ -558,11 +558,6 @@ def initialize
_class: person2.class.name,
_id: 1,
_instance_variables: {
name: person2.name,
dob: {
_class: 'Time',
_data: person2.dob.to_datetime.marshal_dump
},
cars: {
_class: 'Array',
_data: [
Expand All @@ -585,7 +580,12 @@ def initialize
},
},
]
}
},
dob: {
_class: 'Time',
_data: person2.dob.to_datetime.marshal_dump
},
name: person2.name,
},
_classes: [
{
Expand Down Expand Up @@ -618,11 +618,6 @@ def initialize
_class: person3.class.name,
_id: 1,
_instance_variables: {
name: person3.name,
dob: {
_class: 'Time',
_data: person3.dob.to_datetime.marshal_dump
},
cars: {
_class: 'Hash',
_data: [
Expand Down Expand Up @@ -651,7 +646,12 @@ def initialize
},
]
]
}
},
dob: {
_class: 'Time',
_data: person3.dob.to_datetime.marshal_dump
},
name: person3.name,
},
_classes: [
{
Expand Down Expand Up @@ -684,11 +684,6 @@ def initialize
_class: person4.class.name,
_id: 1,
_instance_variables: {
name: person4.name,
dob: {
_class: 'Time',
_data: person4.dob.to_datetime.marshal_dump
},
cars: {
_class: 'Array',
_data: [
Expand All @@ -698,11 +693,11 @@ def initialize
_instance_variables: {
make: car3.make,
model: car3.model,
year: car3.year,
owner: {
_class: person4.class.name,
_id: 1,
},
year: car3.year,
},
},
{
Expand All @@ -711,15 +706,20 @@ def initialize
_instance_variables: {
make: car4.make,
model: car4.model,
year: car4.year,
owner: {
_class: person4.class.name,
_id: 1,
},
year: car4.year,
},
},
]
}
},
dob: {
_class: 'Time',
_data: person4.dob.to_datetime.marshal_dump
},
name: person4.name,
},
_classes: [
{
Expand Down

0 comments on commit bb30cb1

Please sign in to comment.