Class: SBF::Client::BaseEntity

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::AttributeAssignment, ActiveModel::Conversion, ActiveModel::Dirty, EntityResponseConcern
Defined in:
lib/stbaldricks/entities/lib/base.rb

Direct Known Subclasses

Address, AlternateShippingAddress, Api::Response, Campaign::Totals, Challenger::Totals, Diagnosis, Donation::EmployerMatched, Donation::EmployerMatching, Donation::RecognitionCard, Donation::RecognitionCard::Recipient, Donation::SBFGeneralFund, Donation::Tribute, EmailAddress, Event::Activity, Event::Agreement, Event::Agreement::Question, Event::CoachTracking, Event::CoachTracking::CoachingInteractions, Event::CoachTracking::Plaque, Event::CoachTracking::Proceeds, Event::Contacts, Event::Contacts::Contact, Event::Photos, Event::Totals, Event::Venue::Social, EventApplication::Requestor, FullOrganization::ContactName, Fund::Photos, Fund::Totals, Fund::YearlyTotals, Fundraiser::Photos, Fundraiser::Policies, Fundraiser::Rankings, Fundraiser::Rankings::Ranking, Fundraiser::Totals, FundraisingPage, GeoLocation, Grant::FullFundingType, Grant::FullResearcher, Grant::PartialFundingType, Grant::PartialResearcher, Guardian, InternationalPartner::Link, InternationalPartner::WebPage, Kid::Relationship, Kid::Relationship::Permissions, KidURL, Location, MatchingGift::Company::Contact, MatchingGift::Company::EmployeeEligibility, MatchingGift::Company::GiftRatio, MatchingGift::Company::Procedures, MatchingGift::Company::Requirements, MatchingGift::Company::Restrictions, Memorial::Photos, Memorial::Totals, Memorial::Tribute, Message::Recipient, NamePiecesBase, NotImplementedObject, OptOutSettings, Organization::Addresses, Organization::EmailAddresses, Organization::PhoneNumbers, Organization::Totals, Page::Content, Participant::Photos, Participant::Policies, Participant::Rankings, Participant::Rankings::Ranking, Participant::Roles, Participant::Roles::Barber::License, Participant::Roles::Barber::Salon, Participant::Roles::Organizer::Policies, Participant::Roles::Organizer::PreviousOrganization, Participant::Roles::Role, Participant::ShaveSchedule, Participant::Totals, Payment::Details, Permissions, PermissionsAtEvent, Person::Addresses, Person::EmailAddresses, Person::HowAffected, Person::Occupation, Person::Occupation::EducationDetails, Person::Occupation::FinanceDetails, Person::Occupation::MilitaryDetails, Person::PhoneNumbers, Person::Policies, Person::Totals, Phone, Photos, Search::Campaign, Search::Event, Search::Fund, Search::Fundraiser, Search::Kid, Search::Memorial, Search::Participant, Search::Team, ShaveSchedule::ShaveScheduleAssignment, ShaveSchedule::TimeSelectionPermissions, Summary::Totals, Team::Photos, Team::Rankings, Team::Rankings::Ranking, Team::Totals, ThirdPartyMedia, TopLevelEntity, TreatmentStatus, Venue

Constant Summary collapse

ELSE =
->(_) { true }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EntityResponseConcern

#add_errors, #data, #error?, #errors?, #errors_http_code=, #http_code, #success?

Constructor Details

#initialize(data = {}, clear_changes = false) ⇒ BaseEntity

Returns a new instance of BaseEntity.

Raises:



32
33
34
35
36
37
38
39
40
41
# File 'lib/stbaldricks/entities/lib/base.rb', line 32

def initialize(data = {}, clear_changes = false)
  # If disallow instantiation has been set, raise an error if someone is trying to call initilaize
  raise SBF::Client::Error, 'Initialize is not valid on a base object. Use the full or partial version' unless self.class.allow_instantiation?

  super()

  @errors = SBF::Client::Entity::Errors.new(self)
  initialize_attributes(data)
  reload_recursive if clear_changes
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



21
22
23
# File 'lib/stbaldricks/entities/lib/base.rb', line 21

def errors
  @errors
end

Class Method Details

.allow_instantiation?Boolean

Class method which retrieves the

Returns:



221
222
223
# File 'lib/stbaldricks/entities/lib/base.rb', line 221

def self.allow_instantiation?
  !@disallow_instantiation
end

.attr_accessor(*vars) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/stbaldricks/entities/lib/base.rb', line 250

