Module: NsOptions::Helper

Defined in:
lib/ns-options/helper.rb,
lib/ns-options/helper/advisor.rb

Defined Under Namespace

Classes: Advisor

Class Method Summary collapse

Class Method Details

.advisor(namespace = nil) ⇒ Object



52
53
54
# File 'lib/ns-options/helper.rb', line 52

def advisor(namespace=nil)
  NsOptions::Helper::Advisor.new(namespace)
end

.define_namespace_methods(namespace, name) ⇒ Object



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

def define_namespace_methods(namespace, name)
  namespace.metaclass.class_eval <<-DEFINE_METHOD

    def #{name}(&block)
      namespace = self.options.namespaces.get("#{name}")
      namespace.define(&block) if block
      namespace
    end

  DEFINE_METHOD
end

.define_option_methods(namespace, option) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ns-options/helper.rb', line 33

def define_option_methods(namespace, option)
  namespace.metaclass.class_eval <<-DEFINE_METHOD

    def #{option.name}(*args)
      if !args.empty?
        self.send("#{option.name}=", *args)
      else
        self.options.get(:#{option.name})
      end
    end

    def #{option.name}=(*args)
      value = args.size == 1 ? args.first : args
      self.options.set(:#{option.name}, value)
    end

  DEFINE_METHOD
end

.define_root_namespace_methods(define_on, name, key = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ns-options/helper.rb', line 56

def define_root_namespace_methods(define_on, name, key=nil)
  key ||= name.to_s

  # covers defining on Modules and at the class-level of Classes
  method_definitions = <<-CLASS_METHOD

    def self.#{name}(&block)
      @#{name} ||= NsOptions::Namespace.new('#{key}', &block)
    end

  CLASS_METHOD

  if define_on.kind_of?(Class)
    # covers defining at the instance-level of Classes
    method_definitions += <<-INSTANCE_METHOD

      def #{name}(&block)
        unless @#{name}
          @#{name} = NsOptions::Namespace.new('#{key}', &block)
          @#{name}.options.build_from(self.class.#{name}.options, @#{name})
        end
        @#{name}
      end

    INSTANCE_METHOD
  end
  define_on.class_eval(method_definitions)
end

.find_and_define_namespace(namespace, name) ⇒ Object



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

def find_and_define_namespace(namespace, name)
  sub_namespace = namespace.options.get_namespace(name)
  self.define_namespace_methods(namespace, name)
  sub_namespace
end

.find_and_define_option(namespace, option_name) ⇒ Object



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

def find_and_define_option(namespace, option_name)
  option = namespace.options[option_name]
  self.define_option_methods(namespace, option)
  option
end