Class: Husky::DataSource::InMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/husky/data_source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ InMemory

Returns a new instance of InMemory.



25
26
27
# File 'lib/husky/data_source.rb', line 25

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



22
23
24
# File 'lib/husky/data_source.rb', line 22

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



23
24
25
# File 'lib/husky/data_source.rb', line 23

def id
  @id
end

Class Method Details

.has_many(association) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/husky/data_source.rb', line 9

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

Instance Method Details

#update_attributes(attrs) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/husky/data_source.rb', line 29

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