Sha256: 33cead7feaf5a234030a737d1bfcf3a846ea89deb55875e2504334db2fc678fc

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 KB

Contents

module Neo4j::Shared
  # Contains methods related to the management
  class DeclaredProperty
    class IllegalPropertyError < StandardError; end

    ILLEGAL_PROPS = %w(from_node to_node start_node end_node)
    attr_reader :name, :name_string, :name_sym, :options, :magic_typecaster

    def initialize(name, options = {})
      fail IllegalPropertyError, "#{name} is an illegal property" if ILLEGAL_PROPS.include?(name.to_s)
      @name = @name_sym = name
      @name_string = name.to_s
      @options = options
    end

    def register
      register_magic_properties
    end

    def type
      options[:type]
    end

    def typecaster
      options[:typecaster]
    end

    def default_value
      options[:default]
    end

    private

    # Tweaks properties
    def register_magic_properties
      options[:type] ||= Neo4j::Config.timestamp_type if timestamp_prop?

      register_magic_typecaster
      register_type_converter
    end

    def timestamp_prop?
      name.to_sym == :created_at || name.to_sym == :updated_at
    end

    def register_magic_typecaster
      found_typecaster = Neo4j::Shared::TypeConverters.typecaster_for(options[:type])
      return unless found_typecaster && found_typecaster.respond_to?(:primitive_type)
      options[:typecaster] = found_typecaster
      @magic_typecaster = options[:type]
      options[:type] = found_typecaster.primitive_type
    end

    def register_type_converter
      converter = options[:serializer]
      return unless converter
      options[:type]        = converter.convert_type
      options[:typecaster]  = ActiveAttr::Typecasting::ObjectTypecaster.new
      Neo4j::Shared::TypeConverters.register_converter(converter)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
neo4j-5.2.15 lib/neo4j/shared/declared_property.rb
neo4j-5.2.14 lib/neo4j/shared/declared_property.rb
neo4j-5.2.13 lib/neo4j/shared/declared_property.rb
neo4j-5.2.12 lib/neo4j/shared/declared_property.rb
neo4j-5.2.11 lib/neo4j/shared/declared_property.rb
neo4j-5.2.10 lib/neo4j/shared/declared_property.rb
neo4j-5.2.9 lib/neo4j/shared/declared_property.rb
neo4j-5.2.8 lib/neo4j/shared/declared_property.rb
neo4j-5.2.7 lib/neo4j/shared/declared_property.rb
neo4j-5.2.6 lib/neo4j/shared/declared_property.rb
neo4j-5.2.5 lib/neo4j/shared/declared_property.rb
neo4j-5.2.3 lib/neo4j/shared/declared_property.rb
neo4j-5.2.2 lib/neo4j/shared/declared_property.rb
neo4j-5.2.1 lib/neo4j/shared/declared_property.rb
neo4j-5.2.0 lib/neo4j/shared/declared_property.rb