json_serialisable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/json_serialisable.rb +34 -0
- metadata +56 -0
@@ -0,0 +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( *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
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_serialisable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Carl Ellis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: &20584776 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.5.4
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *20584776
|
25
|
+
description: Adds support for autogenerating json serialisation functions
|
26
|
+
email: carlc75@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- lib/json_serialisable.rb
|
32
|
+
homepage: http://carlellis.co.uk
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.11
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Class mixin for JSON support
|
56
|
+
test_files: []
|