Class: Seahorse::ShapeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/seahorse/shape_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ ShapeBuilder

Returns a new instance of ShapeBuilder.



26
27
28
29
# File 'lib/seahorse/shape_builder.rb', line 26

def initialize(context)
  @context = context
  @desc = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(type, *args, &block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/seahorse/shape_builder.rb', line 49

def method_missing(type, *args, &block)
  if @@types[type]
    send_type(type, *args, &block)
  else
    super
  end
end

Class Method Details

.build_default_typesObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/seahorse/shape_builder.rb', line 5

def self.build_default_types
  hash = HashWithIndifferentAccess.new
  hash.update string: [StringType, nil],
              timestamp: [TimestampType, nil],
              integer: [IntegerType, nil],
              boolean: [BooleanType, nil],
              list: [ListType, nil],
              structure: [StructureType, nil]
  hash
end

.type(type, supertype = 'structure', &block) ⇒ Object



16
17
18
19
20
# File 'lib/seahorse/shape_builder.rb', line 16

def self.type(type, supertype = 'structure', &block)
  klass = Class.new(type_class_for(supertype))
  klass.type = type
  @@types[type] = [klass, block]
end

.type_class_for(type) ⇒ Object



22
23
24
# File 'lib/seahorse/shape_builder.rb', line 22

def self.type_class_for(type)
  @@types[type] ? @@types[type][0] : nil
end

Instance Method Details

#build(&block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/seahorse/shape_builder.rb', line 31

def build(&block)
  init_blocks = []
  init_blocks << block if block_given?

  # collect the init block for this type and all of its super types
  klass = @context.class
  while klass != Type
    if block = @@types[klass.type][1]
      init_blocks << block
    end
    klass = klass.superclass
  end

  init_blocks.reverse.each do |init_block|
    instance_eval(&init_block)
  end
end

#desc(text) ⇒ Object



57
58
59
# File 'lib/seahorse/shape_builder.rb', line 57

def desc(text)
  @desc = text
end

#model(model) ⇒ Object



61
62
63
# File 'lib/seahorse/shape_builder.rb', line 61

def model(model)
  @context.model = model
end