def self.attr_accessor(*vars)
  defined_attributes.merge(vars)
  super(*vars)

  vars.each do |attribute|
    define_attribute_methods attribute
    define_changing_attr_methods attribute, false, true
  end

  add_boolean_methods(vars, true)
end

.attr_reader(*vars) ⇒ Object

Override for the ruby built-in attribute accessors. Creates a list of attributes that can be accessed at will and adds some helper methods.



233
234
235
236
237
# File 'lib/stbaldricks/entities/lib/base.rb', line 233

def self.attr_reader(*vars)
  defined_attributes.merge(vars)
  super(*vars)
  add_boolean_methods(vars)
end

.attr_writer(*vars) ⇒ Object



239
240
241
242
243
244
245
246
247
248
# File 'lib/stbaldricks/entities/lib/base.rb', line 239

def self.attr_writer(*vars)
  defined_attributes.merge(vars)

  vars.each do |attribute|
    define_attribute_methods attribute
    define_changing_attr_methods attribute
  end

  add_boolean_methods(vars, true)
end

.collection_attributesObject



365
366
367
# File 'lib/stbaldricks/entities/lib/base.rb', line 365

def self.collection_attributes
  @collection_attributes ||= Set.new
end

.defined_attributesObject



353
354
355
# File 'lib/stbaldricks/entities/lib/base.rb', line 353

def self.defined_attributes
  @defined_attributes ||= Set.new
end

.entity_attr_accessor(attribute, full_class, partial_class = nil, optional = false) ⇒ Object



302
303
304
305
# File 'lib/stbaldricks/entities/lib/base.rb', line 302

def self.entity_attr_accessor(attribute, full_class, partial_class = nil, optional = false)
  mapping_for_single_class = [[ELSE, full_class, partial_class]]
  multitype_attr_accessor(attribute, mapping_for_single_class, optional)
end

.entity_attr_reader(attribute, full_class, partial_class = nil, optional = false) ⇒ Object

Entity attr accessors are simpler to define/easier to read but they really just create a type mapping for the given inputs and then function the same as the multitype attribute accessors do.



292
293
294
295
# File 'lib/stbaldricks/entities/lib/base.rb', line 292

def self.entity_attr_reader(attribute, full_class, partial_class = nil, optional = false)
  mapping_for_single_class = [[ELSE, full_class, partial_class]]
  multitype_attr_reader(attribute, mapping_for_single_class, optional)
end

.entity_attr_writer(attribute, full_class, partial_class = nil, optional = false) ⇒ Object



297
298
299
300
# File 'lib/stbaldricks/entities/lib/base.rb', line 297

def self.entity_attr_writer(attribute, full_class, partial_class = nil, optional = false)
  mapping_for_single_class = [[ELSE, full_class, partial_class]]
  multitype_attr_writer(attribute, mapping_for_single_class, optional)
end

.entity_attributesObject



361
362
363
# File 'lib/stbaldricks/entities/lib/base.rb', line 361

def self.entity_attributes
  @entity_attributes ||= Set.new
end

.entity_collection_attr_accessor(attribute, full_class, partial_class = nil, optional = false) ⇒ Object



347
348
349
350
# File 'lib/stbaldricks/entities/lib/base.rb', line 347

def self.entity_collection_attr_accessor(attribute, full_class, partial_class = nil, optional = false)
  mapping_for_single_class = [[ELSE, full_class, partial_class]]
  multitype_collection_attr_accessor(attribute, mapping_for_single_class, optional)
end

.entity_collection_attr_reader(attribute, full_class, partial_class = nil, optional = false) ⇒ Object

Entity attr accessors are simpler to define/easier to read but they really just create a type mapping for the given inputs and then function the same as the multitype attribute accessors do.



337
338
339
340
# File 'lib/stbaldricks/entities/lib/base.rb', line 337

def self.entity_collection_attr_reader(attribute, full_class, partial_class = nil, optional = false)
  mapping_for_single_class = [[ELSE, full_class, partial_class]]
  multitype_collection_attr_reader(attribute, mapping_for_single_class, optional)
end

.entity_collection_attr_writer(attribute, full_class, partial_class = nil, optional = false) ⇒ Object



342
343
344
345
# File 'lib/stbaldricks/entities/lib/base.rb', line 342

def self.entity_collection_attr_writer(attribute, full_class, partial_class = nil, optional = false)
  mapping_for_single_class = [[ELSE, full_class, partial_class]]
  multitype_collection_attr_writer(attribute, mapping_for_single_class, optional)
end

.inherited(base) ⇒ Object



586
587
588
589
590
591
# File 'lib/stbaldricks/entities/lib/base.rb', line 586

