Class: CommonRepositoryModel::Collection

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/common_repository_model/collection.rb,
lib/generators/common_repository_model/collection/collection_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.has_members(method_name, options = {}) ⇒ Object



38
39
40
41
# File 'lib/common_repository_model/collection.rb', line 38

def self.has_members(method_name, options = {})
  membership_registry.has_members << method_name
  has_many(method_name, options)
end

.is_member_of(method_name, options = {}) ⇒ Object

Creates the :break_relation_with_<macro_name> method which is useful for managing both the ActiveFedora association and relationship.

NOTE: I believe this is actually masking an ActiveFedora bug, at some point, I would imagine that the :break_relation_with method would be deprecated and ultimately be an alias for object.<association_name>.delete(obj1,obj2)



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/common_repository_model/collection.rb', line 50

def self.is_member_of(method_name, options = {})
  membership_registry.is_member_of << method_name
  define_method "break_relation_with_#{method_name}" do |*args|
    send(method_name).delete(*args)
    args.each do |obj|
      remove_relationship(options[:property], obj)
      remove_relationship(:is_member_of, obj)
    end
  end
  has_and_belongs_to_many(method_name, options)
end

.membership_registryObject



30
31
32
# File 'lib/common_repository_model/collection.rb', line 30

def self.membership_registry
  @membership_registry ||= MembershipRegistry.new
end

Instance Method Details

#areaObject



99
100
101
102
103
104
105
# File 'lib/common_repository_model/collection.rb', line 99

def area
  if is_root?
    __area
  else
    parent_areas.first
  end
end

#area=(an_area) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/common_repository_model/collection.rb', line 91

def area=(an_area)
  if is_root?
    self.__area = an_area
  else
    self.__area = parent_areas.first
  end
end

#create_collectionObject



5
6
7
8
9
10
# File 'lib/generators/common_repository_model/collection/collection_generator.rb', line 5

def create_collection
  template(
    'collection.rb.erb',
    File.join('app/repository_models/', "#{file_name}.rb")
  )
end

#create_service_specObject



11
12
13
14
15
16
# File 'lib/generators/common_repository_model/collection/collection_generator.rb', line 11

def create_service_spec
  template(
    'collection_spec.rb.erb',
    File.join('spec/repository_models/', "#{file_name}_spec.rb")
  )
end

#find_or_build_data_for_given_slot_names(slot_names) ⇒ Object



133
134
135
136
137
# File 'lib/common_repository_model/collection.rb', line 133

def find_or_build_data_for_given_slot_names(slot_names)
  slot_names.collect { |name|
    data.detect { |d| d.slot_name == name } || data.build(slot_name: name)
  }
end

#is_root?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/common_repository_model/collection.rb', line 83

def is_root?
  parent_collections.size == 0
end

#membership_registryObject



34
35
36
# File 'lib/common_repository_model/collection.rb', line 34

def membership_registry
  self.class.membership_registry
end

#parent_areasObject



87
88
89
# File 'lib/common_repository_model/collection.rb', line 87

def parent_areas
  parent_collections.collect(&:__area).uniq
end