Module: Neo4j::Wrapper::Property::ClassMethods

Defined in:
lib/neo4j-wrapper/properties/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#_converter(prop_name) ⇒ #to_java, ...

Note:

if the property has not been defined it will return the DefaultConverter

Returns a converter for the given property name



130
131
132
133
# File 'lib/neo4j-wrapper/properties/class_methods.rb', line 130

def _converter(prop_name)
  prop_conf = _decl_props[prop_name.to_sym]
  (prop_conf && prop_conf[:converter]) || Neo4j::TypeConverters::DefaultConverter
end

#_decl_propsHash



8
9
10
# File 'lib/neo4j-wrapper/properties/class_methods.rb', line 8

def _decl_props
  @_decl_props ||= {}
end

#inherited(klass) ⇒ Object

make sure the inherited classes inherit the _decl_props hash



13
14
15
16
# File 'lib/neo4j-wrapper/properties/class_methods.rb', line 13

def inherited(klass)
  klass.instance_variable_set(:@_decl_props, _decl_props.clone)
  super
end

#property(*props) ⇒ Object

Generates accessor method and sets configuration for Neo4j node properties. The generated accessor creates a wrapper around the #[] and #[]= operators and support conversion of values before it is read or written to the database, using the :type config parameter. If a property is set to nil the property will be removed (just like the #[]=accessor]) A lucene index can also be specified, using the :index config parameter. If you want to add index on a none declared property you can (still) use the index class method, see Neo4j::Core::Index::Indexer

:type

A property can be of any primitive type (Boolean, String, Fixnum, Float) and does not even have to be the same. Arrays of primitive types is also supported. Array values must be of the same type and are mutable, e.g. you have to create a new array if you want to change one value. To make sure a property is always stored as the same type you can use the TypeConverter, which is specified using either the :type or the :converter parameter. This is also important when indexing, e.g finding a property which has been declared as a Fixnum but queried as a String will not work.

:converter

You can implement your own converter, see Neo4j::TypeConverters. You can write your own converter by writing a class that respond to :index_as, :to_ruby and :to_java.

:index

If a :index is specified the property will be automatically indexed. By default, there are two different indices available: :fulltext and :exact If not specified in a query it will use by default an :exact index. Notice, that you can't combine a fulltext and exact lucene query in the same query. If you are using fulltext index you must specify the type parameter in the find method (:exact is default). Also, if you want to sort or do range query make sure use the same query parameters as the declared type For example if you index with property :age, :index => :exact, :type => Fixnum) then you must query with age parameter being a fixnum, see examples below.

More Examples

However, you can specify an type for the index, see Neo4j::Index::Indexer#index

Examples:

a property with an index


class Foo
  include Neo4j::NodeMixin
  property :description, :index => :fulltext
end

Lucene Query

Foo.find("description: bla", :type => :fulltext)
Foo.find({:description =>  "bla"}, {:type => :fulltext})

Lucene Query with a Fixnum index

Foo.find(:age =>  35)
Foo.find(:age =>  (32..37))
Foo.find("age: 35") # does not work !

declare a property

class Foo
  include Neo4j::NodeMixin
  property :age
end

use a declared property

foo = Foo.new
foo.age = "hej" # first set it to string
foo.age = 42  # change it to a Fixnum

conversion of none primitive types


class Foo
  include Neo4j::NodeMixin
  property :since, :type => DateTime  # will be converted into a fixnum
end

declare several properties in one line


class Foo
  include Neo4j::NodeMixin
  property :name, :description, :type => String, :index => :exact
end

See Also:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/neo4j-wrapper/properties/class_methods.rb', line 99

def property(*props)
  options = props.last.kind_of?(Hash) ? props.pop : {}

  props.uniq.each do |prop|
    pname = prop.to_sym
    _decl_props[pname] ||= {}
    options.each { |key, value| _decl_props[pname][key] = value }

    converter = options[:converter] || Neo4j::TypeConverters.converter(_decl_props[pname][:type])
    _decl_props[pname][:converter] = converter

    if options.include?(:index)
      index(pname, :type => options[:index], :field_type => converter.index_as)
    end

    define_method(pname) do
      self.class._converter(pname).to_ruby(self[pname])
    end

    name = (pname.to_s() +"=").to_sym
    define_method(name) do |value|
      self[pname] = self.class._converter(pname).to_java(value)
    end
  end
end

#property?(prop_name) ⇒ true, false

Returns true if the given property name has been defined with the class method property or properties.

Notice that the node may have properties that has not been declared. It is always possible to set an undeclared property on a node.



142
143
144
145
# File 'lib/neo4j-wrapper/properties/class_methods.rb', line 142

def property?(prop_name)
  return false if _decl_props[prop_name.to_sym].nil?
  !_decl_props[prop_name.to_sym].nil?
end