Class: OptAR::OAR

Inherits:
Object
  • Object
show all
Includes:
MethodFinderHelper
Defined in:
lib/opt_ar/oar.rb

Overview

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

Constant Summary collapse

BLACKLISTED_ATTRIBUTES =
'BLACKLISTED_ATTRIBUTES'.freeze

Constants included from MethodFinderHelper

MethodFinderHelper::DATE_TIME_CONST_KEY, MethodFinderHelper::DEFAULT_DATE_TIME_ATTRIBUTES, MethodFinderHelper::ID_STRING, MethodFinderHelper::WARN_MSG

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ OAR

Returns a new instance of OAR.



18
19
20
21
22
23
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OptAR::MethodFinderHelper

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



14
15
16
# File 'lib/opt_ar/oar.rb', line 14

def attributes
  @attributes
end

#klass_nameObject (readonly)

Returns the value of attribute klass_name.



14
15
16
# File 'lib/opt_ar/oar.rb', line 14

def klass_name
  @klass_name
end

#klass_objectObject (readonly)

Returns the value of attribute klass_object.



14
15
16
# File 'lib/opt_ar/oar.rb', line 14

def klass_object
  @klass_object
end

Class Method Details

.from_json(string) ⇒ Object

TODO: for handling JSON serialization



46
# File 'lib/opt_ar/oar.rb', line 46

def self.from_json(string); end

.init_manual(attrs, klass, req_attributes) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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

Instance Method Details

#as_jsonObject



35
36
37
38
39
# File 'lib/opt_ar/oar.rb', line 35

def as_json
  {
    klass_key => attributes.as_json
  }
end

#to_jsonObject



41
42
43
# File 'lib/opt_ar/oar.rb', line 41

def to_json
  JSON.dump(as_json)
end