Class: Memoized::Parameters
- Inherits:
-
Object
- Object
- Memoized::Parameters
- Defined in:
- lib/memoized/parameters.rb
Constant Summary collapse
- UNIQUE =
42.freeze
Instance Attribute Summary collapse
-
#key_params ⇒ Object
Returns the value of attribute key_params.
-
#keyreq_params ⇒ Object
Returns the value of attribute keyreq_params.
-
#keyrest_params ⇒ Object
Returns the value of attribute keyrest_params.
-
#opt_params ⇒ Object
Returns the value of attribute opt_params.
-
#req_params ⇒ Object
Returns the value of attribute req_params.
-
#rest_params ⇒ Object
Returns the value of attribute rest_params.
Class Method Summary collapse
Instance Method Summary collapse
- #cache_key ⇒ Object
-
#initialize(parameters = []) ⇒ Parameters
constructor
A new instance of Parameters.
- #params ⇒ Object
- #signature ⇒ Object
Constructor Details
#initialize(parameters = []) ⇒ Parameters
Returns a new instance of Parameters.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/memoized/parameters.rb', line 7 def initialize(parameters = []) # This constructor does not check, whether the parameters were ordered correctly # with respect to the Ruby language specification. However, all outputs will be sorted correctly. @req_params = [] @opt_params = [] @rest_params = [] @keyreq_params = [] @key_params = [] @keyrest_params = [] parameters.each do |(param_type, param_name)| case param_type when :req @req_params << [param_type, param_name] when :opt @opt_params << [param_type, param_name] when :rest @rest_params << [param_type, param_name] when :keyreq @keyreq_params << [param_type, param_name] when :key @key_params << [param_type, param_name] when :keyrest @keyrest_params << [param_type, param_name] when :block raise Memoized::CannotMemoize, 'Cannot memoize a method that takes a block!' else raise Memoized::CannotMemoize, 'Unknown parameter type!' end end if @rest_params.size > 1 || @keyrest_params.size > 1 raise Memoized::CannotMemoize "Multiple rest or keyrest parameters, invalid signature!" end end |
Instance Attribute Details
#key_params ⇒ Object
Returns the value of attribute key_params.
5 6 7 |
# File 'lib/memoized/parameters.rb', line 5 def key_params @key_params end |
#keyreq_params ⇒ Object
Returns the value of attribute keyreq_params.
5 6 7 |
# File 'lib/memoized/parameters.rb', line 5 def keyreq_params @keyreq_params end |
#keyrest_params ⇒ Object
Returns the value of attribute keyrest_params.
5 6 7 |
# File 'lib/memoized/parameters.rb', line 5 def keyrest_params @keyrest_params end |
#opt_params ⇒ Object
Returns the value of attribute opt_params.
5 6 7 |
# File 'lib/memoized/parameters.rb', line 5 def opt_params @opt_params end |
#req_params ⇒ Object
Returns the value of attribute req_params.
5 6 7 |
# File 'lib/memoized/parameters.rb', line 5 def req_params @req_params end |
#rest_params ⇒ Object
Returns the value of attribute rest_params.
5 6 7 |
# File 'lib/memoized/parameters.rb', line 5 def rest_params @rest_params end |
Class Method Details
.to_cache_key(param_type, param_name) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/memoized/parameters.rb', line 80 def self.to_cache_key((param_type, param_name)) case param_type when :req "all_args.push(#{param_name})" when :opt "all_args.push(#{param_name}) unless #{param_name}.equal?(Memoized::Parameters::UNIQUE)" when :rest "all_args.push(*#{param_name})" when :keyreq "all_kwargs[:#{param_name}] = #{param_name}" when :key "all_kwargs[:#{param_name}] = #{param_name} unless #{param_name}.equal?(Memoized::Parameters::UNIQUE)" when :keyrest "all_kwargs.merge!(#{param_name})" else raise "unknown parameter type" end end |
.to_signature(param_type, param_name) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/memoized/parameters.rb', line 51 def self.to_signature((param_type, param_name)) case param_type when :req "#{param_name}" when :opt "#{param_name} = Memoized::Parameters::UNIQUE" when :rest "*#{param_name}" when :keyreq "#{param_name}:" when :key "#{param_name}: Memoized::Parameters::UNIQUE" when :keyrest "**#{param_name}" else raise "unknown parameter type" end end |
Instance Method Details
#cache_key ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/memoized/parameters.rb', line 69 def cache_key <<-STRING all_args = [] all_kwargs = {} #{params.map(&Parameters.method(:to_cache_key)).join("\n")} cache_key = [all_args, all_kwargs] STRING end |
#params ⇒ Object
43 44 45 |
# File 'lib/memoized/parameters.rb', line 43 def params @req_params + @opt_params + @rest_params + @keyreq_params + @key_params + @keyrest_params end |
#signature ⇒ Object
47 48 49 |
# File 'lib/memoized/parameters.rb', line 47 def signature params.map(&Parameters.method(:to_signature)).join(', ') end |