Skip to content

Commit

Permalink
protect against illegal props
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Aug 16, 2014
1 parent d462c6e commit ecd45d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/neo4j/shared/property.rb
Expand Up @@ -11,6 +11,9 @@ module Property

class UndefinedPropertyError < RuntimeError; end
class MultiparameterAssignmentError < StandardError; end
class IllegalPropertyError < StandardError; end

ILLEGAL_PROPS = %w[from_node to_node start_node end_node]

def initialize(attributes={}, options={})
attributes = process_attributes(attributes)
Expand Down Expand Up @@ -141,17 +144,10 @@ module ClassMethods
# property :name, constraint: :unique
# end
def property(name, options={})
check_illegal_prop(name)
magic_properties(name, options)
attribute(name, options)

# either constraint or index, do not set both
if options[:constraint]
raise "unknown constraint type #{options[:constraint]}, only :unique supported" if options[:constraint] != :unique
constraint(name, type: :unique)
elsif options[:index]
raise "unknown index type #{options[:index]}, only :exact supported" if options[:index] != :exact
index(name, options) if options[:index] == :exact
end
constraint_or_index(name, options)
end

def default_property(name, &block)
Expand Down Expand Up @@ -184,6 +180,23 @@ def cached_class?

private

def constraint_or_index(name, options)
# either constraint or index, do not set both
if options[:constraint]
raise "unknown constraint type #{options[:constraint]}, only :unique supported" if options[:constraint] != :unique
constraint(name, type: :unique)
elsif options[:index]
raise "unknown index type #{options[:index]}, only :exact supported" if options[:index] != :exact
index(name, options) if options[:index] == :exact
end
end

def check_illegal_prop(name)
if ILLEGAL_PROPS.include?(name.to_s)
raise IllegalPropertyError, "#{name} is an illegal property"
end
end

# Tweaks properties
def magic_properties(name, options)
set_stamp_type(name, options)
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/shared/property_spec.rb
@@ -0,0 +1,12 @@
require 'spec_helper'

describe Neo4j::Shared::Property do
let(:clazz) { Class.new { include Neo4j::Shared::Property } }

describe ':property class method' do
it 'raises an error when passing illegal properties' do
Neo4j::Shared::Property::ILLEGAL_PROPS.push 'foo'
expect{clazz.property :foo}.to raise_error(Neo4j::Shared::Property::IllegalPropertyError)
end
end
end

1 comment on commit ecd45d4

@subvertallchris
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.