From f7ed2c6b0b54e6fc04d58b38d9639cbef5ee6255 Mon Sep 17 00:00:00 2001 From: Florian Frank Date: Wed, 18 Apr 2012 10:30:51 +0200 Subject: Add implementation for a light object --- lib/json/common.rb | 1 + lib/json/light_object.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 lib/json/light_object.rb (limited to 'lib/json') 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 -- cgit v1.2.1