Module: IMW::Metadata::DSL

Included in:
Dataset
Defined in:
lib/imw/metadata/dsl.rb

Overview

A module which defines a DSL that can be used to define metadata for an object.

Instance Method Summary collapse

Instance Method Details

#field(name, options = {}) ⇒ Object



71
72
73
# File 'lib/imw/metadata/dsl.rb', line 71

def field name, options={}
  accumulate_field Field.new(options.merge(:name => name))
end

#has_many(name, options = {}, &block) ⇒ Object



81
82
83
84
85
# File 'lib/imw/metadata/dsl.rb', line 81

def has_many name, options={}, &block
  new_field_accumulator!
  yield
  accumulate_field Field.new(options.merge(:name => name, :has_many => last_field_accumulator!))
end

#has_one(name, options = {}, &block) ⇒ Object



75
76
77
78
79
# File 'lib/imw/metadata/dsl.rb', line 75

def has_one name, options={}, &block
  new_field_accumulator!
  yield
  accumulate_field Field.new(options.merge(:name => name, :has_one => last_field_accumulator!))
end

#metadata(arg = nil, options = {}, &block) ⇒ IMW::Metadata

When called without a block return this object’s metadata.


#=> { '/path/to/file' => [...], '/path/to/other/file' => [...], ... }

When called with a block, accumulate schema and fields into this object’s metadata

 do

  schema "/path/to/file" do
    # ...
  end

  schema "/path/to/other/file" do
    # ...
  end
end

Returns:

See Also:

  • IMW::Metadata::DSL.[IMW[IMW::Metadata[IMW::Metadata::Schema]
  • IMW::Metadata::DSL.[IMW[IMW::Metadata[IMW::Metadata::Field]


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/imw/metadata/dsl.rb', line 51

def  arg=nil, options={}, &block
  case arg
  when Hash 
    @metadata ||= Metadata.new(arg, options)
  when nil
    @metadata ||= Metadata.new nil, options
  else
    @metadata ||= Metadata.load(arg, options)
  end
  @metadata.base = options[:base] if options[:base]
  return @metadata unless block_given?
  yield
end

#open(uri, options = {}, &block) ⇒ IMW::Resource

Open a new resource at the given URI.

If this dataset has metadata and it describes the resource then configure the resource to understand its schema..

The schema property passed via the options hash will override this.

Parameters:

Returns:

See Also:



20
21
22
23
# File 'lib/imw/metadata/dsl.rb', line 20

def open uri, options={}, &block
  schema_options = (options[:schema].nil? &&  && .describe?(uri)) ? {:schema => [uri]} : {}
  IMW.open(uri, options.merge(schema_options), &block)
end

#open!(uri, options = {}, &block) ⇒ Object



25
26
27
# File 'lib/imw/metadata/dsl.rb', line 25

def open! uri, options={}, &block
  self.open(uri, options.merge(:mode => 'w'), &block)
end

#schema(resource, options = {}, &block) ⇒ Object



65
66
67
68
69
# File 'lib/imw/metadata/dsl.rb', line 65

def schema resource, options={}, &block
  new_field_accumulator!
  yield
  [resource] = Schema.new(last_field_accumulator!)
end