Class: Entitlements::Models::Person

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements.rb,
lib/entitlements/models/person.rb

Constant Summary collapse

C =
::Contracts

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contracts::Core

common, extended, included

Constructor Details

#initialize(uid:, attributes: {}) ⇒ Person

Returns a new instance of Person.



19
20
21
22
# File 'lib/entitlements/models/person.rb', line 19

def initialize(uid:, attributes: {})
  @uid = uid
  setup_attributes(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Grab any methods that have been defined by extras and dispatch them to the appropriate backend. The first argument sent to the method is a reference to this object (self). Any arguments passed by the caller are sent thereafter. For now this ignores blocks (maybe figure this out later?).

Arguments and return varies.

Raises:

  • (NoMethodError)


29
30
31
32
33
34
35
36
37
# File 'lib/entitlements/models/person.rb', line 29

def method_missing(m, *args, &block)
  if method_class_ref = Entitlements.person_extra_methods[m]
    return method_class_ref.send(m, self, *args)
  end

  # :nocov:
  raise NoMethodError, "No method '#{m}' exists for Entitlements::Models::Person or any registered extras."
  # :nocov:
end

Instance Attribute Details

#uidObject (readonly)

Returns the value of attribute uid.



9
10
11
# File 'lib/entitlements/models/person.rb', line 9

def uid
  @uid
end

Instance Method Details

#[](attribute) ⇒ Object



45
46
47
# File 'lib/entitlements/models/person.rb', line 45

def [](attribute)
  outward(@current_attributes.fetch(attribute))
end

#[]=(attribute, val) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/entitlements/models/person.rb', line 66

def []=(attribute, val)
  @touched_attributes.add(attribute)

  if val.nil? && @original_attributes[attribute].nil?
    @original_attributes.delete(attribute)
    @current_attributes.delete(attribute)
    return
  end

  @original_attributes[attribute] ||= nil
  if val.nil? || val.is_a?(String)
    @current_attributes[attribute] = val
  elsif val.is_a?(Set)
    @current_attributes[attribute] = val.dup
  else
    @current_attributes[attribute] = Set.new(val)
  end
end

#add(attribute, val) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
# File 'lib/entitlements/models/person.rb', line 107

def add(attribute, val)
  @touched_attributes.add(attribute)
  ca = @current_attributes.fetch(attribute) # Raises if not found
  raise ArgumentError, "Called add() on attribute that is a #{ca.class}" unless ca.is_a?(Set)
  ca.add(val)
  nil
end

#attribute_changesObject



91
92
93
94
95
96
97
98
# File 'lib/entitlements/models/person.rb', line 91

def attribute_changes
  @current_attributes
    .select { |k, _| @touched_attributes.member?(k) }
    .reject { |k, v| @original_attributes[k] == v }
    .reject { |k, v| (@original_attributes[k].nil? || @original_attributes[k] == Set.new) && (v.nil? || v == Set.new) }
    .map { |k, _| [k, self[k]] }
    .to_h
end

#original(attribute) ⇒ Object



55
56
57
# File 'lib/entitlements/models/person.rb', line 55

def original(attribute)
  outward(@original_attributes[attribute])
end