Module: NoBrainer::Document::Aliases::ClassMethods

Defined in:
lib/no_brainer/document/aliases.rb

Instance Method Summary collapse

Instance Method Details

#field(attr, options = {}) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/no_brainer/document/aliases.rb', line 14

def field(attr, options={})
  if options[:store_as]
    self.alias_map[attr.to_s] = options[:store_as].to_s
    self.alias_reverse_map[options[:store_as].to_s] = attr.to_s
  end
  super
end

#lookup_field_alias(attr) ⇒ Object



33
34
35
# File 'lib/no_brainer/document/aliases.rb', line 33

def lookup_field_alias(attr)
  alias_map[attr.to_s] || attr
end

#persistable_key(k, options = {}) ⇒ Object



51
52
53
# File 'lib/no_brainer/document/aliases.rb', line 51

def persistable_key(k, options={})
  lookup_field_alias(super)
end

#remove_field(attr, options = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/no_brainer/document/aliases.rb', line 22

def remove_field(attr, options={})
  super

  self.alias_map.delete(attr.to_s)
  self.alias_reverse_map.delete(attr.to_s)
end

#reverse_lookup_field_alias(attr) ⇒ Object



29
30
31
# File 'lib/no_brainer/document/aliases.rb', line 29

def reverse_lookup_field_alias(attr)
  alias_reverse_map[attr.to_s] || attr
end

#with_fields_aliased(attrs) ⇒ Object



44
45
46
47
48
49
# File 'lib/no_brainer/document/aliases.rb', line 44

def with_fields_aliased(attrs)
  case attrs
  when Array then attrs.map { |k| lookup_field_alias(k) }
  when Hash  then Hash[attrs.map { |k,v| [lookup_field_alias(k), v] }]
  end
end

#with_fields_reverse_aliased(attrs) ⇒ Object



37
38
39
40
41
42
# File 'lib/no_brainer/document/aliases.rb', line 37

def with_fields_reverse_aliased(attrs)
  case attrs
  when Array then attrs.map { |k| reverse_lookup_field_alias(k) }
  when Hash  then Hash[attrs.map { |k,v| [reverse_lookup_field_alias(k), v] }]
  end
end