Class: Ars::Familiar::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ars/familiar/base.rb

Direct Known Subclasses

Mongoid

Instance Method Summary collapse

Instance Method Details

#attributes_to_defineObject



23
24
25
# File 'lib/ars/familiar/base.rb', line 23

def attributes_to_define
  @attributes_to_define || attributes_to_define!
end

#extract_options(**opts) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ars/familiar/base.rb', line 31

def extract_options(**opts)
  class_map = map_classes(opts)

  only = opts.fetch(:only, :pass).entries.each_with_object(class_map) {|val, hash|  hash.store(*val) }.keys
  class_map

  filter = proc do |relation|
    name = relation[:name]

    only == :pass || only.include?(name)
  end

  [filter, class_map]
end

#idObject



19
20
21
# File 'lib/ars/familiar/base.rb', line 19

def id
  @id || id!
end

#map_classes(**opts) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/ars/familiar/base.rb', line 46

def map_classes(**opts)
  class_base  = opts.fetch(:class_base, nil)
  class_name  = opts.fetch(:class_name, false)
  class_names = opts.fetch(:class_names, {})

  Hash.new {|hash, key| hash[key] = [class_base, key].constantize }.
    tap {|this| class_name && this.default = class_name}.
    merge(class_names)
end

#relationships_to_define(**opts) ⇒ Object



27
28
29
# File 'lib/ars/familiar/base.rb', line 27

def relationships_to_define(**opts)
  @relationships_to_define || relationships_to_define!(**opts)
end

#scribe(dump) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ars/familiar/base.rb', line 5

def scribe(dump)
  if dump
    %i{ attributes_to_define relationships_to_define primary_key id }.each do |attr|
      const_set "@#{attr}", dump[attr]
    end
  end
  {
    attributes_to_define: @attributes_to_define,
    relationships_to_define: @relationships_to_define,
    primary_key: @primary_key,
    id: @id
  }
end