Class: SknUtils::NestedResultBase

Inherits:
Object
  • Object
show all
Includes:
AttributeHelpers
Defined in:
lib/skn_utils/nested_result_base.rb

Direct Known Subclasses

GenericBean, PageControls, ResultBean, ValueBean

Instance Method Summary collapse

Methods included from AttributeHelpers

#[], #[]=, #attributes, included, #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:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/skn_utils/nested_result_base.rb', line 36

def initialize(params={})
  @skn_enabled_depth = params.delete(:depth) {|not_found| :multi }
  @skn_enable_serialization = params.delete(:enable_serialization) {|not_found| false }
  case @skn_enabled_depth
    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:



98
99
100
# File 'lib/skn_utils/nested_result_base.rb', line 98

def attribute_helper_object
  true
end

#clean_key(original) ⇒ Object

Some keys have chars not suitable for symbol keys => @,#,:,- :nodoc:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/skn_utils/nested_result_base.rb', line 104

def clean_key(original)
  formatted_key = original.to_s.gsub(/[#|@]/,'').gsub(/[:|-]/,'_')

  # 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



92
93
94
# File 'lib/skn_utils/nested_result_base.rb', line 92

def depth_level
  @skn_enabled_depth
end

#multi_level_incl_arrays_initializer(params = {}) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/skn_utils/nested_result_base.rb', line 72

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:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/skn_utils/nested_result_base.rb', line 59

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)


87
88
89
# File 'lib/skn_utils/nested_result_base.rb', line 87

def serialization_required?
  @skn_enable_serialization
end

#single_level_initializer(params = {}) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
# File 'lib/skn_utils/nested_result_base.rb', line 50

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