Class: RDFMapper::Model::Attribute
- Inherits:
-
Object
- Object
- RDFMapper::Model::Attribute
- 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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#belongs_to? ⇒ Boolean
Checks if this attribute is a ‘belongs_to` association.
-
#initialize(cls, name, options) ⇒ self
constructor
Constructor is called for each attribute of a model at the time the model is defined.
-
#matches?(predicate, value = nil) ⇒ Boolean
Checks if this attribute has the same predicate and / or value.
-
#model ⇒ Object?
Returns class of the associated model.
-
#property? ⇒ Boolean
Checks if this attribute is a property (i.e. not an association).
-
#type ⇒ RDF::URI?
Returns attribute’s RDF predicate.
-
#value(instance = nil) ⇒ Object
When model instance is not specified, it will return Associations::HasMany, Associations::BelongsTo, Property, etc.
Methods included from Logger
Constructor Details
#initialize(cls, name, options) ⇒ self
Constructor is called for each attribute of a model at the time the model is defined.
24 25 26 27 28 |
# File 'lib/lib/model/attribute.rb', line 24 def initialize(cls, name, ) @cls = cls @name = name @options = .dup end |
Instance Attribute Details
#name ⇒ Object (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.
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.
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 |
#model ⇒ Object?
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).
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_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).
44 45 46 |
# File 'lib/lib/model/attribute.rb', line 44 def property? @options[:association].nil? end |
#type ⇒ RDF::URI?
Returns attribute’s RDF predicate.
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.
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 |