class BlockScore::Collection::Member
Member
of a {Collection} class
Attributes
@!attribute [r] instance member instance methods are delegated to
@return [BlockScore::Base]
@api private
@!attribute [r] parent collection parent the collectino conditionally updates
@return [BlockScore::Base]
@api private
Public Class Methods
Initialize a new member
@param parent [BlockScore::Base] parent resource @param instance [BlockScore::Base] member instance
@return [undefined]
@api private
# File lib/blockscore/collection/member.rb, line 13 def initialize(parent, instance) @instance = instance @parent = parent super(instance) end
Public Instance Methods
Save parent, set parent id, and save instance
@example
# saves both unsaved person and unsaved question_set person = Person.new(attributes) question_set = QuestionSet.new Member.new(person, question_set).save
@return return value of instance ‘#save`
@api public
# File lib/blockscore/collection/member.rb, line 31 def save save_parent send(:"#{parent_name}_id=", parent.id) result = instance.save ids.push(instance.id) unless ids.include?(instance.id) result end
Private Instance Methods
ids that belong to associated parent resource
@return [Array<String>]
@api private
# File lib/blockscore/collection/member.rb, line 95 def ids parent.attributes.fetch(:"#{Util.to_plural(instance.class.resource)}", []) end
Name of parent resource
@example
self.parent_name # => 'person'
@return [String]
@api private
# File lib/blockscore/collection/member.rb, line 49 def parent_name parent.class.resource end
Check if parent is saved
@return [Boolean]
@api private
# File lib/blockscore/collection/member.rb, line 68 def parent_saved? parent.id end
Save parent if it hasn’t already been saved
@return return of parent.save if previously unsaved @return nil otherwise
@api private
# File lib/blockscore/collection/member.rb, line 59 def save_parent parent.save unless parent_saved? end