Class: Fixtury::Schema

Inherits:
Object
  • Object
show all
Includes:
SchemaNode
Defined in:
lib/fixtury/schema.rb

Overview

A Fixtury::SchemaNode implementation that represents a top-level schema or a namespace within a schema.

Constant Summary

Constants included from SchemaNode

Fixtury::SchemaNode::VALID_NODE_NAME

Instance Method Summary collapse

Methods included from SchemaNode

#acts_like_fixtury_schema_node?, #add_child, #apply_options!, #first_ancestor?, #get, #get!, #inspect, #isolation_key, #structure

Constructor Details

#initialize(name: "", **options) ⇒ Schema

a new top-level schema.

Parameters:

  • name (String) (defaults to: "")

    the name of the schema, defaults to “” to represent



12
13
14
# File 'lib/fixtury/schema.rb', line 12

def initialize(name: "", **options)
  super(name: name, **options)
end

Instance Method Details

#acts_like_fixtury_schema?Boolean

Object#acts_like? adherence.

Returns:

  • (Boolean)


21
22
23
# File 'lib/fixtury/schema.rb', line 21

def acts_like_fixtury_schema?
  true
end

#define(&block) ⇒ Fixtury::Schema

Open up self for child definitions.

Parameters:

  • block (Proc)

    The block to be executed in the context of the schema.

Returns:



28
29
30
31
# File 'lib/fixtury/schema.rb', line 28

def define(&block)
  instance_eval(&block)
  self
end

#fixture(relative_name, **options, &block) ⇒ Fixtury::Definition

Create a fixture definition at the given relative name. If the name is already used, a Fixtury::Errors::AlreadyDefinedError will be raised.

Parameters:

  • relative_name (String)

    The relative name of the fixture.

  • options (Hash)

    Additional options for the fixture.

  • block (Proc)

    The block representing the build function of the fixture.

Returns:



68
69
70
71
72
73
74
75
# File 'lib/fixtury/schema.rb', line 68

def fixture(relative_name, **options, &block)
  ::Fixtury::Definition.new(
    name: relative_name,
    parent: self,
    **options,
    &block
  )
end

#namespace(relative_name, **options, &block) ⇒ Fixtury::Schema

Create a child schema at the given relative name. If a child by the name already exists it will be reopened as long as it’s a fixtury schema.

Parameters:

  • relative_name (String)

    The relative name of the child schema.

  • options (Hash)

    Additional options for the child schema, applied after instantiation.

  • block (Proc)

    A block of code to be executed in the context of the child schema.

Returns:

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fixtury/schema.rb', line 47

def namespace(relative_name, **options, &block)
  child = get("./#{relative_name}")

  if child && !child.acts_like?(:fixtury_schema)
    raise Errors::AlreadyDefinedError, child.pathname
  end

  child ||= self.class.new(name: relative_name, parent: self)
  child.apply_options!(options)
  child.instance_eval(&block) if block_given?
  child
end

#resetObject



16
17
18
# File 'lib/fixtury/schema.rb', line 16

def reset
  children.clear
end

#schema_node_typeString

Returns “schema” if top-level, otherwise returns “namespace”.

Returns:

  • (String)

    the type of the schema node.



35
36
37
# File 'lib/fixtury/schema.rb', line 35

def schema_node_type
  first_ancestor? ? "schema" : "namespace"
end