Class: Avro::Builder::DSL
- Inherits:
-
Object
- Object
- Avro::Builder::DSL
- Includes:
- AnonymousTypes, DslAttributes, DslOptions, FileHandler
- Defined in:
- lib/avro/builder/dsl.rb
Overview
This class is used to construct Avro schemas (not protocols) using a ruby DSL
Constant Summary
Constants included from TypeFactory
TypeFactory::BUILTIN_TYPES, TypeFactory::COMPLEX_TYPES, TypeFactory::NAMED_TYPES
Instance Method Summary collapse
- #as_schema ⇒ Object
-
#enum(name = nil, *symbols, **options, &block) ⇒ Object
DSL methods for Types.
- #fixed(name = nil, size = nil, options = {}, &block) ⇒ Object
-
#import(name) ⇒ Object
Imports from the file with specified name fragment.
-
#initialize(str = nil, filename: nil, &block) ⇒ DSL
constructor
An instance of the DSL is initialized with a string or a block to evaluate to define Avro schema objects.
-
#record(name = nil, options = {}, &block) ⇒ Object
Define an Avro schema record.
-
#to_h ⇒ Object
Return the last schema object processed as a Hash representing the Avro schema.
-
#to_json(validate: true, pretty: true) ⇒ Object
Return the last schema object processed as an Avro JSON schema.
-
#type ⇒ Object
Override the type method from AnonymousTypes to store a reference to the last type defined.
- #type_macro(name, type_object, options = {}) ⇒ Object
Methods included from AnonymousTypes
Methods included from FileHandler
#find_file, included, #read_file
Methods included from DslAttributes
Methods included from DslOptions
Constructor Details
#initialize(str = nil, filename: nil, &block) ⇒ DSL
An instance of the DSL is initialized with a string or a block to evaluate to define Avro schema objects.
31 32 33 34 35 36 37 38 39 |
# File 'lib/avro/builder/dsl.rb', line 31 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 |
Instance Method Details
#as_schema ⇒ Object
82 83 84 |
# File 'lib/avro/builder/dsl.rb', line 82 def as_schema Avro::Schema.parse(to_json(validate: false)) end |
#enum(name = nil, *symbols, **options, &block) ⇒ Object
DSL methods for Types
56 57 58 |
# File 'lib/avro/builder/dsl.rb', line 56 def enum(name = nil, *symbols, **, &block) create_named_type(name, :enum, { symbols: symbols }.merge(), &block) end |
#fixed(name = nil, size = nil, options = {}, &block) ⇒ Object
60 61 62 63 |
# File 'lib/avro/builder/dsl.rb', line 60 def fixed(name = nil, size = nil, = {}, &block) size_option = size.is_a?(Hash) ? size : { size: size } create_named_type(name, :fixed, size_option.merge(), &block) end |
#import(name) ⇒ Object
Imports from the file with specified name fragment.
47 48 49 50 51 52 |
# File 'lib/avro/builder/dsl.rb', line 47 def import(name) previous_namespace = namespace result = eval_file(name) namespace(previous_namespace) result end |
#record(name = nil, options = {}, &block) ⇒ Object
Define an Avro schema record
42 43 44 |
# File 'lib/avro/builder/dsl.rb', line 42 def record(name = nil, = {}, &block) create_named_type(name, :record, , &block) end |
#to_h ⇒ Object
Return the last schema object processed as a Hash representing the Avro schema.
67 68 69 |
# File 'lib/avro/builder/dsl.rb', line 67 def to_h @last_object.to_h(SchemaSerializerReferenceState.new) end |
#to_json(validate: true, pretty: true) ⇒ Object
Return the last schema object processed as an Avro JSON schema
72 73 74 75 76 77 78 79 80 |
# File 'lib/avro/builder/dsl.rb', line 72 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 ⇒ Object
Override the type method from AnonymousTypes to store a reference to the last type defined.
88 89 90 |
# File 'lib/avro/builder/dsl.rb', line 88 def type(*) @last_object = super end |
#type_macro(name, type_object, options = {}) ⇒ Object
92 93 94 95 96 |
# File 'lib/avro/builder/dsl.rb', line 92 def type_macro(name, type_object, = {}) 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('.') cache.add_type_by_name(type_object, name, [:namespace] || namespace) end |