Module: Neo4j::Wrapper::Property::ClassMethods
- Defined in:
- lib/neo4j-wrapper/properties/class_methods.rb
Instance Method Summary collapse
-
#_converter(prop_name) ⇒ #to_java, ...
A converter for the given property name.
-
#_decl_props ⇒ Hash
A hash of all properties and its configuration defined by property class method.
-
#inherited(klass) ⇒ Object
make sure the inherited classes inherit the
_decl_props
hash. -
#property(*props) ⇒ Object
Generates accessor method and sets configuration for Neo4j node properties.
-
#property?(prop_name) ⇒ true, false
Returns true if the given property name has been defined with the class method property or properties.
Instance Method Details
#_converter(prop_name) ⇒ #to_java, ...
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_props ⇒ Hash
Returns a hash of all properties and its configuration defined by property class method.
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
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) = props.last.kind_of?(Hash) ? props.pop : {} props.uniq.each do |prop| pname = prop.to_sym _decl_props[pname] ||= {} .each { |key, value| _decl_props[pname][key] = value } converter = [:converter] || Neo4j::TypeConverters.converter(_decl_props[pname][:type]) _decl_props[pname][:converter] = converter if .include?(:index) index(pname, :type => [: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 |