Class: Plumb::Schema

Inherits:
Object
  • Object
show all
Includes:
Composable
Defined in:
lib/plumb/schema.rb

Defined Under Namespace

Classes: Field, SymbolAccessHash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Composable

#>>, #[], #as_node, #build, #check, #children, #defer, #generate, included, #invalid, #invoke, #match, #metadata, #not, #pipeline, #policy, #static, #to_s, #transform, #value, #|

Methods included from Callable

#parse, #resolve

Constructor Details

#initialize(hash = Types::Hash, &block) ⇒ Schema

Returns a new instance of Schema.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/plumb/schema.rb', line 24

def initialize(hash = Types::Hash, &block)
  @pipeline = Types::Any
  @before = Types::Any
  @after = Types::Any
  @_hash = hash
  @fields = @_hash._schema.each.with_object(SymbolAccessHash.new({})) do |(k, v), memo|
    memo[k] = Field.new(k, v)
  end

  setup(&block) if block_given?

  finish
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



22
23
24
# File 'lib/plumb/schema.rb', line 22

def fields
  @fields
end

Class Method Details

.wrap(sch = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/plumb/schema.rb', line 10

def self.wrap(sch = nil, &block)
  raise ArgumentError, 'expected a block or a schema' if sch.nil? && !block_given?

  if sch
    raise ArgumentError, 'expected a Composable' unless sch.is_a?(Composable)

    return sch
  end

  new(&block)
end

Instance Method Details

#&(other) ⇒ Object



92
93
94
# File 'lib/plumb/schema.rb', line 92

def &(other)
  self.class.new(_hash & other._hash)
end

#+(other) ⇒ Object



88
89
90
# File 'lib/plumb/schema.rb', line 88

def +(other)
  self.class.new(_hash + other._hash)
end

#after(callable = nil, &block) ⇒ Object



47
48
49
50
# File 'lib/plumb/schema.rb', line 47

def after(callable = nil, &block)
  @after >>= callable || block
  self
end

#before(callable = nil, &block) ⇒ Object



42
43
44
45
# File 'lib/plumb/schema.rb', line 42

def before(callable = nil, &block)
  @before >>= callable || block
  self
end

#call(result) ⇒ Object



56
57
58
# File 'lib/plumb/schema.rb', line 56

def call(result)
  @pipeline.call(result)
end

#field(key, type = nil, &block) ⇒ Object



78
79
80
81
# File 'lib/plumb/schema.rb', line 78

def field(key, type = nil, &block)
  key = Key.new(key.to_sym)
  @fields[key] = Field.new(key, type, &block)
end

#field?(key, type = nil, &block) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/plumb/schema.rb', line 83

def field?(key, type = nil, &block)
  key = Key.new(key.to_sym, optional: true)
  @fields[key] = Field.new(key, type, &block)
end

#inspectObject



38
39
40
# File 'lib/plumb/schema.rb', line 38

def inspect
  "#{self.class}#{fields.keys.inspect}"
end

#merge(other = nil, &block) ⇒ Object



96
97
98
99
# File 'lib/plumb/schema.rb', line 96

def merge(other = nil, &block)
  other = self.class.wrap(other, &block)
  self + other
end

#to_json_schemaObject



52
53
54
# File 'lib/plumb/schema.rb', line 52

def to_json_schema
  _hash.to_json_schema(root: true)
end