summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2012-04-18 10:30:51 +0200
committerFlorian Frank <flori@ping.de>2012-04-27 20:23:54 +0200
commitf7ed2c6b0b54e6fc04d58b38d9639cbef5ee6255 (patch)
treef89ae2272eab3e33005905fa6faec61e678fe18d
parent0f0f08aa6efd770596d28e321dbca01a4738bb17 (diff)
downloadjson-f7ed2c6b0b54e6fc04d58b38d9639cbef5ee6255.tar.gz
Add implementation for a light object
-rw-r--r--lib/json/common.rb1
-rw-r--r--lib/json/light_object.rb37
2 files changed, 38 insertions, 0 deletions
diff --git a/lib/json/common.rb b/lib/json/common.rb
index cf7a8b9..e8e76b6 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -1,4 +1,5 @@
require 'json/version'
+require 'json/light_object'
module JSON
class << self
diff --git a/lib/json/light_object.rb b/lib/json/light_object.rb
new file mode 100644
index 0000000..69dd5ab
--- /dev/null
+++ b/lib/json/light_object.rb
@@ -0,0 +1,37 @@
+require 'ostruct'
+
+module JSON
+ class LightObject < OpenStruct
+ class << self
+ alias [] new
+
+ def json_create(data)
+ data = data.dup
+ data.delete JSON.create_id
+ self[data]
+ end
+ end
+
+ def to_hash
+ __send__ :table
+ end
+
+ def |(other)
+ self.class[other.to_hash.merge(to_hash)]
+ end
+
+ def as_json(*)
+ to_hash | { JSON.create_id => self.class.name }
+ end
+
+ def to_json(*a)
+ as_json.to_json(*a)
+ end
+
+ def method_missing(*a, &b)
+ to_hash.__send__(*a, &b)
+ rescue NoMethodError
+ super
+ end
+ end
+end