Module: Ohm::Composite::Macros

Defined in:
lib/ohm/composite.rb

Instance Method Summary collapse

Instance Method Details

#_composite(attrs) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/ohm/composite.rb', line 56

def _composite(attrs)
  attrs = attrs.sort

  method_name = Ohm::Composite.method_name(attrs)

  define_method(method_name) { Ohm::Composite.values(attrs.map { |att| send(att) }) }

  method_name
end

#composite_find(hash) ⇒ Object



40
41
42
43
44
45
# File 'lib/ohm/composite.rb', line 40

def composite_find(hash)
  keys = hash.keys.sort
  values = keys.map { |k| hash[k] }

  find(Ohm::Composite.method_name(keys) => Ohm::Composite.values(values))
end

#composite_index(attrs) ⇒ Object



29
30
31
# File 'lib/ohm/composite.rb', line 29

def composite_index(attrs)
  index(_composite(attrs))
end

#composite_unique(attrs) ⇒ Object



25
26
27
# File 'lib/ohm/composite.rb', line 25

def composite_unique(attrs)
  unique(_composite(attrs))
end

#composite_with(hash) ⇒ Object



33
34
35
36
37
38
# File 'lib/ohm/composite.rb', line 33

def composite_with(hash)
  keys = hash.keys.sort
  values = keys.map { |k| hash[k] }

  with(Ohm::Composite.method_name(keys), Ohm::Composite.values(values))
end

#composite_with_or_create(hash) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/ohm/composite.rb', line 47

def composite_with_or_create(hash)
  composite_with(hash) ||
    begin
      create(hash)
    rescue Ohm::UniqueIndexViolation
      composite_with(hash)
    end
end