Skip to content

Commit

Permalink
- [FEATURE] Added declared_parameters_for method.
Browse files Browse the repository at this point in the history
- [FEATURE] You can now limit/filter the list of declared parameters based on
  type:

      has_named_parameters :foobar,
        :required => :a, :optional => [ :b, :c ]
      def foobar
        declared_parameters(:required).inspect
      end

      foobar  # => [ :a ]
  • Loading branch information
jurisgalang committed Nov 30, 2010
1 parent 4991707 commit 24fffda
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
60 changes: 59 additions & 1 deletion README.md
Expand Up @@ -158,12 +158,70 @@ method:
end

# create an instance of GoogleStorage
# and print: [ access-key, secret-key, group-email, apps-domain ]
# and print: [ :access-key, :secret-key, :group-email, :apps-domain ]
GoogleStorage.new :'access-key' => '...', :'secret-key' => '...'

`declared_parameters` is also available from the class methods of user defined
classes.

You can also pass a list of parameter types to limit the result to specific
parameter types:

class GoogleStorage
requires :'access-key', :'secret-key'
recognizes [ :'group-email', 'group@example.org' ], [ :'apps-domain', 'example.org' ]

def initialize options
# list the parameters declared
puts "#{declared_parameters(:required).join(' ')}"

# ... now do the googly stuff ...
end
end

# create an instance of GoogleStorage
# and print: [ :access-key, :secret-key ]
GoogleStorage.new :'access-key' => '...', :'secret-key' => '...'

The method `declared_parameters` is context specific. It returns the list of
parameters for the current method. To get a list of parameters for a specific
method, use `declared_parameters_for`:

class GoogleStorage
requires :'access-key', :'secret-key'
recognizes [ :'group-email', 'group@example.org' ], [ :'apps-domain', 'example.org' ]

def initialize options
# list the parameters declared
puts "#{declared_parameters(:required).join(' ')}"

# ... now do the googly stuff ...
end
def self.required_parameters
declared_parameters_for :new, :required
end

def self.optional_parameters
declared_parameters_for :new, :optional
end

def self.all_parameters
declared_parameters_for :new
end
end

# list the required parameters for the class
GoogleStorage.required_parameters # => [ :access-key, :secret-key ]

# list the optional parameters for the class
GoogleStorage.required_parameters # => [ :group-email, :apps-domain ]

# list all of the recognized parameters for the class
GoogleStorage.all_parameters # => [ :access-key, :secret-key, :group-email, :apps-domain ]

Notice that both methods may receive a filter of parameter types.

Filtering Arguments
-------------------
Sometimes you'll have a `Hash` object that will have a bunch of keys that may
Expand Down
15 changes: 14 additions & 1 deletion RELEASENOTES
@@ -1,3 +1,16 @@
0.0.14 [Nov 29, 2010]
- [FEATURE] Added declared_parameters_for method.
- [FEATURE] You can now limit/filter the list of declared parameters based on
type:

has_named_parameters :foobar,
:required => :a, :optional => [ :b, :c ]
def foobar
declared_parameters(:required).inspect
end

foobar # => [ :a ]

0.0.13 [Nov 28, 2010]
- [INTERNAL] instrument -> apply_method_spec, to make it compatible with Rails.

Expand All @@ -18,7 +31,7 @@
declared_parameters.inspect
end

foobar # => [ :a, :b, :c ]
foobar # => [ :a, :b, :c ]

- [INTERNAL] Parameter spec table is now retained.

Expand Down
11 changes: 9 additions & 2 deletions lib/named-parameters/module.rb
Expand Up @@ -33,10 +33,13 @@ def self.included base # :nodoc:
# declared in the the `has_named_parameters` clause, or the list specified
# in either the `requires` and `recognizes` clause.
#
# @param [Array<Symbol>] type limits the list of parameters returned to the
# parameter types specified. Defaults to `[ :required, :optional, :oneof ]`
#
# @return [Array<Symbol>] the list of symbols representing the name of the declared
# parameters.
#
def declared_parameters
def declared_parameters type = [ :required, :optional, :oneof ]
klazz = self.instance_of?(Class) ? self : self.class
specs = klazz.send :specs

Expand All @@ -48,7 +51,11 @@ def declared_parameters
mapper = lambda{ |entry| entry.instance_of?(Hash) ? entry.keys.first : entry }
sorter = lambda{ |x, y| x.to_s <=> y.to_s }

[ :required, :optional, :oneof ].map{ |k| spec[k].map(&mapper) }.flatten.sort(&sorter)
Array(type).map{ |k| spec[k].map(&mapper) }.flatten.sort(&sorter)
end

def declared_parameters_for method, type = [ :required, :optional, :oneof ]
declared_parameters(type) { method }
end

# Filter out keys from `options` that are not declared as parameter to the
Expand Down

0 comments on commit 24fffda

Please sign in to comment.