Class: RDFMapper::Model::Attribute

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/lib/model/attribute.rb

Overview

Contains configuration and convenience methods for model attributes. Instances of this class are assigned to classes (not instances!) of RDFMapper::Model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#debug, #fatal, #warn

Constructor Details

#initialize(cls, name, options) ⇒ self

Constructor is called for each attribute of a model at the time the model is defined.

Parameters:

  • cls (Object)

    class of the model

  • name (String)

    name of the attribute

  • options (Hash)

    options to pass on to the property / association constructor



24
25
26
27
28
# File 'lib/lib/model/attribute.rb', line 24

def initialize(cls, name, options)
  @cls = cls
  @name = name
  @options = options.dup
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/lib/model/attribute.rb', line 12

def name
  @name
end

Instance Method Details

#belongs_to?Boolean

Checks if this attribute is a ‘belongs_to` association.

Returns:

  • (Boolean)


35
36
37
# File 'lib/lib/model/attribute.rb', line 35

def belongs_to?
  not property? and not multiple?
end

#matches?(predicate, value = nil) ⇒ Boolean

Checks if this attribute has the same predicate and / or value. Value is accepted both as an instance and a class.

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lib/model/attribute.rb', line 117

def matches?(predicate, value = nil)
  if type.nil?                      # Always false if attribute predicate is not defined
    return false
  end
  if value == nil                   # Checking predicates
    return type.to_s == predicate.to_s
  end
  unless value.respond_to? :new     # Converting instance to a class
    value = value.class
  end
  if model.nil?               # Value is not nil, but model is undefined
    return false
  end
  if value.type != model.type # Value type and model type should match
    return false
  end
  if predicate.nil?                 # Value and model types match
    true
  else
    predicate == type
  end
end

#modelObject?

Returns class of the associated model. Uses either the ‘:class_name` option or relies on association name. It follows ActiveRecord naming conventions(e.g. has_many should be plural, belongs_to - singular).

Returns:

  • (Object)

    a subclass of RDFMapper::Model

  • (nil)

    if association is not found or this is a property attribute



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lib/model/attribute.rb', line 72

def model
  # A bit of 'caching': lookups based on namespace can be quite slow
  unless @model.nil?
    return @model
  end
  
  # Should return nil if this is not an association
  if property?
    return nil
  end

  if @model = model_from_options || model_from_namespace
    @model
  else
    raise RuntimeError, 'Could not find association model for %s (%s :%s)' % [@cls, @options[:association], @name]
  end
end

#property?Boolean

Checks if this attribute is a property (i.e. not an association).

Returns:

  • (Boolean)


44
45
46
# File 'lib/lib/model/attribute.rb', line 44

def property?
  @options[:association].nil?
end

#typeRDF::URI?

Returns attribute’s RDF predicate.

Returns:

  • (RDF::URI)
  • (nil)

    if not specified and model has no namespace



54
55
56
57
58
59
60
61
62
# File 'lib/lib/model/attribute.rb', line 54

def type
  # Type is 'cached': lookups based on namespace can be quite slow
  @type ||= unless @options[:predicate].nil?
    RDF::URI.new(@options[:predicate].to_s)
  else
    # Keep this weird comparison. RDF::Vocabulary doesn't recognize `nil?` or `==` 
    (nil == @cls.namespace) ? nil : @cls.namespace[@name]
  end
end

#value(instance = nil) ⇒ Object

When model instance is not specified, it will return Associations::HasMany, Associations::BelongsTo, Property, etc. Alternatively, when RDFMapper::Model is specified, it will return an instance of these classes.

Parameters:

  • instance (Object) (defaults to: nil)

    instance of RDFMapper::Model

Returns:

  • (Object)

    class of this attribute

  • (Object)

    instance of this attribute



102
103
104
105
106
107
108
109
110
111
# File 'lib/lib/model/attribute.rb', line 102

def value(instance = nil)
  if nil == instance
    return attribute_type
  end
  attribute_type.new(instance, @options.merge({
    :cls => model,
    :type => type,
    :name => name
  }))
end