Sha256: 96bbad80d0257ffc734c7b726d84a2f5d3fcb5118c90746bfc0952eb4568859e
Contents?: true
Size: 1.23 KB
Versions: 1
Compression:
Stored size: 1.23 KB
Contents
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 set = Husky::Repo::Registry.for(singular_association).all.select { |k,v| v.send(klass_id) == self.id } Husky::DataSource::MemoryRelation.new(set, self) 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, :receiver def initialize(set, caller, receiver) @set = set @caller = caller @receiver = receiver end def each set.map { |item| yield item } end def create(attributes) receiver.create({ "#{caller.class.downcase}_id".to_sym => caller.id }) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
husky-0.2.1 | lib/husky/data_source.rb |