module Perimeter::Repository::ClassMethods
Public Instance Methods
backend()
click to toggle source
Convenience Wrapper
# File lib/perimeter/repository.rb, line 33 def backend backend_class end
backend_class(*args)
click to toggle source
–––––––––––––––––––––– Class name definitions ––––––––––––––––––––––
# File lib/perimeter/repository.rb, line 22 def backend_class(*args) @backend_class = args.first unless args.empty? @backend_class || default_backend_class end
entity_class(*args)
click to toggle source
# File lib/perimeter/repository.rb, line 27 def entity_class(*args) @entity_class = args.first unless args.empty? @entity_class || default_entity_class end
Private Instance Methods
attributes_to_record(attributes, options = {})
click to toggle source
–––––––––– Conversion ––––––––––
# File lib/perimeter/repository.rb, line 71 def attributes_to_record(attributes, options = {}) options.symbolize_keys! if id = options[:add_id] attributes[:id] = id end entity = entity_class.new attributes entity_to_record entity end
default_backend_class()
click to toggle source
––––––––––––––––––– Default Class names –––––––––––––––––––
# File lib/perimeter/repository.rb, line 43 def default_backend_class backend_class_name = name + '::Backend' backend_class_name.constantize rescue NameError => exception if exception.message.to_s == "uninitialized constant #{backend_class_name}" raise NameError, %{Repository "#{name}" expects the Backend "#{backend_class_name}" to be defined.} else raise exception end end
default_entity_class()
click to toggle source
# File lib/perimeter/repository.rb, line 55 def default_entity_class entity_class_name = name.singularize entity_class_name.constantize rescue NameError => exception if exception.message.to_s == "uninitialized constant #{entity_class_name}" raise NameError, %{Repository "#{name}" expects the Entity "#{entity_class_name}" to be defined.} else raise exception end end
entity_to_record(entity, options = {})
click to toggle source
# File lib/perimeter/repository.rb, line 80 def entity_to_record(entity, options = {}) options.symbolize_keys! attributes = entity.attributes if options[:strip_id] attributes.delete :id attributes.delete 'id' end backend.new attributes end
record_to_entity(record)
click to toggle source
# File lib/perimeter/repository.rb, line 95 def record_to_entity(record) return if record.blank? begin entity = entity_class.new record.attributes rescue ArgumentError => exception if exception.message.to_s == 'wrong number of arguments(1 for 0)' raise ArgumentError, %{The Class "#{entity_class}" appears not to be an Entity, because the initializer does not accept one argument. Did you "include Perimeter::Entity"?} else raise exception end end if record.errors.present? record.errors.each { |attribute, message| entity.errors.add attribute, message } end entity.id = record.id run_hook :after_conversion, entity, record entity end
records_to_entities(records)
click to toggle source
# File lib/perimeter/repository.rb, line 90 def records_to_entities(records) return [] if records.blank? Array(records).map { |record| record_to_entity(record) } end