class Avro::Builder::DSL
This class is used to construct Avro
schemas (not protocols) using a ruby DSL
Public Class Methods
new(str = nil, filename: nil, &block)
click to toggle source
An instance of the DSL
is initialized with a string or a block to evaluate to define Avro
schema objects.
# File lib/avro/builder/dsl.rb, line 34 def initialize(str = nil, filename: nil, &block) if str instance_eval(*[str, filename].compact) elsif filename instance_eval(File.read(filename), filename) else instance_eval(&block) end end
Public Instance Methods
abstract?()
click to toggle source
# File lib/avro/builder/dsl.rb, line 44 def abstract? @last_object && @last_object.abstract? end
as_schema()
click to toggle source
# File lib/avro/builder/dsl.rb, line 89 def as_schema Avro::Schema.parse(to_json(validate: false)) end
enum(name = nil, *symbols, **options, &block)
click to toggle source
fixed(name = nil, size = nil, options = {}, &block)
click to toggle source
# File lib/avro/builder/dsl.rb, line 67 def fixed(name = nil, size = nil, options = {}, &block) size_option = size.is_a?(Hash) ? size : { size: size } create_named_type(name, :fixed, size_option.merge(options), &block) end
import(name)
click to toggle source
Imports from the file with specified name fragment.
# File lib/avro/builder/dsl.rb, line 54 def import(name) previous_namespace = namespace result = eval_file(name) namespace(previous_namespace) result end
record(name = nil, options = {}, &block)
click to toggle source
Define an Avro
schema record
# File lib/avro/builder/dsl.rb, line 49 def record(name = nil, options = {}, &block) create_named_type(name, :record, options, &block) end
to_h()
click to toggle source
Return the last schema object processed as a Hash representing the Avro
schema.
# File lib/avro/builder/dsl.rb, line 74 def to_h @last_object.to_h(SchemaSerializerReferenceState.new) end
to_json(validate: true, pretty: true)
click to toggle source
Return the last schema object processed as an Avro
JSON schema
# File lib/avro/builder/dsl.rb, line 79 def to_json(validate: true, pretty: true) hash = to_h (pretty ? JSON.pretty_generate(hash) : hash.to_json).tap do |json| # Uncomment the next line to debug: # puts json # Parse the schema to validate before returning ::Avro::Schema.parse(json) if validate end end
type(*)
click to toggle source
Override the type method from AnonymousTypes
to store a reference to the last type defined.
Calls superclass method
Avro::Builder::AnonymousTypes#type
# File lib/avro/builder/dsl.rb, line 95 def type(*) @last_object = super end
type_macro(name, type_object, options = {})
click to toggle source
# File lib/avro/builder/dsl.rb, line 99 def type_macro(name, type_object, options = {}) raise "#{type_object.inspect} must be a type object" unless type_object.is_a?(Types::Type) raise "namespace cannot be included in name: #{name}" if name.to_s.index('.') type_clone = type_object.clone type_clone.send(:abstract=, true) cache.add_type_by_name(type_clone, name, options[:namespace] || namespace) @last_object = type_clone end
Private Instance Methods
cache()
click to toggle source
# File lib/avro/builder/dsl.rb, line 111 def cache @cache ||= Avro::Builder::DefinitionCache.new(self) end
create_named_type(name, avro_type_name, options = {}, &block)
click to toggle source
# File lib/avro/builder/dsl.rb, line 115 def create_named_type(name, avro_type_name, options = {}, &block) @last_object = create_and_configure_builtin_type(avro_type_name, cache: cache, internal: { _name: name, namespace: namespace }, options: options, &block) end
eval_file(name)
click to toggle source
# File lib/avro/builder/dsl.rb, line 124 def eval_file(name) file_path = if namespace begin find_file([namespace, name].join('.')) rescue FileNotFoundError find_file(name) end else find_file(name) end instance_eval(File.read(file_path), file_path) end