Module: NoBrainer::Document::MissingAttributes

Extended by:
ActiveSupport::Concern
Defined in:
lib/no_brainer/document/missing_attributes.rb

Instance Method Summary collapse

Instance Method Details

#_read_attribute(name) ⇒ Object



53
54
55
56
# File 'lib/no_brainer/document/missing_attributes.rb', line 53

def _read_attribute(name)
  assert_access_field(name)
  super
end

#_write_attribute(name, value) ⇒ Object



58
59
60
# File 'lib/no_brainer/document/missing_attributes.rb', line 58

def _write_attribute(name, value)
  super.tap { clear_missing_field(name) }
end

#assert_access_field(name, msg = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/no_brainer/document/missing_attributes.rb', line 43

def assert_access_field(name, msg=nil)
  if missing_field?(name)
    method = @missing_attributes.keys.first
    msg ||= "The attribute `#{name}' is not accessible,"
    msg += " add `:#{name}' to pluck()" if method == :pluck
    msg += " remove `:#{name}' from without()" if method == :without
    raise NoBrainer::Error::MissingAttribute.new(msg)
  end
end

#assign_attributes(attrs, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/no_brainer/document/missing_attributes.rb', line 8

def assign_attributes(attrs, options={})
  # there is one and only one key :pluck or :without to missing_attributes
  if options[:missing_attributes]
    # TODO XXX this whole thing is gross.
    # if @missing_attributes is already there, it's because we are doing a
    # incremental reload. clear_missing_field will do the work of recognizing
    # which fields are here or not.
    @missing_attributes ||= options[:missing_attributes]

    assert_access_field(self.class.pk_name, "The primary key is not accessible. Use .raw or")
    assert_access_field(:_type, "The subclass type is not accessible. Use .raw or") if self.class.is_polymorphic
  end

  attrs.keys.each { |attr| clear_missing_field(attr) } if @missing_attributes && options[:from_db]

  super
end

#clear_missing_field(name) ⇒ Object



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

def clear_missing_field(name)
  return unless @missing_attributes
  @cleared_missing_fields ||= {}
  @cleared_missing_fields[name.to_s] = true
end

#missing_field?(name) ⇒ Boolean

Returns:



26
27
28
29
30
31
32
33
34
35
# File 'lib/no_brainer/document/missing_attributes.rb', line 26

def missing_field?(name)
  return false unless @missing_attributes
  name = name.to_s
  return false if @cleared_missing_fields.try(:[], name)
  if @missing_attributes[:pluck]
    !@missing_attributes[:pluck][name]
  else
    !!@missing_attributes[:without][name]
  end
end

#write_attribute(attr, value) ⇒ Object



4
5
6
# File 'lib/no_brainer/document/missing_attributes.rb', line 4

def write_attribute(attr, value)
  super.tap { clear_missing_field(attr) }
end