Class: Leafy::Schema

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/leafy/schema.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = []) ⇒ Schema

Returns a new instance of Schema.



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

def initialize(fields = [])
  @fields = fields.map { |field| Leafy::Field.new(field) }
end

Class Method Details

.dump(schema) ⇒ Object



37
38
39
# File 'lib/leafy/schema.rb', line 37

def self.dump(schema)
  JSON.dump(schema.serializable_hash)
end

.load(data) ⇒ Object



41
42
43
# File 'lib/leafy/schema.rb', line 41

def self.load(data)
  Schema.new(JSON.parse(data))
end

Instance Method Details

#[](identifier) ⇒ Object



24
25
26
# File 'lib/leafy/schema.rb', line 24

def [](identifier)
  @fields.find { |f| f.id == identifier }
end

#eachObject



8
9
10
11
12
13
14
# File 'lib/leafy/schema.rb', line 8

def each
  if block_given?
    @fields.each { |i| yield i }
  else
    @fields.each
  end
end

#idsObject



20
21
22
# File 'lib/leafy/schema.rb', line 20

def ids
  @fields.map(&:id)
end

#push(field) ⇒ Object Also known as: <<

Raises:

  • (ArgumentError)


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

def push(field)
  raise(ArgumentError, "is not a field") unless field.is_a?(Leafy::Field)
  @fields << field
end

#serializable_hashObject



33
34
35
# File 'lib/leafy/schema.rb', line 33

def serializable_hash
  @fields.map(&:serializable_hash)
end