Class: SknUtils::NestedResultBase

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, AttributeHelpers
Defined in:
lib/skn_utils/nested_result_base.rb

Instance Method Summary collapse

Methods included from AttributeHelpers

#[], #[]=, #attributes, #respond_to_missing?, #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 :nodoc:



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/skn_utils/nested_result_base.rb', line 160

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

#attribute_helper_objectObject

determines if this is one of our objects :nodoc:



222
223
224
# File 'lib/skn_utils/nested_result_base.rb', line 222

def attribute_helper_object
  true
end

#clean_key(original) ⇒ Object

Some keys have chars not suitable for symbol keys :nodoc:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/skn_utils/nested_result_base.rb', line 228

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_levelObject

enablement for latter additions



216
217
218
# File 'lib/skn_utils/nested_result_base.rb', line 216

def depth_level
  @skn_enabled_depth
end

#multi_level_incl_arrays_initializer(params = {}) ⇒ Object

:nodoc:



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/skn_utils/nested_result_base.rb', line 196

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

:nodoc:



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/skn_utils/nested_result_base.rb', line 183

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

Returns:

  • (Boolean)


211
212
213
# File 'lib/skn_utils/nested_result_base.rb', line 211

def serialization_required?
  @skn_enable_serialization
end

#single_level_initializer(params = {}) ⇒ Object

:nodoc:



174
175
176
177
178
179
180
# File 'lib/skn_utils/nested_result_base.rb', line 174

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