Sha256: c21f52c0bd27d54338d963964ee3f30b96d623e62ec6a4aee4331b92089f9d95

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# Axe this
module Husky

  module DataSource

    class InMemory

      class << self

        def has_many(association)
          define_method association do
            singular_association = association.to_s[0..-2].to_sym
            klass_id = "#{self.class.name.split('::')[1].downcase}_id".to_sym
            repo = Husky::Repo::Registry.for(singular_association)
            set = repo.all.select { |k,v| v.send(klass_id) == self.id }
            receiving_class = repo.class.to_s[0..-5].split('::').reduce(Module, :const_get)
            Husky::DataSource::MemoryRelation.new(set, self, receiving_class)
          end
        end

      end

      attr_reader :attributes
      attr_accessor :id

      def initialize(attributes)
        @attributes = attributes
      end

      def update_attributes(attrs)
        @attributes.each do |key, value|
          if attrs.key?(key)
            @attributes[key] = attrs[key]
          end
        end
      end

    end

    class MemoryRelation
      include Enumerable

      attr_reader :set, :caller, :receiving_class

      def initialize(set, caller, receiving_class)
        @set             = set
        @caller          = caller
        @receiving_class = receiving_class
      end

      def each
        set.map { |item| yield item }
      end

      def new(attributes)
        attrs = attributes
        attrs[caller_id] = caller.id
        receiving_class.new(attrs)
      end

      def caller_id
        "#{caller.class.to_s.split('::').last.downcase}_id".to_sym
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
husky-0.5.9 lib/husky/data_source.rb
husky-0.5.8 lib/husky/data_source.rb
husky-0.5.7 lib/husky/data_source.rb