lib/jimmy/link.rb in jimmy-0.4.1 vs lib/jimmy/link.rb in jimmy-0.4.2

- old
+ new

@@ -1,5 +1,7 @@ +require 'forwardable' + module Jimmy class Link < Hash attr_reader :schema def initialize(schema, rel, href) @@ -22,10 +24,30 @@ def compile merge schemas.map { |k, v| [(k ? "#{k}Schema" : 'schema'), v.compile] }.to_h end + def schema_creator + @schema_creator ||= SchemaCreator.new(self) + end + + class SchemaCreator < Hash + include SchemaCreation::Referencing + extend Forwardable + delegate [:schema, :domain] => :@link + + def initialize(link) + @link = link + end + + def parent + schema + end + + SchemaCreation.apply_to(self) { |schema, prefix| @link.schemas[prefix] = schema } + end + class DSL attr_reader :link def initialize(link) @link = link @@ -45,15 +67,23 @@ def evaluate(&block) instance_exec &block end - def schema(type = :object, prefix = nil, &block) - link.schemas[prefix] = Schema.new(type, link.schema).tap { |s| s.setup &block } + def schema(*args, prefix: nil, **opts, &block) + if args.empty? && opts.any? + args = opts.shift + type = args.shift + else + type = args.shift || :object + end + args.unshift type, prefix + args << opts if opts.any? + link.schema_creator.__send__ *args, &block end - def target_schema(type = :object, &block) - schema type, :target, &block + def target_schema(*args, **opts, &block) + schema *args, **opts.merge(prefix: :target), &block end def set(**values) values.each { |k, v| link[k.to_s] = v } end