Sha256: 8ab6e3c323e9c868acfef9afa57dc090492418b7dd99e74b51af4e4761e6dce3

Contents?: true

Size: 1.86 KB

Versions: 9

Compression:

Stored size: 1.86 KB

Contents

class JsonObject

  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

  def self.keys
    @keys
  end

  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

  attr_accessor :schema_id

  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

  def valid?
    !@valid.empty?
  end

  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

  def from_json(json_string)
    from_hash(JSON(json_string))
  end

  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

  def to_json
    JSON.pretty_generate(to_hash)
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
inqlude-0.12.0 lib/json_object.rb
inqlude-0.11.0 lib/json_object.rb
inqlude-0.10.0 lib/json_object.rb
inqlude-0.9.0 lib/json_object.rb
inqlude-0.8.0 lib/json_object.rb
inqlude-0.7.4 lib/json_object.rb
inqlude-0.7.3 lib/json_object.rb
inqlude-0.7.2 lib/json_object.rb
inqlude-0.7.1 lib/json_object.rb