Class: Lolita::Configuration::Factory::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/configuration/factory/field.rb

Class Method Summary collapse

Class Method Details

.create(dbi, *args, &block) ⇒ Object Also known as: add

There are three ways to add field. *first - Pass name and type

Field.add(dbi,"name","type")

*second - Pass it through hash

Field.add(dbi,:name => "name", :type => "type")

*third - Pass dbi_field

Field.add(dbi,:dbi_field => dbi.fields.first)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lolita/configuration/factory/field.rb', line 13

def create(dbi,*args,&block)

  options = args ? args.extract_options! : {}
  dbi_field = options[:dbi_field]
  name = args[0] || options[:name] || (dbi_field ? dbi_field.name : nil)
  dbi_field ||= dbi.field_by_name(name)
  dbi_field ||= dbi.field_by_association(name)
  association ||= detect_association(dbi,name)

  type = args[1] || options[:type] ||
    (association ? :array : nil ) ||
    (dbi_field ? dbi_field.type : nil) ||
    :string
  options[:dbi_field] = dbi_field
  if !name || !type
    raise Lolita::FieldTypeError, "type not defined. Set is as second argument or as :dbi_field where value is Adapter::[ORM]::Field object."
  else
    field_class(type).new(dbi,name,type,options,&block)
  end

end

.detect_association(dbi, name) ⇒ Object



37
38
39
# File 'lib/lolita/configuration/factory/field.rb', line 37

def detect_association(dbi,name)
  dbi.associations[name.to_s]
end

.field_class(name) ⇒ Object



41
42
43
# File 'lib/lolita/configuration/factory/field.rb', line 41

def field_class(name)
  ("Lolita::Configuration::Field::"+name.to_s.camelize).constantize
end