Sha256: 26083fad42c0ccb365d957be5889029e0b90150f4dbff88c3f36a812069c90ac

Contents?: true

Size: 964 Bytes

Versions: 2

Compression:

Stored size: 964 Bytes

Contents

require 'dry-initializer'

module Artisanal::Model
  require_relative 'dsl'

  class Builder < Module
    attr_reader :config

    def initialize(options={})
      @config = Config.new(options)
    end

    def included(base)
      base.extend Dry::Initializer[undefined: config.undefined?]
      base.extend Artisanal::Model::DSL

      # Make attributes mutable
      define_writers if config.writable?

      # Store artisanal model config
      base.artisanal_model.config = config
    end

    protected

    def define_writers
      # Add writers to all attributes
      config.defaults[:writer] = true unless config.defaults.has_key? :writer

      # Define mass-assignment method
      define_method(:assign_attributes) do |attrs|
        attrs = artisanal_model.symbolize(attrs)

        (attrs.keys & artisanal_model.schema.keys).each do |key|
          public_send("#{key}=", attrs[key]) if respond_to? "#{key}="
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
artisanal-model-0.2.1 lib/artisanal/model/builder.rb
artisanal-model-0.2.0 lib/artisanal/model/builder.rb