Class: Neo4j::Rails::Model
- Inherits:
-
Object
- Object
- Neo4j::Rails::Model
- Defined in:
- lib/neo4j_helper/accepts_hash_for.rb
Class Method Summary collapse
-
.accepts_hash_for(prop, *block) ⇒ Object
This allows anything to be thrown at a relationship and understood.
Class Method Details
.accepts_hash_for(prop, *block) ⇒ Object
This allows anything to be thrown at a relationship and understood. usage:
has_one(:location).to(Location) accepts_hash_for :location
Will run Location.new with the passed in :location hash. You can also give it a block which returns a location node:
accepts_hash_for :location do |location_attributes|
Location.find_or_create_by(:name => location_attributes[:name])
end
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/neo4j_helper/accepts_hash_for.rb', line 19 def accepts_hash_for(prop, *block) # todo: handle persistence? # for working with this method, see these neo4j examples: # def _add_relationship(rel_type, node) # def update_nested_attributes # def accepts_nested_attributes_for rel = self._decl_rels[prop.to_sym] raise "No relationship declared with has_n or has_one with type #{prop}" unless rel if rel.has_one? setter = :"#{prop}=" original_setter = :"#{prop}_without_accepting_hash=" define_method :"#{prop}_with_accepting_hash=" do |attributes| node = if attributes.is_a? rel.target_class attributes elsif attributes.present? # not sure what would be to happen if a block were somehow to be given to the setter method if block_given? yield(attributes) else # todo: allow find_or_create target_class.new attributes end else nil end send(original_setter, node) end alias_method_chain setter, :accepting_hash end else # todo: has_n end |