On Mon, Mar 4, 2013 at 7:36 PM, Frederick C.
<[email protected]
wrote:
Could you give some examples of what you’re trying to do or problems
you’ve run into? You’ve posted some code but haven’t described how you’ll
be using it and in what way it isn’t adequate.In fewer words: I don’t see
what your question is.
Thanks for replying.
Yes sorry. I realize that some context is missing.
I am writing an API where the users are allowed to write something
like
that:
FooContextWithName.new.eval do |o|
o.integer “id”, an_option: true # [1]
o.integer “foo”, an_option: true # [2]
o.bar “a_name” do |o|
o.integer an_option: true # [3]
end
end
I would like that users are able to factor [1] and [2] by writing a
small
plugin like this:
module MyHelpers
def id(name = “id”)
integer name, an_option: true
end
end
MyLib.add_helpers(MyHelpers) # add_helpers would be defined
appropriately
To factor [3] users should be able to write something like that:
module MyHelpers
def id
integer an_option: true
end
end
To implement this API I have something like this
module DefWithName
def integer(name, options = {})
store_result name, MyInteger.new(options)
end
end
module DefWithoutName
def integer(options = {})
store_result MyInteger.new(options)
end
end
class FooWithName < BasicObject
include DefWithName
def store_result(name, object)
@props[name] = object
end
end
class BarWithoutName < BasicObject
include DefWithoutName
def store_result(object)
@items << object
end
end
===========================
My goal is to provide a way for users to write their “id” method once
and
to work in both cases.
I am thinking about something like that:
def id(*args)
if name_required?
name = name_given? ? given_name : “id”
end
integer name, an_option: true
end
def integer(*args)
value = MyInteger.new(options)
if name_required?
if name_given?
store_result given_name, value
else
raise ArgumentError
end
else
if name_given?
raise ArgumentError
end
store_result value
end
end
where “name_required?”, “given_name” and “name_given?” would be defined
appropriately based on the arguments passed to the method.
I hope this is clearer now.
–
Nicolas D.