Sha256: 004994676a7cbcd2b34d66f55ed8b4a546f270cd61442e9de8f19968453a04d2

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

module Lolita
  module Configuration
    module Factory
      class Field
        # There are three ways to add field.
        # *<tt>first</tt> - Pass name and type
        #   Field.add(dbi,"name","type")
        # *<tt>second</tt> - Pass it through hash
        #   Field.add(dbi,:name => "name", :type => "type")
        # *<tt>third</tt> - Pass dbi_field
        #   Field.add(dbi,:dbi_field => dbi.fields.first)
        def self.add(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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lolita-3.2.0.rc.7 lib/lolita/configuration/factory/field.rb
lolita-3.1.18 lib/lolita/configuration/factory/field.rb
lolita-3.2.0.rc.6 lib/lolita/configuration/factory/field.rb
lolita-3.2.0.rc.5 lib/lolita/configuration/factory/field.rb
lolita-3.2.0.rc.4 lib/lolita/configuration/factory/field.rb
lolita-3.2.0.rc.3 lib/lolita/configuration/factory/field.rb