Sha256: b8bfbef548a98a2fcdd8cfabcd8b0f43d48659d2737210db96ac463f0a591e6a

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'active_support/core_ext/hash/indifferent_access'
module ActiveHash

  module ARApi

    module Init

      attr_reader :associations

      def initialize(attributes = {})
        filter_associations(HashWithIndifferentAccess.new(attributes))
        @attributes.dup.merge(@associations.dup).each do |key, value|
          send "#{key}=", value
        end
      end

      private

      def filter_associations(attributes)
        permitted_params = [*self.class.send(:attribute_names), *self.class.send(:association_names)]
        rejected_params = attributes.reject{ |p| permitted_params.include? p.to_sym}
        raise "Rejected params: #{rejected_params} for #{self.class.name}" if !rejected_params.empty?
        @attributes = attributes.select do |k, v|
          self.class.send(:attribute_names).include? k.to_sym
        end
        @attributes = self.class.send(:attribute_template).merge(@attributes)
        @associations = attributes.select do |k, v|
          self.class.send(:association_names).include? k.to_sym
        end
        @associations = self.class.send(:association_template).merge(associations)

      end

    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_mocker-1.1.23 lib/active_hash/init.rb