class OptAR::OAR

Dupe of ActiveRecord::Base providing necessary read functionalities

of it, while removing a lot of abstactions that it provides
to reduce memory and processing time utilisation

attributes : contains the requested attributes from AR klass_name : class name of original AR model klass_object : original AR object loaded

Constants

BLACKLISTED_ATTRIBUTES

Attributes

attributes[R]
klass_name[R]
klass_object[R]

Public Class Methods

from_json(string) click to toggle source

TODO: for handling JSON serialization

# File lib/opt_ar/oar.rb, line 46
def self.from_json(string); end
init_manual(attrs, klass, req_attributes) click to toggle source
# File lib/opt_ar/oar.rb, line 25
def self.init_manual(attrs, klass, req_attributes)
  oar = allocate
  attr_keys = oar.send(:fetch_attribute_keys, req_attributes, klass)
  attrs = attrs.slice(*attr_keys)
  oar.instance_variable_set('@attributes', attrs)
  oar.instance_variable_set('@klass_name', klass.name)
  oar.send(:transform_datetime_attributes, klass)
  oar
end
new(object, options = {}) click to toggle source
# File lib/opt_ar/oar.rb, line 18
def initialize(object, options = {})
  req_attributes = options[:attrs] || []
  assign_attributes(object, req_attributes)
  @klass_name = object.class.name
  # define_attr_readers
end

Public Instance Methods

as_json() click to toggle source
# File lib/opt_ar/oar.rb, line 35
def as_json
  {
    klass_key => attributes.as_json
  }
end
to_json() click to toggle source
# File lib/opt_ar/oar.rb, line 41
def to_json
  JSON.dump(as_json)
end

Private Instance Methods

assign_attributes(object, req_attributes) click to toggle source

By default only the primary key will be fetched Any required attribute has to be called out deliberately

# File lib/opt_ar/oar.rb, line 60
def assign_attributes(object, req_attributes)
  obj_attributes = object.attributes.symbolize_keys
  klass = object.class
  p_key = klass_primary_key(klass)

  unless obj_attributes[p_key]
    raise OptAR::Errors::MandatoryPrimaryKeyMissingError
  end

  attribute_keys = fetch_attribute_keys(req_attributes, klass)
  @attributes = obj_attributes.slice(*attribute_keys)
  transform_datetime_attributes(object.class)
end
fetch_attribute_keys(req_attributes, klass) click to toggle source
# File lib/opt_ar/oar.rb, line 74
def fetch_attribute_keys(req_attributes, klass)
  req_attributes + mandatory_attributes(klass) - skipped_attributes(klass)
end
mandatory_attributes(klass) click to toggle source
# File lib/opt_ar/oar.rb, line 78
def mandatory_attributes(klass)
  [klass_primary_key(klass)]
end
marshal_dump() click to toggle source
# File lib/opt_ar/oar.rb, line 110
def marshal_dump
  [@attributes, @klass_name]
end
marshal_load(result) click to toggle source
# File lib/opt_ar/oar.rb, line 114
def marshal_load(result)
  @attributes, @klass_name = result
end
skipped_attributes(klass) click to toggle source

Skips fetching attributes defined by BLACKLISTED_ATTRIBUTES constant

for the ActiveRecord class

Can be used to prevent exposing attributes having PII, password,

security related information to the outside world
# File lib/opt_ar/oar.rb, line 86
def skipped_attributes(klass)
  if klass.const_defined?(BLACKLISTED_ATTRIBUTES)
    attrs = klass.const_get(BLACKLISTED_ATTRIBUTES)
    if attrs.include? klass_primary_key(klass)
      raise OptAR::Errors::PrimaryKeyBlacklistedError
    end
    attrs
  else
    []
  end
end
transform_datetime_attributes(klass) click to toggle source
# File lib/opt_ar/oar.rb, line 98
def transform_datetime_attributes(klass)
  fields = transformable_fields(klass)
  return unless fields.present?
  fields.each do |field|
    val = attributes[field]
    raise OptAR::Errors::TimeTypeExpectedError unless val.is_a?(Time)
    attributes[field] = val.to_i
  end
ensure
  attributes.freeze
end