Class: PolicyMachineStorageAdapter::ActiveRecord::PolicyElement

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/policy_machine_storage_adapters/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/policy_machine_storage_adapters/active_record.rb', line 31

def method_missing(meth, *args, &block)
  methodize_extra_attributes_hash
  if respond_to?(meth)
    send(meth, *args)
  elsif meth.to_s[-1] == '='
    @extra_attributes_hash[meth.to_s] = args.first
    methodize_extra_attributes_hash
  else
    super
  end
end

Instance Attribute Details

#extra_attributes_hashObject

Returns the value of attribute extra_attributes_hash.



28
29
30
# File 'lib/policy_machine_storage_adapters/active_record.rb', line 28

def extra_attributes_hash
  @extra_attributes_hash
end

Instance Method Details

#methodize_extra_attributes_hashObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/policy_machine_storage_adapters/active_record.rb', line 48

def methodize_extra_attributes_hash
  @extra_attributes_hash = JSON.parse(self.extra_attributes, quirks_mode: true) if self.extra_attributes
  @extra_attributes_hash ||= {}
  @extra_attributes_hash.extract!(*self.class.column_names).each do |key, value|
    send("#{key}=", value) unless value.nil?
  end
  @extra_attributes_hash.each do |key, value|
    define_singleton_method key, lambda {@extra_attributes_hash[key.to_s]}
    define_singleton_method "#{key}=", lambda { | value | @extra_attributes_hash[key.to_s] = value }
  end
end

#respond_to_missing?(meth, *args) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/policy_machine_storage_adapters/active_record.rb', line 43

def respond_to_missing?(meth, *args)
  methodize_extra_attributes_hash unless @extra_attributes_hash
  @extra_attributes_hash[meth.to_s] || super
end

#serialize_extra_attributes_hashObject



60
61
62
63
# File 'lib/policy_machine_storage_adapters/active_record.rb', line 60

def serialize_extra_attributes_hash
  methodize_extra_attributes_hash unless @extra_attributes_hash
  self.extra_attributes = extra_attributes_hash.to_json
end