Class: Knish::Collection

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/knish/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent_config) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
# File 'lib/knish/collection.rb', line 5

def initialize(name, parent_config)
  super([])
  @name = name
  @config = CollectionConfig.new(parent_config, name)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/knish/collection.rb', line 3

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/knish/collection.rb', line 3

def name
  @name
end

Instance Method Details

#add(model) ⇒ Object Also known as: <<



11
12
13
14
# File 'lib/knish/collection.rb', line 11

def add(model)
  configure(model)
  push(model)
end

#configure(model) ⇒ Object



36
37
38
39
# File 'lib/knish/collection.rb', line 36

def configure(model)
  model.config = config.member_config(model.config)
  model
end

#loadObject



22
23
24
25
26
27
28
29
30
# File 'lib/knish/collection.rb', line 22

def load
  clear
  config.generic_model_configs.each do |c|
    push(
      Member.new(config, c).loaded_model
    )
  end
  self
end

#next_idObject



32
33
34
# File 'lib/knish/collection.rb', line 32

def next_id
  config.next_id + select{|m| !m.persisted? }.size
end

#saveObject



18
19
20
# File 'lib/knish/collection.rb', line 18

def save
  each(&:save)
end