Class: FastAttributes::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_attributes/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
10
11
# File 'lib/fast_attributes/builder.rb', line 6

def initialize(klass, options = {})
  @klass      = klass
  @options    = options
  @attributes = []
  @methods    = Module.new
end

Instance Method Details

#attribute(*attributes, type, options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fast_attributes/builder.rb', line 13

def attribute(*attributes, type, options)
  unless options.is_a?(Hash)
    (attributes ||= []) << type
    type = options
    options = {}
  end

  unless FastAttributes.type_exists?(type)
    raise UnsupportedTypeError, %(Unsupported attribute type "#{type.inspect}")
  end

  @attributes << [attributes, type, options || {}]
end

#compile!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fast_attributes/builder.rb', line 27

def compile!
  compile_getter
  compile_setter
  set_defaults

  if @options[:initialize]
    compile_initialize
  end

  if @options[:attributes]
    compile_attributes(@options[:attributes])
  end

  include_methods
end