diff options
Diffstat (limited to 'lib/json/light_object.rb')
-rw-r--r-- | lib/json/light_object.rb | 37 |
1 files changed, 37 insertions, 0 deletions
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 |