Class: RDFMapper::Associations::Base

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/lib/associations/base.rb

Overview

Base class for all association types. Contains default constructor.

Direct Known Subclasses

BelongsTo, HasMany

Instance Method Summary collapse

Methods included from Logger

#debug, #fatal, #warn

Constructor Details

#initialize(instance, options = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
# File 'lib/lib/associations/base.rb', line 16

def initialize(instance, options = {})
  @instance = instance
  @association = options[:cls]
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object (private)

-


85
86
87
88
89
90
91
# File 'lib/lib/associations/base.rb', line 85

def method_missing(symbol, *args, &block)
  if value.respond_to? symbol
    value.send(symbol, *args, &block)
  else
    raise RuntimeError, 'Undefined method `%s`' % symbol
  end
end

Instance Method Details

#inspectString

Developer-friendly representation of the instance

Returns:

  • (String)


68
69
70
# File 'lib/lib/associations/base.rb', line 68

def inspect #nodoc
  value.inspect
end

#object(force = false) ⇒ Object

-


32
33
34
35
# File 'lib/lib/associations/base.rb', line 32

def object(force = false)
  value.nil? and value.empty? if force
  self
end

#replace(value) ⇒ Object

Association should override this method

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/lib/associations/base.rb', line 25

def replace(value)
  raise NotImplementedError, 'Expected association to override `replace`'
end

#to_statements(options = {}) ⇒ Object

-


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lib/associations/base.rb', line 40

def to_statements(options = {})
  options[:skip] ||= []
  if value.kind_of? Array
    items = value
  else
    items = [value]
  end
  items.reject do |item|
    options[:skip].include?(items)
  end.map do |item|
    node = if options[:full]
      item.to_statements(:skip => @instance)
    else
      item.to_statements(:short => true)
    end
    node + [{
      :subject => @instance.id,
      :predicate => @options[:type],
      :object => item.id
    }]
  end.flatten
end