Class: SBF::Client::BaseEntity

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

Direct Known Subclasses

Address, Api::Response, Campaign::Totals, Challenger::Totals, Diagnosis, 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, EventApplication::Requestor, FullInstitution, FullOrganization::ContactName, Fund::Photos, Fund::Totals, Fund::YearlyTotals, Fundraiser::Location, Fundraiser::Photos, Fundraiser::Policies, Fundraiser::Rankings, Fundraiser::Rankings::Ranking, Fundraiser::Totals, Fundraiser::Venue, 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, NamePieces, NotImplementedObject, OptOutSettings, Organization::Addresses, Organization::EmailAddresses, Organization::PhoneNumbers, Page::Content, PartialInstitution, 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, 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, Venue::Social

Constant Summary collapse

ELSE =
->(_) { true }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ BaseEntity

Returns a new instance of BaseEntity.

Raises:



29
30
31
32
33
34
35
36
37
# File 'lib/stbaldricks/entities/lib/base.rb', line 29

def initialize(data = {})
  # 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 = ActiveModel::Errors.new(self)
  self.attributes = data
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



18
19
20
# File 'lib/stbaldricks/entities/lib/base.rb', line 18

def errors
  @errors
end

Class Method Details

.allow_instantiation?Boolean

Class method which retrieves the

Returns:



157
158
159
# File 'lib/stbaldricks/entities/lib/base.rb', line 157

def self.allow_instantiation?
  !@disallow_instantiation
end

.attr_accessor(*vars) ⇒ Object



181
182
183
184
185
# File 'lib/stbaldricks/entities/lib/base.rb', line 181

def self.attr_accessor(*vars)
  attributes.merge(vars)
  super(*vars)
  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.



169
170
171
172
173
# File 'lib/stbaldricks/entities/lib/base.rb', line 169

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

.attr_writer(*vars) ⇒ Object



175
176
177
178
179
# File 'lib/stbaldricks/entities/lib/base.rb', line 175

def self.attr_writer(*vars)
  attributes.merge(vars)
  super(*vars)
  add_boolean_methods(vars, true)
end

.attributesObject



278
279
280
# File 'lib/stbaldricks/entities/lib/base.rb', line 278

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

.collection_attributesObject



290
291
292
# File 'lib/stbaldricks/entities/lib/base.rb', line 290

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

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



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

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.



217
218
219
220
# File 'lib/stbaldricks/entities/lib/base.rb', line 217

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



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

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



286
287
288
# File 'lib/stbaldricks/entities/lib/base.rb', line 286

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

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



272
273
274
275
# File 'lib/stbaldricks/entities/lib/base.rb', line 272

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.



262
263
264
265
# File 'lib/stbaldricks/entities/lib/base.rb', line 262

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



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

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



497
498
499
500
501
502
# File 'lib/stbaldricks/entities/lib/base.rb', line 497

def self.inherited(base)
  # Add all of our attributes to the class that is inheriting us.
  base.attributes.merge(attributes) unless 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



206
207
208
209
210
211
# File 'lib/stbaldricks/entities/lib/base.rb', line 206

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>], […]


192
193
194
195
196
197
# File 'lib/stbaldricks/entities/lib/base.rb', line 192

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



199
200
201
202
203
204
# File 'lib/stbaldricks/entities/lib/base.rb', line 199

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



251
252
253
254
255
256
# File 'lib/stbaldricks/entities/lib/base.rb', line 251

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>], […]


237
238
239
240
241
242
# File 'lib/stbaldricks/entities/lib/base.rb', line 237

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



244
245
246
247
248
249
# File 'lib/stbaldricks/entities/lib/base.rb', line 244

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



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

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

Instance Method Details

#add_errors(error) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/stbaldricks/entities/lib/base.rb', line 67

def add_errors(error)
  errors.clear
  count = error.fields.length
  error.fields.each do |field|
    field = field.gsub('_id', '') # Hacky, since API returns DB field that errored instead of View Field
    errors.add(field, error.details) if count > 1
    errors.add(:base, "#{field}: #{error.details}") if count == 1
  end
  errors.add(:base, "#{error.type}: #{error.details}") if count == 0
end

#attributes=(data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stbaldricks/entities/lib/base.rb', line 48

def attributes=(data)
  data ||= {}
  data.each do |key, value|
    # Use the setter if one is available, otherwise set the variable directly
    setter = "#{key}=".to_sym
    if respond_to?(setter, true)
      send(setter, value)

    else
      instance_variable_set("@#{key}".to_sym, value)

    end
  end

  # For each attribute that may be optional, call mark_attribute_not_provided
  # if the data set does not contain a key with the optional attribute's name
  self.class.optional_attributes.each { |name| mark_attribute_not_provided(name) unless data.key?(name) }
end

#model_nameObject

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



40
41
42
# File 'lib/stbaldricks/entities/lib/base.rb', line 40

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



162
163
164
# File 'lib/stbaldricks/entities/lib/base.rb', line 162

def not_provided_attributes
  @not_provided_attributes ||= Set.new
end

#persisted?Boolean

Returns:



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

def persisted?
  true
end

#to_hashObject

Recursively converts an entity into a Hash



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/stbaldricks/entities/lib/base.rb', line 454

def to_hash
  entity_hash = {}
  self.class.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)
    if value.is_a?(BaseEntity)
      entity_hash[key] = value.to_hash

    # If the value is an Array, need to try to call to_hash on each item
    elsif value.is_a?(Array)
      entity_hash[key] = value.map { |element|
        if element.is_a?(BaseEntity)
          next element.to_hash
        else
          next element
        end
      }

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

    # Just set the value
    else
      entity_hash[key] = value

    end
  }

  # Return the hash
  entity_hash
end

#to_json(*a) ⇒ Object

Recursively convert an entity into json



493
494
495
# File 'lib/stbaldricks/entities/lib/base.rb', line 493

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