Module: ActiveTriples::Properties::ClassMethods

Defined in:
lib/active_triples/properties.rb

Overview

Class methods for classes with ‘Properties`

Instance Method Summary collapse

Instance Method Details

#config_for_term_or_uri(term) ⇒ ActiveTriples::NodeConfig

Given a property name or a predicate, return the configuration for the matching property.

Parameters:

  • term (#to_sym, RDF::Resource)

    property name or predicate

Returns:



155
156
157
158
159
# File 'lib/active_triples/properties.rb', line 155

def config_for_term_or_uri(term)
  return properties[term.to_s] unless
    term.is_a?(RDF::Resource) && !term.is_a?(RDFSource)
  properties.each_value { |v| return v if v.predicate == term.to_uri }
end

#fieldsArray<Symbol>

List the property names registered to the class.

Returns:

  • (Array<Symbol>)

    list of the symbolized names of registered properties



166
167
168
# File 'lib/active_triples/properties.rb', line 166

def fields
  properties.keys.map(&:to_sym)
end

#generated_property_methodsModule

Note:

use the alias #initialize_generated_modules for clarity of intent where appropriate

Gives existing generated property methods. If the property methods are not yet present, generates them as a new Module and includes it.

Returns:

  • (Module)

    a module self::GeneratedPropertyMethods which is included in self and defines the property methods

See Also:



111
112
113
114
115
116
117
# File 'lib/active_triples/properties.rb', line 111

def generated_property_methods
  @generated_property_methods ||= begin
    mod = const_set(:GeneratedPropertyMethods, Module.new)
    include mod
    mod
  end
end

#inherited(child_class) ⇒ Object

:nodoc:



82
83
84
85
# File 'lib/active_triples/properties.rb', line 82

def inherited(child_class) #:nodoc:
  child_class.initialize_generated_modules
  super
end

#initialize_generated_modulesModule

Note:

this is an alias to #generated_property_methods. Use it when you intend to initialize, rather than retrieve, the methods for code readability

If the property methods are not yet present, generates them.

Returns:

  • (Module)

    a module self::GeneratedPropertyMethods which is included in self and defines the property methods

See Also:



97
98
99
# File 'lib/active_triples/properties.rb', line 97

def initialize_generated_modules
  generated_property_methods
end

#property(name, opts = {}) {|index| ... } ⇒ Hash{String=>ActiveTriples::NodeConfig}

Registers properties for Resource-like classes

Parameters:

  • name (Symbol)

    of the property (and its accessor methods)

  • opts (Hash) (defaults to: {})

    for this property, must include a :predicate

Yields:

  • (index)

    index sets solr behaviors for the property

Returns:

Raises:

  • (ArgumentError)


128
129
130
131
132
# File 'lib/active_triples/properties.rb', line 128

def property(name, opts={}, &block)
  raise ArgumentError, "#{name} is a keyword and not an acceptable property name." if protected_property_name? name
  reflection = PropertyBuilder.build(self, name, opts, &block)
  Reflection.add_reflection self, name, reflection
end

#protected_property_name?(name) ⇒ Boolean

Checks potential property names for conflicts with existing class instance methods. We avoid setting properties with these names to prevent catastrophic method overwriting.

Parameters:

  • name (Symblol)

    A potential property name.

Returns:

  • (Boolean)

    true if the given name matches an existing instance method which is not an ActiveTriples property.



142
143
144
145
146
# File 'lib/active_triples/properties.rb', line 142

def protected_property_name?(name)
  reject = self.instance_methods.map! { |s| s.to_s.gsub(/=$/, '').to_sym }
  reject -= properties.keys.map { |k| k.to_sym }
  reject.include? name
end