lib/neoid/model_additions.rb in neoid-0.0.2 vs lib/neoid/model_additions.rb in neoid-0.0.5.alpha
- old
+ new
@@ -1,32 +1,53 @@
module Neoid
module ModelAdditions
module ClassMethods
- def neoidable(options)
- @config = Neoid::ModelConfig.new
- yield(@config) if block_given?
- @neoidable_options = options
+ attr_reader :neoid_config
+ attr_reader :neoid_options
+
+ def neoid_config
+ @neoid_config ||= Neoid::ModelConfig.new(self)
end
-
- def neoidable_options
- @neoidable_options
+
+ def neoidable(options = {})
+ yield(neoid_config) if block_given?
+ @neoid_options = options
end
def neo_index_name
@index_name ||= "#{self.name.tableize}_index"
end
end
module InstanceMethods
def to_neo
- {}
+ if self.class.neoid_config.stored_fields
+ hash = self.class.neoid_config.stored_fields.inject({}) do |all, (field, block)|
+ all[field] = if block
+ instance_eval(&block)
+ else
+ self.send(field) rescue (raise "No field #{field} for #{self.class.name}")
+ end
+
+ all
+ end
+
+ hash.reject { |k, v| v.nil? }
+ else
+ {}
+ end
end
+ def neo_resave
+ _reset_neo_representation
+ neo_update
+ end
+
protected
- def neo_properties_to_hash(*property_list)
- property_list.flatten.inject({}) { |all, property|
- all[property] = self.attributes[property]
+ def neo_properties_to_hash(*attribute_list)
+ attribute_list.flatten.inject({}) { |all, property|
+ all[property] = self.send(property)
all
}
end
private
@@ -34,13 +55,22 @@
@_neo_representation ||= begin
results = neo_find_by_id
if results
neo_load(results.first['self'])
else
- node = neo_create
- node
+ neo_create
end
end
end
+
+ def _reset_neo_representation
+ @_neo_representation = nil
+ end
+ end
+
+ def self.included(receiver)
+ receiver.extend ClassMethods
+ receiver.send :include, InstanceMethods
+ Neoid.models << receiver
end
end
end