transmutable 1.0.0
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.
- checksums.yaml +7 -0
- data/lib/transmutable.rb +65 -0
- metadata +46 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 5f4eb1c2d626257e3cb1d8d5e0c167a21595439b
|
|
4
|
+
data.tar.gz: 57eecc8bedb73fdb4ce9f86fe41da1c8441845f6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 71a79f706637962a6c4f56964d32b3912142f531fbc3fa77a42d6fc54ec3d6dc53a9ec30e130ad04e0a1c7741218573c6668e85fcc7982712deedbfab641dae7
|
|
7
|
+
data.tar.gz: 4f2d388d6fc62a98cdb73c229cff4ef451a5259424f804dbf66e5eeada0d50b4f370e6563e6f782947a87b2ec1f2fe4ff31d43df103235373467f8b328df1cc3
|
data/lib/transmutable.rb
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require 'transmutable/base'
|
|
2
|
+
require 'transmutable/transmutable_class_methods'
|
|
3
|
+
|
|
4
|
+
module Transmutable
|
|
5
|
+
def self.included(base)
|
|
6
|
+
base.extend(TransmutableClassMethods)
|
|
7
|
+
|
|
8
|
+
add_default_transmuter_to(base)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.add_default_transmuter_to(base)
|
|
12
|
+
base.class_eval do
|
|
13
|
+
transmuter Base
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def serialize
|
|
18
|
+
transmuter.new(self).transmute
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def serialize_with(*relations)
|
|
22
|
+
return_hash = serialize
|
|
23
|
+
|
|
24
|
+
relations.each do |relation|
|
|
25
|
+
case relation
|
|
26
|
+
when String, Symbol
|
|
27
|
+
serialize_val(relation, return_hash, send(relation))
|
|
28
|
+
when Hash
|
|
29
|
+
relation.each do |key, values|
|
|
30
|
+
relation_val = send(key)
|
|
31
|
+
|
|
32
|
+
if relation_val.respond_to?(:each) && !relation_val.is_a?(Hash)
|
|
33
|
+
relation_val.each do |rel_val|
|
|
34
|
+
if return_hash[key].present?
|
|
35
|
+
return_hash[key] << rel_val.serialize_with(*values)
|
|
36
|
+
else
|
|
37
|
+
return_hash[key] = [rel_val.serialize_with(*values)]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
else
|
|
41
|
+
return_hash[key] = relation_val.serialize_with(*values)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
return_hash
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def serialize_val(key, return_hash, value)
|
|
51
|
+
return return_hash[key] = {} if value.nil?
|
|
52
|
+
|
|
53
|
+
if value.respond_to?(:each) && !value.is_a?(Hash)
|
|
54
|
+
return_hash[key] = value.collect do |val|
|
|
55
|
+
if val.respond_to? :serialize
|
|
56
|
+
return_hash[key] = val.serialize
|
|
57
|
+
else
|
|
58
|
+
{}
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
else
|
|
62
|
+
return_hash[key] = value.serialize
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: transmutable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mitch Monsen
|
|
8
|
+
- Jeff Thorup
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2015-03-11 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: A great little way to simplify object conversion when rendering an object
|
|
15
|
+
to a hash or JSON.
|
|
16
|
+
email: mmonsen7@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- "./lib/transmutable.rb"
|
|
22
|
+
homepage: https://github.com/blarshk/transmutable
|
|
23
|
+
licenses:
|
|
24
|
+
- MIT
|
|
25
|
+
metadata: {}
|
|
26
|
+
post_install_message:
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 2.2.2
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: An expressive DSL for converting Ruby objects to hashes and JSON objects
|
|
46
|
+
test_files: []
|