Class: NsOptions::NamespaceData

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

Defined Under Namespace

Classes: DslMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ns, option_type_class) ⇒ NamespaceData

Returns a new instance of NamespaceData.



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

def initialize(ns, option_type_class)
  @ns, @option_type_class = ns, option_type_class
  @child_namespaces       = NsOptions::Namespaces.new
  @child_options          = NsOptions::Options.new
end

Instance Attribute Details

#child_namespacesObject (readonly)

Returns the value of attribute child_namespaces.



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

def child_namespaces
  @child_namespaces
end

#child_optionsObject (readonly)

Returns the value of attribute child_options.



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

def child_options
  @child_options
end

#nsObject (readonly)

Returns the value of attribute ns.



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

def ns
  @ns
end

#option_type_classObject (readonly)

Returns the value of attribute option_type_class.



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

def option_type_class
  @option_type_class
end

Instance Method Details

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



38
39
40
41
42
43
44
# File 'lib/ns-options/namespace_data.rb', line 38

def add_namespace(name, option_type_class=nil, &block)
  opt_type_class = option_type_class || @option_type_class
  ns = NsOptions::Namespace.new(name, opt_type_class, &block)

  @child_options.rm(name)
  @child_namespaces.add(name, ns)
end

#add_option(*args) ⇒ Object



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

def add_option(*args)
  name = args.first
  opt  = NsOptions::Option.new(*NsOptions::Option.args(args, @option_type_class))

  @child_namespaces.rm(name)
  @child_options.add(name, opt)
end

#apply(values = nil) ⇒ Object

The opposite of #to_hash. Takes a hash representation of options and namespaces and mass assigns option values.



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

def apply(values=nil)
  (values || {}).each do |name, value|
    if has_namespace?(name)
      # recursively apply namespace values if hash given; ignore otherwise.
      get_namespace(name).apply(value) if value.kind_of?(Hash)
    else
      # this is meant as a "value importer", so always apply distinct values
      # to prevent unintentional pass-by-ref shared objects.
      # be sure to use the namespace's writer to write the option value
      @ns.send("#{name}=", NsOptions.distinct_value(value))
    end
  end
end

#build_from(other_ns_data) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ns-options/namespace_data.rb', line 91

def build_from(other_ns_data)
  set_option_type_class(other_ns_data.option_type_class)

  other_ns_data.child_options.each do |name, opt|
    add_option(name, opt.type_class, opt.rules)
  end

  other_ns_data.child_namespaces.each do |name, ns|
    new_ns = add_namespace(name)
    new_ns.build_from(ns)
  end
end

#define(&block) ⇒ Object

define the parent ns using the given block



82
83
84
85
86
87
88
89
# File 'lib/ns-options/namespace_data.rb', line 82

def define(&block)
  if block && block.arity > 0
    block.call @ns
  elsif block
    @ns.instance_eval(&block)
  end
  @ns
end

#eachObject

allow for iterating over the key/values of a namespace this uses #to_hash so you won’t get option/namespace objs for the values



77
78
79
# File 'lib/ns-options/namespace_data.rb', line 77

def each
  to_hash.each{|k,v| yield k,v if block_given? }
end

#get_namespace(name) ⇒ Object



37
# File 'lib/ns-options/namespace_data.rb', line 37

def get_namespace(name);   @child_namespaces.get(name);    end

#get_option(name) ⇒ Object



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

def get_option(name);      @child_options.get(name);       end

#has_namespace?(name) ⇒ Boolean

Returns:



36
# File 'lib/ns-options/namespace_data.rb', line 36

def has_namespace?(name);  !!@child_namespaces[name];      end

#has_option?(name) ⇒ Boolean

Returns:



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

def has_option?(name);     !!@child_options[name];         end

#ns_method_missing(meth, *args, &block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ns-options/namespace_data.rb', line 132

def ns_method_missing(meth, *args, &block)
  dslm = DslMethod.new(meth, *args, &block)

  if is_namespace_reader?(dslm)
    get_namespace(dslm.name).define(&block)
  elsif is_option_reader?(dslm)
    get_option(dslm.name)
  elsif is_option_writer?(dslm)
    add_option(dslm.name) unless has_option?(dslm.name)
    set_option(dslm.name, dslm.data)
  else # namespace writer or unknown
    raise NoMethodError.new("undefined method `#{meth}' for #{@ns.inspect}")
  end
end

#ns_respond_to?(meth) ⇒ Boolean

Returns:



123
124
125
126
127
128
129
130
# File 'lib/ns-options/namespace_data.rb', line 123

def ns_respond_to?(meth)
  dslm = DslMethod.new(meth)

  has_namespace?(dslm.name) || # namespace reader
  has_option?(dslm.name)    || # option reader
  dslm.writer?              || # dynamic option writer
  false
end

#required_set?Boolean

Recursively check if options that were defined as :required have been set.

Returns:



17
18
19
# File 'lib/ns-options/namespace_data.rb', line 17

def required_set?
  @child_options.required_set? && @child_namespaces.required_set?
end

#resetObject



104
105
106
107
# File 'lib/ns-options/namespace_data.rb', line 104

def reset
  child_options.each {|name, opt| opt.reset}
  child_namespaces.each {|name, ns| ns.reset}
end

#set_option(name, val) ⇒ Object



27
# File 'lib/ns-options/namespace_data.rb', line 27

def set_option(name, val); @child_options.set(name, val);  end

#set_option_type_class(value) ⇒ Object



21
22
23
# File 'lib/ns-options/namespace_data.rb', line 21

def set_option_type_class(value)
  @option_type_class = value
end

#to_hashObject

recursively build a hash representation of the namespace, using symbols for the option/namespace name-keys



48
49
50
51
52
53
54
55
56
57
# File 'lib/ns-options/namespace_data.rb', line 48

def to_hash
  Hash.new.tap do |hash|
    @child_options.each do |name, opt|
      # this is meant to be a "value exporter", so always use distinct values
      # on the returned hash to prevent unintentional pass-by-ref shared objects
      hash[name.to_sym] = NsOptions.distinct_value(opt.value)
    end
    @child_namespaces.each{|name, value| hash[name.to_sym] = value.to_hash}
  end
end