Class: NsOptions::Options

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, parent = nil) ⇒ Options

Returns a new instance of Options.



6
7
8
9
10
# File 'lib/ns-options/options.rb', line 6

def initialize(key, parent = nil)
  self.key = key.to_s
  self.parent = parent
  self.namespaces = NsOptions::Namespaces.new
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/ns-options/options.rb', line 4

def key
  @key
end

#namespacesObject

Returns the value of attribute namespaces.



4
5
6
# File 'lib/ns-options/options.rb', line 4

def namespaces
  @namespaces
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/ns-options/options.rb', line 4

def parent
  @parent
end

Instance Method Details

#[](name) ⇒ Object



12
13
14
# File 'lib/ns-options/options.rb', line 12

def [](name)
  super(name.to_sym)
end

#[]=(name, value) ⇒ Object



15
16
17
# File 'lib/ns-options/options.rb', line 15

def []=(name, value)
  super(name.to_sym, value)
end

#add(*args) ⇒ Object



19
20
21
22
# File 'lib/ns-options/options.rb', line 19

def add(*args)
  option = NsOptions::Option.new(*args)
  self[option.name] = option
end

#add_namespace(name, key = nil, parent = nil, &block) ⇒ Object



49
50
51
52
# File 'lib/ns-options/options.rb', line 49

def add_namespace(name, key = nil, parent = nil, &block)
  key ||= name
  self.namespaces.add(name, key, parent, &block)
end

#build_from(options, namespace) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ns-options/options.rb', line 62

def build_from(options, namespace)
  options.each do |key, option|
    self.add(option.name, option.type_class, option.rules)
    NsOptions::Helper.find_and_define_option(namespace, option.name)
  end
  options.namespaces.each do |name, ns|
    new_namespace = self.add_namespace(name, ns.options.key, ns.options.parent)
    NsOptions::Helper.find_and_define_namespace(namespace, name)
    new_namespace.options.build_from(ns.options, new_namespace)
  end
end

#del(name) ⇒ Object Also known as: remove



24
25
26
# File 'lib/ns-options/options.rb', line 24

def del(name)
  self[name] = nil
end

#get(name) ⇒ Object



29
30
31
32
# File 'lib/ns-options/options.rb', line 29

def get(name)
  option = self[name]
  option and option.value
end

#get_namespace(name) ⇒ Object



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

def get_namespace(name)
  self.namespaces[name]
end

#is_defined?(name) ⇒ Boolean

Returns:



39
40
41
# File 'lib/ns-options/options.rb', line 39

def is_defined?(name)
  !!self[name]
end

#is_namespace_defined?(name) ⇒ Boolean

Returns:



58
59
60
# File 'lib/ns-options/options.rb', line 58

def is_namespace_defined?(name)
  !!self.get_namespace(name)
end

#required_set?Boolean

Returns:



43
44
45
46
47
# File 'lib/ns-options/options.rb', line 43

def required_set?
  self.values.reject{|option| !option.required? }.inject(true) do |bool, option|
    bool && option.is_set?
  end
end

#set(name, new_value) ⇒ Object



34
35
36
37
# File 'lib/ns-options/options.rb', line 34

def set(name, new_value)
  self[name].value = new_value
  self[name]
end