Sha256: 016e2d9a1f91791c67f8792182050a4d059448a3cdfc53f1c409cc3ed9c44545

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'rubiks/nodes/validated_node'
require 'rubiks/nodes/cube'
require 'multi_json'

module ::Rubiks

  class Schema < ::Rubiks::ValidatedNode
    child :cubes, [::Rubiks::Cube]

    validates :cubes_present

    def self.new_from_hash(hash={})
      new_instance = new([])
      return new_instance.from_hash(hash)
    end

    def cubes_present
      if self.cubes.present?
        self.cubes.each do |cube|
          cube.validate
          errors.push(*cube.errors)
        end
      else
        errors << 'Cubes Required for Schema'
      end
    end

    def from_hash(working_hash)
      return self if working_hash.nil?
      working_hash.stringify_keys!

      parse_cubes(working_hash.delete('cubes'))
      return self
    end

    def parse_cubes(cubes_array)
      return if cubes_array.nil? || cubes_array.empty?

      cubes_array.each do |cube_hash|
        self.cubes << ::Rubiks::Cube.new_from_hash(cube_hash)
      end
    end

    def to_hash
      hash = {}

      hash['cubes'] = self.cubes.map(&:to_hash) if self.cubes.present?

      return hash
    end

    def to_json
      MultiJson.dump(to_hash)
    end

    def to_xml
      to_hash.to_xml(:root => 'Schema')
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubiks-0.0.4 lib/rubiks/nodes/schema.rb