Sha256: 1b1279cbdee79d23564cde22a79054928c0e82fd3edb3443e0cf8a704b021b15

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8
require 'active_support/core_ext/string/inflections'
module SchemaTools
  class KlassFactory

    class << self

      # Build classes from schema inside the given namespace. Uses all classes
      # found in schema path
      #
      # @param [String|Symbol|Module] namespace of the new classes e.g. MyCustomNamespace::MySchemaClass
      # @param [Hash] opts
      # @options opts [SchemaTools::Reader] :reader to use instead of global one
      def build(opts={})
        reader = opts[:reader] || SchemaTools::Reader
        schemata = reader.read_all( opts[:path] || SchemaTools.schema_path )
        namespace = opts[:namespace] || Object
        if namespace.is_a?(String) || namespace.is_a?(Symbol)
          namespace = "#{namespace}".constantize
        end

        schemata.each do |schema|
          klass_name = schema['name'].classify
          next if namespace.const_defined?(klass_name, false)
          klass = namespace.const_set(klass_name, Class.new)
          klass.class_eval do
            include SchemaTools::Modules::Attributes
            has_schema_attrs schema['name'], reader: reader
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_schema_tools-0.0.7 lib/schema_tools/klass_factory.rb