Class: UrlPlumber::Plumber
- Inherits:
-
Object
- Object
- UrlPlumber::Plumber
- Defined in:
- lib/url_plumber/plumber.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #extract(key_path) ⇒ Object
-
#initialize(params) ⇒ Plumber
constructor
A new instance of Plumber.
- #plumb(attributes = {}) ⇒ Object
- #update_scope(scope, key_path, value = nil) ⇒ Object
Constructor Details
#initialize(params) ⇒ Plumber
Returns a new instance of Plumber.
7 8 9 |
# File 'lib/url_plumber/plumber.rb', line 7 def initialize params @params = dup(params) end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/url_plumber/plumber.rb', line 5 def params @params end |
Instance Method Details
#extract(key_path) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/url_plumber/plumber.rb', line 11 def extract key_path keys = key_path.to_s.split('.').map(&:to_sym) key = keys[-1] scope = params keys[0..-2].each do |part| scope = scope.fetch(part) { HashWithIndifferentAccess.new } end if scope.has_key?(key) scope[key] else nil end end |
#plumb(attributes = {}) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/url_plumber/plumber.rb', line 27 def plumb attributes = {} scope = dup(params) attributes.each do |key, value| scope = update_scope scope, key, value end scope end |
#update_scope(scope, key_path, value = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/url_plumber/plumber.rb', line 35 def update_scope scope, key_path, value = nil keys = key_path.to_s.split('.').map(&:to_sym) key = keys[-1] scopes = [] keys[0..-2].each do |part| scopes << scope scope = scope.fetch(part) { HashWithIndifferentAccess.new } end if value.nil? scope.delete key else scope[key] = value end keys[0..-2].reverse.each do |part| parent = scopes.pop parent[part] = scope scope = parent end scope end |