Class: SknUtils::NestedResultBase
- Inherits:
-
Object
- Object
- SknUtils::NestedResultBase
- Includes:
- ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, AttributeHelpers
- Defined in:
- lib/skn_utils/nested_result_base.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#clean_key(original) ⇒ Object
Some keys have chars not suitable for symbol keys.
-
#depth_level ⇒ Object
enablement for latter additions.
-
#initialize(params = {}) ⇒ NestedResultBase
constructor
:depth controls how deep into input hash/arrays we convert :depth => :single | :multi | :multi_with_arrays :depth defaults to :multi :enable_serialization controls the use of singleton_method() to preserve the ability to Marshal :enable_serialization defaults to false.
-
#multi_level_incl_arrays_initializer(params = {}) ⇒ Object
Multi Level Initializer including Arrays of Hashes.
-
#multi_level_initializer(params = {}) ⇒ Object
Multi Level Initializer – value eql hash then interate.
-
#serialization_required? ⇒ Boolean
enablement for latter additions.
-
#single_level_initializer(params = {}) ⇒ Object
Single Level Initializer – ignore value eql hash.
Methods included from AttributeHelpers
#[], #[]=, #attribute_helper_object, #attributes, #to_hash
Constructor Details
#initialize(params = {}) ⇒ NestedResultBase
:depth controls how deep into input hash/arrays we convert :depth => :single | :multi | :multi_with_arrays :depth defaults to :multi :enable_serialization controls the use of singleton_method() to preserve the ability to Marshal :enable_serialization defaults to false
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/skn_utils/nested_result_base.rb', line 159 def initialize(params={}) @skn_enabled_depth = params.delete(:depth) {|not_found| :multi } @skn_enable_serialization = params.delete(:enable_serialization) {|not_found| false } case depth_level when :single single_level_initializer(params) when :multi_with_arrays multi_level_incl_arrays_initializer(params) else multi_level_initializer(params) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class SknUtils::AttributeHelpers
Instance Method Details
#clean_key(original) ⇒ Object
Some keys have chars not suitable for symbol keys
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/skn_utils/nested_result_base.rb', line 217 def clean_key(original) formatted_key = original.to_s if /^[#|@|:]/.match(formatted_key) # filter out (@xsi) from '@xsi:type' keys label = /@(.+):(.+)/.match(formatted_key) || /[#|@|:](.+)/.match(formatted_key) || [] formatted_key = case label.size when 1 label[1].to_s when 2 "#{label[1]}_#{label[2]}" else original # who knows what it was, give it back end end formatted_key end |
#depth_level ⇒ Object
enablement for latter additions
212 213 214 |
# File 'lib/skn_utils/nested_result_base.rb', line 212 def depth_level @skn_enabled_depth end |
#multi_level_incl_arrays_initializer(params = {}) ⇒ Object
Multi Level Initializer including Arrays of Hashes
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/skn_utils/nested_result_base.rb', line 192 def multi_level_incl_arrays_initializer(params={}) # Multi Level Initializer including Arrays of Hashes params.each do |k,v| key = clean_key(k) singleton_class.send(:attr_accessor, key) unless respond_to?(key) or serialization_required? if v.kind_of?(Array) and v.first.kind_of?(Hash) instance_variable_set("@#{key}".to_sym, (v.map {|nobj| self.class.new(nobj)}) ) elsif v.kind_of?(Hash) instance_variable_set("@#{key}".to_sym, self.class.new(v)) else instance_variable_set("@#{key}".to_sym,v) end end end |
#multi_level_initializer(params = {}) ⇒ Object
Multi Level Initializer – value eql hash then interate
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/skn_utils/nested_result_base.rb', line 180 def multi_level_initializer(params={}) # Multi Level Initializer -- value eql hash then interate params.each do |k,v| key = clean_key(k) singleton_class.send(:attr_accessor, key) unless respond_to?(key) or serialization_required? if v.kind_of?(Hash) instance_variable_set("@#{key}".to_sym, self.class.new(v)) else instance_variable_set("@#{key}".to_sym,v) end end end |
#serialization_required? ⇒ Boolean
enablement for latter additions
207 208 209 |
# File 'lib/skn_utils/nested_result_base.rb', line 207 def serialization_required? @skn_enable_serialization end |
#single_level_initializer(params = {}) ⇒ Object
Single Level Initializer – ignore value eql hash
172 173 174 175 176 177 178 |
# File 'lib/skn_utils/nested_result_base.rb', line 172 def single_level_initializer(params={}) # Single Level Initializer -- ignore value eql hash params.each do |k,v| key = clean_key(k) singleton_class.send(:attr_accessor, key) unless respond_to?(key) or serialization_required? instance_variable_set("@#{key}".to_sym,v) end end |