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.



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

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.has_many(association) ⇒ Object



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

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



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

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