Sha256: ab86ed0e691807b3bc956f428edea6cfada240759b9787359e5147bf5b75682f

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true
module ActiveTriples
  class NodeConfig
    attr_accessor :predicate, :term, :class_name, :type, :behaviors, :cast

    def initialize(term, predicate, args={})
      self.term = term
      self.predicate = predicate
      self.class_name = args.delete(:class_name) { default_class_name }
      self.cast = args.delete(:cast) { true }
      @opts = args
      yield(self) if block_given?
    end

    def [](value)
      value = value.to_sym
      self.respond_to?(value) ? self.send(value) : @opts[value]
    end

    def class_name
      return nil if @class_name.nil?
      raise "class_name for #{term} is a #{@class_name.class}; must be a class" unless @class_name.kind_of? Class or @class_name.kind_of? String
      if @class_name.kind_of?(String)
        begin
          new_class = @class_name.constantize
          @class_name = new_class
        rescue NameError
        end
      end
      @class_name
    end

    def with_index (&block)
      # needed for solrizer integration
      iobj = IndexObject.new
      yield iobj
      self.type = iobj.data_type
      self.behaviors = iobj.behaviors
    end

    private

    def default_class_name
      nil
    end

    # this enables a cleaner API for solr integration
    class IndexObject
      attr_accessor :data_type, :behaviors
      def initialize
        @behaviors = []
        @data_type = :string
      end
      def as(*args)
        @behaviors = args
      end
      def type(sym)
        @data_type = sym
      end
      def defaults
        :noop
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active-triples-0.11.0 lib/active_triples/node_config.rb
active-triples-0.10.2 lib/active_triples/node_config.rb
active-triples-0.10.1 lib/active_triples/node_config.rb
active-triples-0.10.0 lib/active_triples/node_config.rb
active-triples-0.9.0 lib/active_triples/node_config.rb
active-triples-0.8.3 lib/active_triples/node_config.rb
active-triples-0.8.2 lib/active_triples/node_config.rb