Skip to content

Commit

Permalink
Merge pull request #490 from PagedeGeek/some-refactoring
Browse files Browse the repository at this point in the history
Some ruby style refactoring
  • Loading branch information
subvertallchris committed Sep 24, 2014
2 parents 917d87c + 89ff253 commit 06a2292
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 51 deletions.
6 changes: 1 addition & 5 deletions lib/neo4j/active_node/id_property.rb
Expand Up @@ -42,12 +42,8 @@ def define_id_methods(clazz, name, conf)

def validate_conf(conf)
return if conf.empty?

raise "Expected a Hash, got #{conf.class} (#{conf.to_s}) for id_property" unless conf.is_a?(Hash)

unless conf.include?(:auto) || conf.include?(:on)
raise "Illegal value #{conf.inspect} for id_property, expected :on or :auto"
end
raise "Illegal value #{conf.inspect} for id_property, expected :on or :auto" unless conf.include?(:auto) || conf.include?(:on)
end

def define_property_method(clazz, name)
Expand Down
2 changes: 1 addition & 1 deletion lib/neo4j/active_node/labels.rb
Expand Up @@ -86,7 +86,7 @@ def find_by(*args)
self.query_as(:n).where(n: eval(args.join)).limit(1).pluck(:n).first
end

# Like find_by, except that if no record is found, raises a RecordNotFound error.
# Like find_by, except that if no record is found, raises a RecordNotFound error.
def find_by!(*args)
a = eval(args.join)
find_by(args) or raise RecordNotFound, "#{self.query_as(:n).where(n: a).limit(1).to_cypher} returned no results"
Expand Down
6 changes: 3 additions & 3 deletions lib/neo4j/active_node/query_methods.rb
Expand Up @@ -44,10 +44,10 @@ def include?(other)
private

def exists_query_start(node_condition)
case
when node_condition.class == Fixnum
case node_condition
when Fixnum
self.query_as(:n).where("ID(n) = #{node_condition}")
when node_condition.class == Hash
when Hash
self.where(node_condition.keys.first => node_condition.values.first)
else
self.query_as(:n)
Expand Down
2 changes: 1 addition & 1 deletion lib/neo4j/active_node/scope.rb
Expand Up @@ -61,7 +61,7 @@ def _scope

def _call_scope_context(eval_context, query_params, proc)
if proc.arity == 1
eval_context.instance_exec(query_params,&proc)
eval_context.instance_exec(query_params, &proc)
else
eval_context.instance_exec(&proc)
end
Expand Down
11 changes: 2 additions & 9 deletions lib/neo4j/active_node/validations.rb
Expand Up @@ -31,19 +31,12 @@ def validate_each(record, attribute, value)
# TODO: Added as find(:name => nil) throws error
value = "" if value == nil

if options[:case_sensitive]
conditions[attribute] = value
else
conditions[attribute] = /^#{Regexp.escape(value.to_s)}$/i
end
conditions[attribute] = options[:case_sensitive] ? value : /^#{Regexp.escape(value.to_s)}$/i

# prevent that same object is returned
# TODO: add negative condtion to not return current record
found = record.class.where(conditions).to_a.select{|f| f.neo_id != record.neo_id}

if found.count > 0
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
end
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value)) if found.count > 0
end

def message(instance)
Expand Down
20 changes: 7 additions & 13 deletions lib/neo4j/active_rel/persistence.rb
Expand Up @@ -13,9 +13,7 @@ def save(*)
end

def save!(*args)
unless save(*args)
raise RelInvalidError.new(self)
end
raise RelInvalidError.new(self) unless save(*args)
end

def create_model(*)
Expand Down Expand Up @@ -44,21 +42,17 @@ def create(props = {})

# Same as #create, but raises an error if there is a problem during save.
def create!(*args)
unless create(*args)
raise RelInvalidError.new(self)
end
raise RelInvalidError.new(self) unless create(*args)
end
end

private
private

def confirm_node_classes
[from_node, to_node].each do |node|
type = from_node == node ? :_from_class : :_to_class
next if allows_any_class?(type)
unless class_as_constant(type) == node.class
raise ModelClassInvalidError, "Node class was #{node.class}, expected #{self.class.send(type)}"
end
raise ModelClassInvalidError, "Node class was #{node.class}, expected #{self.class.send(type)}" unless class_as_constant(type) == node.class
end
end

Expand All @@ -71,10 +65,10 @@ def _create_rel(from_node, to_node, *args)

def class_as_constant(type)
given_class = self.class.send(type)
case
when given_class.is_a?(String)
case given_class
when String
given_class.constantize
when given_class.is_a?(Symbol)
when Symbol
given_class.to_s.constantize
else
given_class
Expand Down
17 changes: 8 additions & 9 deletions lib/neo4j/active_rel/query.rb
Expand Up @@ -54,7 +54,7 @@ def cypher_string(dir = :outbound)
when :inbound
identifier = '(n2'
identifier + (_to_class == :any ? ')' : cypher_label(:inbound))
end
end
end

def cypher_label(dir = :outbound)
Expand All @@ -63,22 +63,21 @@ def cypher_label(dir = :outbound)
end

def as_constant(given_class)
case
when given_class.is_a?(String)
case given_class
when String
given_class.constantize
when given_class.is_a?(Symbol)
when Symbol
given_class.to_s.constantize
else
given_class
end
end

def where_string(args)
if args.is_a?(Hash)
args.map do |k, v|
v.is_a?(Integer) ? "r1.#{k} = #{v}" : "r1.#{k} = '#{v}'"
end.join(', ')
else
case args
when Hash
args.map { |k, v| v.is_a?(Integer) ? "r1.#{k} = #{v}" : "r1.#{k} = '#{v}'" }.join(', ')
else
args
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/neo4j/active_rel/rel_wrapper.rb
Expand Up @@ -5,7 +5,7 @@ def wrapper
props.symbolize_keys!
return self unless props.is_a?(Hash) && props.has_key?(Neo4j::Config.class_name_property)
begin
found_class = props[Neo4j::Config.class_name_property].constantize
found_class = props[Neo4j::Config.class_name_property].constantize
rescue NameError
return self
end
Expand Down
6 changes: 1 addition & 5 deletions lib/neo4j/shared/property.rb
Expand Up @@ -108,11 +108,7 @@ def instantiate_object(field, values_with_empty_parameters)
return nil if values_with_empty_parameters.all? { |v| v.nil? }
values = values_with_empty_parameters.collect { |v| v.nil? ? 1 : v }
klass = field[:type]
if klass
klass.new(*values)
else
values
end
klass ? klass.new(*values) : values
end

module ClassMethods
Expand Down
8 changes: 4 additions & 4 deletions lib/neo4j/shared/validations.rb
Expand Up @@ -28,13 +28,13 @@ def valid?(context = nil)
super(context)
errors.empty?
end

private

def perform_validations(options={})
perform_validation = case options
when Hash
options[:validate] != false
when Hash
options[:validate] != false
end

if perform_validation
Expand Down

0 comments on commit 06a2292

Please sign in to comment.