Class: JsonObject
- Inherits:
-
Object
- Object
- JsonObject
- Defined in:
- lib/json_object.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#schema_id ⇒ Object
Returns the value of attribute schema_id.
Class Method Summary collapse
Instance Method Summary collapse
- #from_hash(hash) ⇒ Object
- #from_json(json_string) ⇒ Object
-
#initialize ⇒ JsonObject
constructor
A new instance of JsonObject.
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ JsonObject
Returns a new instance of JsonObject.
51 52 53 54 55 56 57 58 59 |
# File 'lib/json_object.rb', line 51 def initialize @values = Hash.new self.class.all_keys.each do |key,value| if value @values[key] = value.new end end @valid = Hash.new end |
Instance Attribute Details
#schema_id ⇒ Object
Returns the value of attribute schema_id.
49 50 51 |
# File 'lib/json_object.rb', line 49 def schema_id @schema_id end |
Class Method Details
.all_keys ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/json_object.rb', line 37 def self.all_keys keys = Hash.new klass = self while(klass != JsonObject) do if klass.keys keys.merge!(klass.keys) end klass = klass.superclass end keys end |
.attribute(name, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/json_object.rb', line 3 def self.attribute(name, &block) define_method("#{name}=") do |value| @values[name] = value @valid[name] = true end @keys ||= Hash.new if block_given? define_method("#{name}") do return @values[name] end nested_class = self.const_set(name.to_s.capitalize, Class.new(JsonObject)) nested_class.class_exec(&block) type = nested_class else define_method("#{name}") do if @valid[name] return @values[name] else return nil end end type = nil end @keys[name] = type end |
.keys ⇒ Object
33 34 35 |
# File 'lib/json_object.rb', line 33 def self.keys @keys end |
Instance Method Details
#from_hash(hash) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/json_object.rb', line 65 def from_hash(hash) hash.each do |key, value| next if key == "$schema" type = self.class.all_keys[key.to_sym] value = hash[key] if type nested_object = send("#{key}") nested_object.from_hash(value) else send("#{key}=", value) end end self end |
#from_json(json_string) ⇒ Object
80 81 82 |
# File 'lib/json_object.rb', line 80 def from_json(json_string) from_hash(JSON(json_string)) end |
#to_hash ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/json_object.rb', line 84 def to_hash hash = Hash.new if schema_id hash["$schema"] = schema_id end self.class.all_keys.each do |attribute,type| value = @values[attribute] if type && value.valid? hash[attribute] = value.to_hash elsif @valid[attribute] hash[attribute] = value end end hash end |
#to_json ⇒ Object
100 101 102 |
# File 'lib/json_object.rb', line 100 def to_json JSON.pretty_generate(to_hash) end |
#valid? ⇒ Boolean
61 62 63 |
# File 'lib/json_object.rb', line 61 def valid? !@valid.empty? end |