Sha256: a80f69663eef9b83dc4ddcadc6a8d2fd4c8229804b6bdcfca49fcd4d4c61fc13

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module ROM

  # @api private
  class ModelBuilder
    attr_reader :options, :const_name, :namespace, :klass

    def self.[](type)
      case type
      when :poro then PORO
      else
        raise ArgumentError, "#{type.inspect} is not a supported model type"
      end
    end

    def self.call(*args)
      new(*args).call
    end

    def initialize(options = {})
      @options = options

      if options[:name]
        split = options[:name].split('::')

        @const_name = split.last

        @namespace =
          if split.size > 1
            Inflecto.constantize((split-[const_name]).join('::'))
          else
            Object
          end
      end
    end

    def define_const
      namespace.const_set(const_name, klass)
    end

    def call(header)
      define_class(header)
      define_const if const_name
      @klass
    end

    class PORO < ModelBuilder

      def define_class(header)
        @klass = Class.new

        attributes = header.keys

        @klass.send(:attr_reader, *attributes)

        @klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
          def initialize(params)
            #{attributes.map { |name| "@#{name} = params[:#{name}]" }.join("\n")}
          end
        RUBY

        self
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.4.2 lib/rom/model_builder.rb