Module: Pigeon::NestedScopes

Included in:
Channel, ChannelAttribute, ChannelSchema
Defined in:
app/models/pigeon/nested_scopes.rb

Instance Method Summary collapse

Instance Method Details

#find_attr_recursive(name, attributes) ⇒ Object



4
5
6
7
# File 'app/models/pigeon/nested_scopes.rb', line 4

def find_attr_recursive(name, attributes)
  hash, key = find_attr_ref_recursive(name, attributes)
  hash && hash[key]
end

#find_attr_ref_recursive(name, attributes) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/pigeon/nested_scopes.rb', line 9

def find_attr_ref_recursive(name, attributes)
  fold_scoped_name(name, [attributes, nil]) do |(hash, key), name|
    if key.nil?
      [hash, name]
    elsif !hash.nil? && hash[key].is_a?(Hash)
      [hash[key], name]
    else
      [nil, nil]
    end
  end
end

#fold_scoped_name(name, initial, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'app/models/pigeon/nested_scopes.rb', line 21

def fold_scoped_name(name, initial, &block)
  m = name.to_s.match(/\A(?<name>\w+)(\[(?<nested>\w+)\](?<rest>.*))?\Z/)
  if m.nil?
    raise ArgumentError, "invalid scoped name #{name}"
  elsif m[:nested].nil?
    yield(initial, m[:name])
  else
    fold_scoped_name(m[:nested] + m[:rest], yield(initial, m[:name]), &block)
  end
end