Class: SBF::Client::BaseEntity
- Inherits:
-
Object
- Object
- SBF::Client::BaseEntity
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
-
.allow_instantiation? ⇒ Boolean
Class method which retrieves the.
-
.attr_accessor(*vars) ⇒ Object
-
.attr_reader(*vars) ⇒ Object
Override for the ruby built-in attribute accessors.
-
.attr_writer(*vars) ⇒ Object
-
.collection_attributes ⇒ Object
-
.defined_attributes ⇒ Object
-
.entity_attr_accessor(attribute, full_class, partial_class = nil, optional = false) ⇒ Object
-
.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.
-
.entity_attr_writer(attribute, full_class, partial_class = nil, optional = false) ⇒ Object
-
.entity_attributes ⇒ Object
-
.entity_collection_attr_accessor(attribute, full_class, partial_class = nil, optional = false) ⇒ Object
-
.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.
-
.entity_collection_attr_writer(attribute, full_class, partial_class = nil, optional = false) ⇒ Object
-
.inherited(base) ⇒ Object
-
.multitype_attr_accessor(attribute, class_mappings, optional = false) ⇒ Object
-
.multitype_attr_reader(attribute, class_mappings, optional = false) ⇒ Object
Multitype Attribute accessors.
-
.multitype_attr_writer(attribute, class_mappings, optional = false) ⇒ Object
-
.multitype_collection_attr_accessor(attribute, class_mappings, optional = false) ⇒ Object
-
.multitype_collection_attr_reader(attribute, class_mappings, optional = false) ⇒ Object
Multitype Collection accessors.
-
.multitype_collection_attr_writer(attribute, class_mappings, optional = false) ⇒ Object
-
.optional_attributes ⇒ Object
Instance Method Summary
collapse
#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.
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)
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
#errors ⇒ Object
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
215
216
217
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 215
def self.allow_instantiation?
!@disallow_instantiation
end
|
.attr_accessor(*vars) ⇒ Object
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 244
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.
227
228
229
230
231
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 227
def self.attr_reader(*vars)
defined_attributes.merge(vars)
super(*vars)
add_boolean_methods(vars)
end
|
.attr_writer(*vars) ⇒ Object
233
234
235
236
237
238
239
240
241
242
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 233
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_attributes ⇒ Object
359
360
361
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 359
def self.collection_attributes
@collection_attributes ||= Set.new
end
|
.defined_attributes ⇒ Object
347
348
349
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 347
def self.defined_attributes
@defined_attributes ||= Set.new
end
|
.entity_attr_accessor(attribute, full_class, partial_class = nil, optional = false) ⇒ Object
296
297
298
299
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 296
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.
286
287
288
289
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 286
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
291
292
293
294
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 291
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_attributes ⇒ Object
355
356
357
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 355
def self.entity_attributes
@entity_attributes ||= Set.new
end
|
.entity_collection_attr_accessor(attribute, full_class, partial_class = nil, optional = false) ⇒ Object
341
342
343
344
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 341
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.
331
332
333
334
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 331
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
336
337
338
339
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 336
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
580
581
582
583
584
585
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 580
def self.inherited(base)
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
275
276
277
278
279
280
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 275
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>], […]
261
262
263
264
265
266
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 261
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
268
269
270
271
272
273
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 268
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
320
321
322
323
324
325
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 320
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>], […]
306
307
308
309
310
311
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 306
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
313
314
315
316
317
318
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 313
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_attributes ⇒ Object
351
352
353
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 351
def self.optional_attributes
@optional_attributes ||= Set.new
end
|
Instance Method Details
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 77
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
|
#error ⇒ Object
Attempt to return an ErrorEntity similar to or exactly like the original
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 590
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_hash ⇒ Object
104
105
106
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 104
def keys_hash
respond_to?(:id) && id ? {id: id} : {}
end
|
#model_name ⇒ Object
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_attributes ⇒ Object
Class method to retrieve the not_provided_attributes attribute
220
221
222
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 220
def not_provided_attributes
@not_provided_attributes ||= Set.new
end
|
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!
clear_changes_information
end
|
#reload_recursive ⇒ Object
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_hash ⇒ Object
Recursively converts an entity into a Hash
539
540
541
542
543
544
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
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 539
def to_hash
entity_hash = {}
self.class.defined_attributes.each { |key|
next if not_provided_attributes.include?(key)
value = send(key)
entity_hash[key] = if value.is_a?(BaseEntity)
value.to_hash
elsif value.is_a?(Array)
value.map { |element|
next element.to_hash if element.is_a?(BaseEntity)
next element
}
elsif value.nil? && self.class.collection_attributes.include?(key)
[]
else
value
end
}
entity_hash
end
|
#to_json(*a) ⇒ Object
Recursively convert an entity into json
576
577
578
|
# File 'lib/stbaldricks/entities/lib/base.rb', line 576
def to_json(*a)
to_hash.to_json(*a)
end
|