Sha256: 9c4cfff1fdcd84c0f3f171076fae351de11ec4e63e115c4288eff80cd884e10f

Contents?: true

Size: 1.2 KB

Versions: 13

Compression:

Stored size: 1.2 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 #maybe could just check if responds_to method? How AR works?
        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

13 entries across 13 versions & 1 rubygems

Version Path
active_mocker-1.2 lib/active_hash/init.rb
active_mocker-1.2.pre.11 lib/active_hash/init.rb
active_mocker-1.2.pre.10 lib/active_hash/init.rb
active_mocker-1.2.pre.9 lib/active_hash/init.rb
active_mocker-1.2.pre.8 lib/active_hash/init.rb
active_mocker-1.2.pre.7 lib/active_hash/init.rb
active_mocker-1.2.pre.6 lib/active_hash/init.rb
active_mocker-1.2.pre.5 lib/active_hash/init.rb
active_mocker-1.2.pre.4 lib/active_hash/init.rb
active_mocker-1.2.pre.3 lib/active_hash/init.rb
active_mocker-1.2.pre.2 lib/active_hash/init.rb
active_mocker-1.2.pre.1 lib/active_hash/init.rb
active_mocker-1.2.pre lib/active_hash/init.rb