Module: ReservedFor::ModuleMethods

Included in:
ReservedFor
Defined in:
lib/reserved_for.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/reserved_for.rb', line 52

def method_missing(name, *args)
  case name.to_s
  when /(.*)=$/
    args    = args.flatten
    plurals = args.map{ |arg| ActiveSupport::Inflector.pluralize(arg) }
    @reserved_list_map[$1.to_sym]               = Set.new(args)
    @reserved_list_map["_plurals_#{$1}".to_sym] = Set.new(plurals)
  else
    set =  @reserved_list_map[name.to_sym]
    return nil unless set
    set += @reserved_list_map["_plurals_#{name.to_s}".to_sym] if options[:check_plural]
    set
  end
end

Instance Method Details

#any(whitelist: true) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/reserved_for.rb', line 42

def any(whitelist: true)
  set = @reserved_list_map.map{ |k, v|
    next v if options[:check_plural]
    next nil if k =~ /^_plurals_/
    v
  }.compact.inject(:+)
  set -= @reserved_list_map[:whitelist] if whitelist
  set
end

#clear_all!Object



9
10
11
12
# File 'lib/reserved_for.rb', line 9

def clear_all!
  @reserved_list_map = { whitelist: Set.new }
  self
end

#configure(options = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/reserved_for.rb', line 25

def configure(options = {}, &block)
  config = OpenStruct.new
  block.call(config)
  @options = _default_config
    .merge(options)
    .merge(Hash[config.each_pair.map{ |k,v| [k, v] }])

  invalid_options = @options.keys - _default_config.keys
  raise ReservedFor::InvalidOptionError, "invalid options: #{invalid_options}" if invalid_options.size > 0
  reset!(reset_option: false)
  self
end

#optionsObject



38
39
40
# File 'lib/reserved_for.rb', line 38

def options
  @options ||= _default_config
end

#reset!(reset_option: true) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/reserved_for.rb', line 14

def reset!(reset_option: true)
  clear_all!
  @options = nil if reset_option
  if options[:use_default_reserved_list]
    plurals = _default_usernames.map{ |word| ActiveSupport::Inflector.pluralize(word) }
    @reserved_list_map[:usernames] = _default_usernames
    @reserved_list_map[:_plurals_usernames] = Set.new(plurals)
  end
  self
end