Module: T12n

Extended by:
T12n
Included in:
T12n
Defined in:
lib/t12n.rb,
lib/t12n/version.rb,
lib/t12n/interface.rb,
lib/t12n/serializer.rb,
lib/t12n/schema_store.rb,
lib/t12n/schema_builder.rb

Defined Under Namespace

Classes: ArgumentError, Error, SchemaBuilder, SchemaStore, Serializer

Constant Summary collapse

Attr =
Ivo.new(:name, :serializer)
Schema =
Ivo.new(:name, :attrs, :context_blocks)
VERSION =
"0.3.0"
Interface =
Ivo.new(:schema_store) do
  def define_schema(name, &block)
    raise ArgumentError, "No block given" unless block

    raise ArgumentError, "Unexpected proc arity: #{block.arity}" if block.arity > 1

    builder = SchemaBuilder.new(self)

    if block.arity == 1
      block.call(builder)
    else
      builder.instance_exec(&block)
    end

    schema = Schema.new(name, builder.schema_attrs, builder.context_blocks)
    schema_store.save(schema)
    :ok
  end

  def fetch_schema(schema_name, context = nil)
    schema = schema_store.fetch(schema_name) or return

    if schema.context_blocks.any?
      builder = SchemaBuilder.new(self)
      schema.context_blocks.each { |block| builder.instance_exec(context, &block) }
      Schema.new(schema.name, (schema.attrs + builder.schema_attrs), [])
    else
      schema
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#start(schema_store: nil) ⇒ Object



18
19
20
21
# File 'lib/t12n.rb', line 18

def start(schema_store: nil)
  schema_store ||= SchemaStore.new
  Interface.new(schema_store)
end