Skip to content

Commit

Permalink
#18 Forbid registering equally named relations
Browse files Browse the repository at this point in the history
  • Loading branch information
snusnu committed Aug 4, 2014
1 parent a23c792 commit 3157331
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/ramom/schema/definition/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def attr_names
end # Base
end # Relation

class AlreadyRegistered < StandardError
def initialize(name)
super("The relation #{name.inspect} is already registered")
end
end # AlreadyRegistered

DEFAULT_ATTRIBUTES = {
base: {},
virtual: {},
Expand All @@ -47,6 +53,8 @@ def call(&block)
end

def base_relation(name, options, &block)
assert_not_already_registered(name, base)

base[name] = Relation::Base.new(
name: name,
visibility: options.fetch(:visibility, :public),
Expand All @@ -68,12 +76,20 @@ def external(name, &block)
end

def relation(name, visibility, &block)
assert_not_already_registered(name, virtual)

virtual[name] = Relation.new(
name: name,
visibility: visibility,
body: block
)
end

private

def assert_not_already_registered(name, relations)
fail(AlreadyRegistered.new(name)) if relations.key?(name)
end
end # Context
end # Definition
end # Schema
Expand Down

0 comments on commit 3157331

Please sign in to comment.