Sha256: 69c99156f418b2dddcdba1ce8c803ee96dbbf4631079e588cf9354d6534b3b7a

Contents?: true

Size: 1.9 KB

Versions: 12

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true
require 'active_support/core_ext/class'

module ActiveTriples
  module Reflection
    extend ActiveSupport::Concern

    included do
      class_attribute :_active_triples_config
      self._active_triples_config = {}
    end

    ##
    # Gives access to a `Reflection` of the properties configured on this class
    #
    # @example
    #   my_source.reflections.has_property?(:title)
    #   my_source.reflections.reflect_on_property(:title)
    # @return [Class] gives `self#class`
    #
    def reflections
      self.class
    end

    def self.add_reflection(model, name, reflection)
      model._active_triples_config = 
        model._active_triples_config.merge(name.to_s => reflection)
    end

    module ClassMethods
      ##
      # @param [#to_s] property
      #
      # @return [ActiveTriples::NodeConfig] the configuration for the property
      #
      # @raise [ActiveTriples::UndefinedPropertyError] when the property does 
      #   not exist
      def reflect_on_property(property)
        _active_triples_config.fetch(property.to_s) do
          raise ActiveTriples::UndefinedPropertyError.new(property.to_s, self)
        end
      end

      ##
      # @return [Hash{String=>ActiveTriples::NodeConfig}] a hash of property 
      #   names and their configurations
      def properties
        _active_triples_config
      end

      ##
      # @param [Hash{String=>ActiveTriples::NodeConfig}] a complete config hash
      #   to set the properties to.
      # @return [Hash{String=>ActiveTriples::NodeConfig}] a hash of property 
      #   names and their configurations
      def properties=(val)
        self._active_triples_config = val
      end

      ##
      # @param [#to_s] property
      #
      # @return [Boolean] true if the property exsits; false otherwise
      def has_property?(property)
        _active_triples_config.keys.include? property.to_s
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
active-triples-1.2.0 lib/active_triples/reflection.rb
active-triples-1.1.1 lib/active_triples/reflection.rb
active-triples-1.1.0 lib/active_triples/reflection.rb
active-triples-1.0.0 lib/active_triples/reflection.rb
active-triples-1.0.0.rc1 lib/active_triples/reflection.rb
active-triples-0.11.0 lib/active_triples/reflection.rb
active-triples-0.10.2 lib/active_triples/reflection.rb
active-triples-0.10.1 lib/active_triples/reflection.rb
active-triples-0.10.0 lib/active_triples/reflection.rb
active-triples-0.9.0 lib/active_triples/reflection.rb
active-triples-0.8.3 lib/active_triples/reflection.rb
active-triples-0.8.2 lib/active_triples/reflection.rb