Class: Ricordami::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/ricordami/relationship.rb

Constant Summary collapse

SUPPORTED_TYPES =
[:references_many, :references_one, :referenced_in]
MANDATORY_ARGS =
[:other, :self]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options = {}) ⇒ Relationship

Returns a new instance of Relationship.

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ricordami/relationship.rb', line 8

def initialize(type, options = {})
  options.assert_valid_keys(:other, :as, :self, :alias, :dependent)
  raise TypeNotSupported.new(type.to_s) unless SUPPORTED_TYPES.include?(type)
  missing = find_missing_args(options)
  raise MissingMandatoryArgs.new(missing.map(&:to_s).join(", ")) unless missing.empty?
  if options[:dependent] && ![:delete, :nullify].include?(options[:dependent])
    raise OptionValueInvalid.new(options[:dependent].to_s)
  end
  @name = options[:as] || options[:other]
  @type = type
  @object_kind = options[:other].to_s.singularize.to_sym
  @self_kind = options[:self].to_s.singularize.to_sym
  @alias = options[:alias] || options[:self]
  @dependent = options[:dependent]
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias.



6
7
8
# File 'lib/ricordami/relationship.rb', line 6

def alias
  @alias
end

#dependentObject (readonly)

Returns the value of attribute dependent.



6
7
8
# File 'lib/ricordami/relationship.rb', line 6

def dependent
  @dependent
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ricordami/relationship.rb', line 6

def name
  @name
end

#object_kindObject (readonly)

Returns the value of attribute object_kind.



6
7
8
# File 'lib/ricordami/relationship.rb', line 6

def object_kind
  @object_kind
end

#self_kindObject (readonly)

Returns the value of attribute self_kind.



6
7
8
# File 'lib/ricordami/relationship.rb', line 6

def self_kind
  @self_kind
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/ricordami/relationship.rb', line 6

def type
  @type
end

Instance Method Details

#object_classObject



24
25
26
# File 'lib/ricordami/relationship.rb', line 24

def object_class
  @object_kind.to_s.camelize.constantize
end

#referrer_idObject



28
29
30
31
32
# File 'lib/ricordami/relationship.rb', line 28

def referrer_id
  return @referrer_id unless @referrer_id.nil?
  referrer = type == :referenced_in ? @name.to_s : @alias.to_s.singularize
  @referrer_id = referrer.foreign_key
end