def self.inherited(base)
  # Add all of our attributes to the class that is inheriting us.
  base.defined_attributes.merge(defined_attributes) unless defined_attributes.empty?
  base.optional_attributes.merge(optional_attributes) unless optional_attributes.empty?
  base.entity_attributes.merge(entity_attributes) unless entity_attributes.empty?
end

.multitype_attr_accessor(attribute, class_mappings, optional = false) ⇒ Object



281
282
283
284
285
286
# File 'lib/stbaldricks/entities/lib/base.rb', line 281

def self.multitype_attr_accessor(attribute, class_mappings, optional = false)
  entity_attributes << attribute
  optional_attributes << attribute if optional
  attr_accessor(attribute)
  add_multitype_setter_method(attribute, class_mappings)
end

.multitype_attr_reader(attribute, class_mappings, optional = false) ⇒ Object

Multitype Attribute accessors. These methods take an array of type to class mappings. Expected form is:

[<lamda method which evals to true if this type should be used>, <full class>, <partial class>], […]


267
268
269
270
271
272
# File 'lib/stbaldricks/entities/lib/base.rb', line 267

def self.multitype_attr_reader(attribute, class_mappings, optional = false)
  entity_attributes << attribute
  optional_attributes << attribute if optional
  attr_reader(attribute)
  add_multitype_setter_method(attribute, class_mappings, true)
end

.multitype_attr_writer(attribute, class_mappings, optional = false) ⇒ Object



274
275
276
277
278
279
# File 'lib/stbaldricks/entities/lib/base.rb', line 274

def self.multitype_attr_writer(attribute, class_mappings, optional = false)
  entity_attributes << attribute
  optional_attributes << attribute if optional
  attr_writer(attribute)
  add_multitype_setter_method(attribute, class_mappings)
end

.multitype_collection_attr_accessor(attribute, class_mappings, optional = false) ⇒ Object



326
327
328
329
330
331
# File 'lib/stbaldricks/entities/lib/base.rb', line 326

def self.multitype_collection_attr_accessor(attribute, class_mappings, optional = false)
  collection_attributes << attribute
  optional_attributes << attribute if optional
  attr_accessor(attribute)
  add_multitype_collection_setter_method(attribute, class_mappings)
end

.multitype_collection_attr_reader(attribute, class_mappings, optional = false) ⇒ Object

Multitype Collection accessors. These methods take an array of type to class mappings. Expected form is:

[<lamda method which evals to true if this type should be used>, <full class>, <partial class>], […]


312
313
314
315
316
317
# File 'lib/stbaldricks/entities/lib/base.rb', line 312

def self.multitype_collection_attr_reader(attribute, class_mappings, optional = false)
  collection_attributes << attribute
  optional_attributes << attribute if optional
  attr_reader(attribute)
  add_multitype_collection_setter_method(attribute, class_mappings, true)
end

.multitype_collection_attr_writer(attribute, class_mappings, optional = false) ⇒ Object



319
320
321
322
323
324
# File 'lib/stbaldricks/entities/lib/base.rb', line 319

def self.multitype_collection_attr_writer(attribute, class_mappings, optional = false)
  collection_attributes << attribute
  optional_attributes << attribute if optional
  attr_writer(attribute)
  add_multitype_collection_setter_method(attribute, class_mappings)
end

.optional_attributesObject



357
358
359
# File 'lib/stbaldricks/entities/lib/base.rb', line 357

def self.optional_attributes
  @optional_attributes ||= Set.new
end

Instance Method Details

#destroyed?Boolean

Returns:



70
71
72
# File 'lib/stbaldricks/entities/lib/base.rb', line 70

def destroyed?
  @destroyed
end

#dirty_data(with_keys = false) ⇒ Hash

Returns a hash of changed data for the entity and its sub-entities

Parameters:

  • with_keys (Boolean) (defaults to: false)

    when true, include the keys of the current entity. (sub-entity keys will always be included if they are present)

Returns:

  • (Hash)

    the changed data



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/stbaldricks/entities/lib/base.rb', line 83

def dirty_data(with_keys = false)
  data = {}
  changes.each { |k, arr| data[k.to_sym] = arr[1] }

  instance_variables.each do |var|
    attribute_symbol = var.to_s.delete('@').to_sym
    attribute = instance_variable_get(var)
    if attribute.is_a?(BaseEntity)
      if attribute.dirty_data(true).empty?
        next unless changed.map(&:to_sym).include?(attribute_symbol)

        data.merge!(attribute_symbol => attribute.keys_hash)
      else
        data.merge!(attribute_symbol => attribute.dirty_data(true))
      end
    elsif attribute.is_a?(Array)
      next unless attribute.all? { |e| e.is_a?(BaseEntity) } && attribute.any? { |e| !e.dirty_data(true).empty? }

      data.merge!(attribute_symbol => attribute)
    end
  end

  return data if data.empty?

  with_keys ? keys_hash.merge(data) : data
