Class: Toolchest::Parameters
- Inherits:
-
Object
- Object
- Toolchest::Parameters
- Defined in:
- lib/toolchest/parameters.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #except(*keys) ⇒ Object
- #fetch(key, *args, &block) ⇒ Object
-
#initialize(raw = {}, tool_definition: nil) ⇒ Parameters
constructor
A new instance of Parameters.
- #inspect ⇒ Object
- #key?(key) ⇒ Boolean (also: #has_key?, #include?)
- #merge(other) ⇒ Object
- #permit(*keys) ⇒ Object
- #require(key) ⇒ Object
- #slice(*keys) ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
Constructor Details
#initialize(raw = {}, tool_definition: nil) ⇒ Parameters
Returns a new instance of Parameters.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/toolchest/parameters.rb', line 5 def initialize(raw = {}, tool_definition: nil) @raw = raw.is_a?(Hash) ? raw : {} if tool_definition allowed_keys = tool_definition.params.map { |p| p.name.to_s } filtered = @raw.select { |k, _| allowed_keys.include?(k.to_s) } @params = ActiveSupport::HashWithIndifferentAccess.new(filtered) else @params = ActiveSupport::HashWithIndifferentAccess.new(@raw) end end |
Instance Method Details
#[](key) ⇒ Object
17 18 19 |
# File 'lib/toolchest/parameters.rb', line 17 def [](key) @params[key] end |
#each(&block) ⇒ Object
65 |
# File 'lib/toolchest/parameters.rb', line 65 def each(&block) = @params.each(&block) |
#empty? ⇒ Boolean
63 |
# File 'lib/toolchest/parameters.rb', line 63 def empty? = @params.empty? |
#except(*keys) ⇒ Object
32 |
# File 'lib/toolchest/parameters.rb', line 32 def except(*keys) = @params.except(*keys.map(&:to_s)) |
#fetch(key, *args, &block) ⇒ Object
21 |
# File 'lib/toolchest/parameters.rb', line 21 def fetch(key, *args, &block) = @params.fetch(key, *args, &block) |
#inspect ⇒ Object
69 |
# File 'lib/toolchest/parameters.rb', line 69 def inspect = "#<Toolchest::Parameters #{@params.inspect}>" |
#key?(key) ⇒ Boolean Also known as: has_key?, include?
23 |
# File 'lib/toolchest/parameters.rb', line 23 def key?(key) = @params.key?(key) |
#merge(other) ⇒ Object
67 |
# File 'lib/toolchest/parameters.rb', line 67 def merge(other) = self.class.new(@params.merge(other)) |
#permit(*keys) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/toolchest/parameters.rb', line 42 def permit(*keys) permitted = {} keys.each do |key| case key when Symbol, String permitted[key.to_s] = @params[key] if @params.key?(key.to_s) when Hash key.each do |k, v| if @params.key?(k.to_s) && @params[k].is_a?(Array) permitted[k.to_s] = @params[k].map do |item| item.is_a?(Hash) ? item.slice(*v.map(&:to_s)) : item end elsif @params.key?(k.to_s) && @params[k].is_a?(Hash) permitted[k.to_s] = @params[k].slice(*v.map(&:to_s)) end end end end ActiveSupport::HashWithIndifferentAccess.new(permitted) end |
#require(key) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/toolchest/parameters.rb', line 34 def require(key) value = @params[key] if value.nil? && !@params.key?(key.to_s) raise Toolchest::ParameterMissing, "param is missing or the value is empty: #{key}" end value end |
#slice(*keys) ⇒ Object
30 |
# File 'lib/toolchest/parameters.rb', line 30 def slice(*keys) = @params.slice(*keys.map(&:to_s)) |
#to_h ⇒ Object Also known as: to_hash
27 |
# File 'lib/toolchest/parameters.rb', line 27 def to_h = @params.to_h |