json_serialisable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/json_serialisable.rb +34 -34
- metadata +5 -5
data/lib/json_serialisable.rb
CHANGED
@@ -1,34 +1,34 @@
|
|
1
|
-
# Mixin for the Class class
|
2
|
-
class Class
|
3
|
-
# A shorthand which automatically adds methods for serialising via JSON
|
4
|
-
#
|
5
|
-
# Example:
|
6
|
-
# attr_serialise :a, :b, ...
|
7
|
-
#
|
8
|
-
# Arguments:
|
9
|
-
# (:variable_label)+
|
10
|
-
#
|
11
|
-
# Remark:
|
12
|
-
# There should be a matching contstructor with the same method signature as the labels used in the function call.
|
13
|
-
# This way json_create will not error when it is called.
|
14
|
-
|
15
|
-
def attr_serialise(*attrs)
|
16
|
-
|
17
|
-
# Sanity checks
|
18
|
-
abort "Please provide at least one argument" if attrs.length == 0
|
19
|
-
|
20
|
-
#Build to the to_json method
|
21
|
-
tojson = "def to_json(*a)\n{ 'json_class' => #{self.name}"
|
22
|
-
attrs.each { |a| tojson << ",\n'#{a}' => @#{a}"}
|
23
|
-
tojson << "\n}.to_json(*a)\nend"
|
24
|
-
|
25
|
-
#build to json_create method
|
26
|
-
jsoncreate = "def self.json_create(o)\nnew(
|
27
|
-
attrs.each { |a| jsoncreate << ",
|
28
|
-
jsoncreate << ")\nend"
|
29
|
-
|
30
|
-
# Add methods
|
31
|
-
class_eval(tojson)
|
32
|
-
class_eval(jsoncreate)
|
33
|
-
end
|
34
|
-
end
|
1
|
+
# Mixin for the Class class
|
2
|
+
class Class
|
3
|
+
# A shorthand which automatically adds methods for serialising via JSON
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
# attr_serialise :a, :b, ...
|
7
|
+
#
|
8
|
+
# Arguments:
|
9
|
+
# (:variable_label)+
|
10
|
+
#
|
11
|
+
# Remark:
|
12
|
+
# There should be a matching contstructor with the same method signature as the labels used in the function call.
|
13
|
+
# This way json_create will not error when it is called.
|
14
|
+
|
15
|
+
def attr_serialise(*attrs)
|
16
|
+
|
17
|
+
# Sanity checks
|
18
|
+
abort "Please provide at least one argument" if attrs.length == 0
|
19
|
+
|
20
|
+
#Build to the to_json method
|
21
|
+
tojson = "def to_json(*a)\n{ 'json_class' => #{self.name}"
|
22
|
+
attrs.each { |a| tojson << ",\n'#{a}' => @#{a}"}
|
23
|
+
tojson << "\n}.to_json(*a)\nend"
|
24
|
+
|
25
|
+
#build to json_create method
|
26
|
+
jsoncreate = "def self.json_create(o)\nnew( o['#{attrs.shift}']"
|
27
|
+
attrs.each { |a| jsoncreate << ", o['#{a}']" }
|
28
|
+
jsoncreate << ")\nend"
|
29
|
+
|
30
|
+
# Add methods
|
31
|
+
class_eval(tojson)
|
32
|
+
class_eval(jsoncreate)
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_serialisable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &7888880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 1.5.4
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *7888880
|
25
25
|
description: Adds support for autogenerating json serialisation functions
|
26
26
|
email: carlc75@gmail.com
|
27
27
|
executables: []
|
@@ -29,7 +29,7 @@ extensions: []
|
|
29
29
|
extra_rdoc_files: []
|
30
30
|
files:
|
31
31
|
- lib/json_serialisable.rb
|
32
|
-
homepage:
|
32
|
+
homepage: https://github.com/carl-ellis/json_serialisable
|
33
33
|
licenses: []
|
34
34
|
post_install_message:
|
35
35
|
rdoc_options: []
|