end

#errorObject

Attempt to return an ErrorEntity similar to or exactly like the original



596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/stbaldricks/entities/lib/base.rb', line 596

def error
  log_deprecated('error', caller)
  return nil if single_active_model_error.nil?

  details = single_active_model_error.find { |x| x.is_a?(String) } if single_active_model_error.is_a?(Array)
  details ||= single_active_model_error.is_a?(String) ? single_active_model_error : nil
  field = single_active_model_error.is_a?(Array) ? single_active_model_error.find { |x| x.is_a?(Symbol) && x != :base } : nil

  fields = errors.keys.reject { |k| k == :base }.empty? ? nil : errors.keys.reject { |k| k == :base }.map(&:to_s)
  details = details["#{errors.type}: ".length..(details.length - 1)] if details.start_with?("#{errors.type}: ")
  details = details["#{field}: ".length..(details.length - 1)] if details.start_with?("#{field}: ")

  SBF::Client::ErrorEntity.new(code: errors.code, type: errors.type, details: details, errors: fields)
end

#keys_hashObject



110
111
112
# File 'lib/stbaldricks/entities/lib/base.rb', line 110

def keys_hash
  respond_to?(:id) && id ? {id: id} : {}
end

#model_nameObject

Overridden from ActiveModel::Naming to remove namespace and Full/Partial



44
45
46
# File 'lib/stbaldricks/entities/lib/base.rb', line 44

def model_name
  @_model_name ||= ActiveModel::Name.new(self, SBF::Client, self.class.name.gsub(/::(Full|Partial)/, '::'))
end

#not_provided_attributesObject

Class method to retrieve the not_provided_attributes attribute



226
227
228
# File 'lib/stbaldricks/entities/lib/base.rb', line 226

def not_provided_attributes
  @not_provided_attributes ||= Set.new
end

#persisted?Boolean

Returns:



48
49
50
# File 'lib/stbaldricks/entities/lib/base.rb', line 48

def persisted?
  true
end

#reload!Object



52
53
54
55
# File 'lib/stbaldricks/entities/lib/base.rb', line 52

def reload!
  # get the values from the persistence layer
  clear_changes_information
end

#reload_recursiveObject



57
58
59
60
61
62
63
64
# File 'lib/stbaldricks/entities/lib/base.rb', line 57

def reload_recursive
  instance_variables.each do |var|
    attribute = instance_variable_get(var)
    attribute.reload_recursive if attribute.is_a?(BaseEntity)
    attribute.each { |a| a.reload_recursive if a.is_a?(BaseEntity) } if attribute.is_a?(Array)
  end
  reload!
end

#rollback!Object



66
67
68
# File 'lib/stbaldricks/entities/lib/base.rb', line 66

def rollback!
  restore_attributes
end

#to_hashObject

Recursively converts an entity into a Hash



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/stbaldricks/entities/lib/base.rb', line 545

def to_hash
  entity_hash = {}
  self.class.defined_attributes.each { |key|
    # If the method was not retrieved, do not include it in the hash
    next if not_provided_attributes.include?(key)

    # Get the value
    value = send(key)

    # If the value is an Entity, call to_hash on it (recursively)
    entity_hash[key] = if value.is_a?(BaseEntity)
                         value.to_hash

                       # If the value is an Array, need to try to call to_hash on each item
                       elsif value.is_a?(Array)
                         value.map { |element|
                           next element.to_hash if element.is_a?(BaseEntity)

                           next element
                         }

                       # Collections should return empty array rather than nil
                       elsif value.nil? && self.class.collection_attributes.include?(key)
                         []

                       # Just set the value
                       else
                         value

                       end
  }

  # Return the hash
  entity_hash
end

#to_json(*a) ⇒ Object

Recursively convert an entity into json



582
583
584
# File 'lib/stbaldricks/entities/lib/base.rb', line 582

def to_json(*a)
  to_hash.to_json(*a)
end

#to_partialObject



74
75
76
77
78
# File 'lib/stbaldricks/entities/lib/base.rb', line 74

def to_partial
  base_class = self.class.name.gsub(/(.*::)(Full|Partial)(.*)/, '\1')
  base_name = self.class.name.gsub(/(.*::)(Full|Partial)(.*)/, '\3')
  "#{base_class}Partial#{base_name}".to_class.new(to_hash)
end