Class: NsOptions::NamespaceData
- Inherits:
-
Object
- Object
- NsOptions::NamespaceData
- Defined in:
- lib/ns-options/namespace_data.rb
Defined Under Namespace
Classes: DslMethod
Instance Attribute Summary collapse
-
#child_namespaces ⇒ Object
readonly
Returns the value of attribute child_namespaces.
-
#child_options ⇒ Object
readonly
Returns the value of attribute child_options.
-
#ns ⇒ Object
readonly
Returns the value of attribute ns.
-
#option_type_class ⇒ Object
readonly
Returns the value of attribute option_type_class.
Instance Method Summary collapse
- #add_namespace(name, option_type_class = nil, &block) ⇒ Object
- #add_option(*args) ⇒ Object
-
#apply(values = nil) ⇒ Object
The opposite of #to_hash.
- #build_from(other_ns_data) ⇒ Object
-
#define(&block) ⇒ Object
define the parent ns using the given block.
-
#each ⇒ Object
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.
- #get_namespace(name) ⇒ Object
- #get_option(name) ⇒ Object
- #has_namespace?(name) ⇒ Boolean
- #has_option?(name) ⇒ Boolean
-
#initialize(ns, option_type_class) ⇒ NamespaceData
constructor
A new instance of NamespaceData.
- #ns_method_missing(meth, *args, &block) ⇒ Object
- #ns_respond_to?(meth) ⇒ Boolean
-
#required_set? ⇒ Boolean
Recursively check if options that were defined as :required have been set.
- #reset ⇒ Object
- #set_option(name, val) ⇒ Object
- #set_option_type_class(value) ⇒ Object
-
#to_hash ⇒ Object
recursively build a hash representation of the namespace, using symbols for the option/namespace name-keys.
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_namespaces ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute child_options.
8 9 10 |
# File 'lib/ns-options/namespace_data.rb', line 8 def @child_options end |
#ns ⇒ Object (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_class ⇒ Object (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..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 |
#each ⇒ Object
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
36 |
# File 'lib/ns-options/namespace_data.rb', line 36 def has_namespace?(name); !!@child_namespaces[name]; end |
#has_option?(name) ⇒ Boolean
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
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.
17 18 19 |
# File 'lib/ns-options/namespace_data.rb', line 17 def required_set? @child_options.required_set? && @child_namespaces.required_set? end |
#reset ⇒ Object
104 105 106 107 |
# File 'lib/ns-options/namespace_data.rb', line 104 def reset .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_hash ⇒ Object
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 |