Sha256: 6f099ab86fca03a170c00c0b73afe43cb378a1cdb944000c6d84601fb8eb0fbb

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

module Lolita
  module Configuration
    class Columns

      include Enumerable
      include ObservedArray
      include Lolita::Builder
      
      attr_accessor :list
      attr_reader :dbi
      
      def initialize(list,dbi=nil)
        @list=list
        @dbi=dbi || list.dbi
        @columns=[]
      end
      
      def each
        @columns.each_with_index{|column,index|
          if column.is_a?(Lolita::Configuration::Column)
            yield column
          else
            raise "Any column must be Lolita::Configuratin::Column object instead of #{column.class}."
          end
        }
      end

      def generate!
        @columns.clear
        @dbi.fields.each_with_index{|field,index|
          unless Lolita::Configuration::Helper.tehnical_field?(field,@dbi)
            @columns << Lolita::Configuration::Column.new(@dbi,field)
          end
        }
      end

      def add attributes={},&block
        if block_given?
          @columns<<build_element(&block)
        else
          @columns<<build_element(attributes)
        end
        self
      end

      private

      def collection_variable
        @columns
      end
      
      def build_element(column=nil,&block)
        if column.is_a?(Lolita::Configuration::Column)
          column
        elsif column.is_a?(Proc)
          Lolita::Configuration::Column.new(@dbi,&column)
        elsif block_given?
          Lolita::Configuration::Column.new(@dbi,&block)
        elsif [Symbol,String,Hash].include?(column.class)
          Lolita::Configuration::Column.new(@dbi,column)
        else
          raise ArgumentError.new("Column can not be defined with #{column.class}.")
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lolita-3.1.17 lib/lolita/configuration/columns.rb
lolita-3.1.16 lib/lolita/configuration/columns.rb
lolita-3.1.15 lib/lolita/configuration/columns.rb