Class: NsOptions::Helper::Advisor

Inherits:
Object
  • Object
show all
Defined in:
lib/ns-options/helper/advisor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ Advisor

Returns a new instance of Advisor.



9
10
11
# File 'lib/ns-options/helper/advisor.rb', line 9

def initialize(namespace)
  self.namespace = namespace
end

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



7
8
9
# File 'lib/ns-options/helper/advisor.rb', line 7

def namespace
  @namespace
end

Instance Method Details

#bad_method_message(kind, name) ⇒ Object



67
68
69
70
71
# File 'lib/ns-options/helper/advisor.rb', line 67

def bad_method_message(kind, name)
  [ "The #{kind} '#{name}' overwrites a namespace method that NsOptions depends on.",
    "Please choose a different name for your #{kind}."
  ].join(" ")
end

#bad_methodsObject



59
60
61
# File 'lib/ns-options/helper/advisor.rb', line 59

def bad_methods
  @bad_methods ||= [ :option, :namespace, :define, :options ]
end

#duplicate_message(name) ⇒ Object



73
74
75
76
77
# File 'lib/ns-options/helper/advisor.rb', line 73

def duplicate_message(name)
  [ "WARNING! '#{name}' has already been defined and will be overwritten.",
    "It's likely that it will not behave as expected."
  ].join(" ")
end

#is_already_defined?(name) ⇒ Boolean

Returns:



54
55
56
57
# File 'lib/ns-options/helper/advisor.rb', line 54

def is_already_defined?(name)
  self.namespace.options.is_defined?(name) ||
  self.namespace.options.is_namespace_defined?(name)
end

#is_this_namespace_ok?(name, from = nil) ⇒ Boolean

root namespace advisor

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/ns-options/helper/advisor.rb', line 15

def is_this_namespace_ok?(name, from = nil)
  if [:options].include?(name.to_sym)
    puts self.not_recommended_method_message('namespace', name)
  else
    return true
  end
  puts "From: #{(from || caller).first}"
  false
end

#is_this_ok?(kind, name, from) ⇒ Boolean

helper methods

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ns-options/helper/advisor.rb', line 37

def is_this_ok?(kind, name, from)
  display = kind.to_s
  if self.bad_methods.include?(name.to_sym)
    message = self.bad_method_message(display, name)
    exception = NsOptions::Errors::InvalidName.new(message, from)
    raise(exception)
  elsif self.is_already_defined?(name)
    puts self.duplicate_message(name)
  elsif self.not_recommended_methods.include?(name.to_sym)
    puts self.not_recommended_method_message(display, name)
  else
    return true
  end
  puts "From: #{from.first}"
  false
end

#is_this_option_ok?(name, from = nil) ⇒ Boolean

Returns:



31
32
33
# File 'lib/ns-options/helper/advisor.rb', line 31

def is_this_option_ok?(name, from = nil)
  self.is_this_ok?(:option, name, (from || caller))
end

#is_this_sub_namespace_ok?(name, from = nil) ⇒ Boolean

sub-namespace / namespace-option advisors

Returns:



27
28
29
# File 'lib/ns-options/helper/advisor.rb', line 27

def is_this_sub_namespace_ok?(name, from = nil)
  self.is_this_ok?(:'sub-namespace', name, (from || caller))
end


79
80
81
82
83
# File 'lib/ns-options/helper/advisor.rb', line 79

def not_recommended_method_message(kind, name)
  [ "WARNING! The #{kind} '#{name}' overwrites a method NsOptions depends on.",
    "This will limit some of the functionality of NsOptions."
  ].join(" ")
end


63
64
65
# File 'lib/ns-options/helper/advisor.rb', line 63

def not_recommended_methods
  @not_recommended_methods ||= NsOptions::Namespace.instance_methods(false).map(&:to_sym)
end