Sha256: 7c4f31e3d4e6731d2433378ae8e633aeba98f396aa57b0fc76c52cdb8f64131e

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Schematic
  module Generator
    class Sandbox
      attr_accessor :ignored_elements, :added_elements, :required_elements, :non_required_elements

      def initialize(klass)
        @klass = klass
        @ignored_elements ||= Hash.new([])
        @added_elements ||= {}
        @required_elements ||= []
        @non_required_elements ||= []
      end

      def run(&block)
        instance_eval &block
      end

      def xsd_generator
        @xsd_generator ||= Schematic::Generator::Xsd.new(@klass)
      end

      def to_xsd(options = {})
        output = ""
        builder = Builder::XmlMarkup.new(:target => output, :indent => 2)
        xsd_generator.options = options
        xsd_generator.header(builder)
        xsd_generator.schema(builder)
        output
      end

      def generate_xsd(builder, klass, include_collection, options, exclude)
        xsd_generator.options = options
        xsd_generator.generate(builder, klass, include_collection, exclude)
      end

      def ignore(*fields)
        if fields[0].is_a?(Hash)
          fields[0].each do |key, value|
            ignored_elements[key.to_sym] = value
          end
        else
          fields.each { |field| ignored_elements[field] = nil }
        end
      end

      def add(*args)
        name = args.shift
        if name.is_a? Hash
          added_elements[name.keys.first] = name.values.first
        else
          added_elements[name] = nil
        end
      end

      def required(*fields)
        fields.each { |field| required_elements << field }
      end

      def not_required(*fields)
        fields.each { |field| non_required_elements << field }
      end

      def root(name)
        xsd_generator.names.root = name
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schematic-0.6.2 lib/schematic/generator/sandbox.rb