Sha256: c86ceaa22bc5bffb7bda390fae2fb9ea3eed297bea8acbebbe25fa12ee796af5

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module ROM

  # @api private
  class ModelBuilder
    attr_reader :options, :const_name, :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
      @const_name = options[:name]
    end

    def define_const
      Object.const_set(const_name, klass)
    end

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

    class PORO < ModelBuilder

      def define_class(header)
        @klass = Class.new

        attributes = header.keys

        @klass.send(:attr_accessor, *attributes)

        ivar_list = attributes.map { |name| "@#{name}" }.join(", ")
        sym_list = attributes.map { |name| ":#{name}" }.join(", ")

        @klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def initialize(params)
        #{ivar_list} = params.values_at(#{sym_list})
            end
        RUBY

        self
      end

    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rom-0.4.1 lib/rom/model_builder.rb
rom-0.4.0 lib/rom/model_builder.rb
rom-0.3.1 lib/rom/model_builder.rb
rom-0.3.0 lib/rom/model_builder.rb