Class: CollectionUtils::HashDeserializedObject

Inherits:
Object
  • Object
show all
Defined in:
lib/collection_utils/hash_deserialized_object.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ HashDeserializedObject

Returns a new instance of HashDeserializedObject.



20
21
22
23
24
25
# File 'lib/collection_utils/hash_deserialized_object.rb', line 20

def initialize(hash = {})
  @original = hash
  hash.each do |key, value|
    insert(key, value)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(argument_name, *args) ⇒ Object

Add arguments on the runtime. Define attr_accessor for the arguments and get original hash for the same. Delete the unecessary arguments using delete method

> var = CollectionUtils::HashDeserializedObject.new

> var.query.bool.must.match = “hello”

> var.get_serialized_object #:query=>{:bool=>{:must=>{“match”=>“hello”}}}

Examples:

Add argument on runtime

Parameters:

  • argument_name (Symbol)

    name of the argument that needs to be added

  • *args

    value of the argument



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/collection_utils/hash_deserialized_object.rb', line 36

def method_missing(argument_name, *args)
  if argument_name.to_s == "original"
    super
    return
  end
  name = argument_name
  if name.to_s.include?"="
    name = name.to_s.split("=").first
    insert(name, args.first)
  else
    insert(name, {})
  end
  return instance_variable_get("@#{name}")
end

Instance Method Details

#delete(name) ⇒ Object

Delete the key value pair from deserialized object. This will rmeove the attr_accessor from object and key value from hash.

> obj = CollectionUtils::HashDeserializedObject.new()

> obj.insert(:name, “CollectionUtils”)

> obj.name #CollectionUtils

> obj.insert(:type, “HashDeserializedObject”)

> obj.type #HashDeserializedObject

> value = obj.delete(:name) #CollectionUtils

> obj.get_serialized_object #“HashDeserializedObject”

Examples:

Delete from object

Parameters:

  • name

    attr_accessor name to be deleted

Returns:

  • value of the deleted attribute



89
90
91
92
93
94
# File 'lib/collection_utils/hash_deserialized_object.rb', line 89

def delete(name)
  @original.delete(name)
  instance_eval("undef :#{name.to_s}")
  instance_eval("undef :#{name.to_s}=")
  return remove_instance_variable("@#{name.to_s}")
end

#get_serialized_objectHash

Get original Hash used to build the object. It will grow as we insert more values in the object

> hash = {:message1 => “welcome”, :message2 => “to” }

> hash_new = CollectionUtils::HashDeserializedObject.new(hash)

> hash_new(:message3, “Collections”)

> hash = hash_new.get_serialized_object # hash = => “welcome”, :message2 => “to”, :message3 => “collections”

Examples:

Create Object from hash, change the hash and get original

Returns:

  • (Hash)

    original hash used to build the object



104
105
106
# File 'lib/collection_utils/hash_deserialized_object.rb', line 104

def get_serialized_object
  return @original
end

#insert(name, value) ⇒ Object

Insert new key value pair in deserialized object. This will create an attr_accessor with key and value as given value

> obj = CollectionUtils::HashDeserializedObject.new()

> obj.insert(:name, “CollectionUtils”)

> obj.name # CollectionUtils

Examples:

Insert an value in deserialized object and access it

Parameters:

  • name

    key of the hash

  • value

    value of the given jey



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/collection_utils/hash_deserialized_object.rb', line 59

def insert(name, value)
  @original[name] = value
  if value.class.name == "Hash" || value.class.superclass.name == "Hash"
    value = CollectionUtils::HashDeserializedObject.new(value)
  end
  name = convert_name(name.to_s)
  self.class.send(:attr_accessor, name)
  instance_variable_set("@#{name}", value)
  define_singleton_method "#{name}=".to_sym do |arg|
    @original[name] = arg
    instance_variable_set("@#{name}", arg)
  end
  define_singleton_method "#{name}".to_sym do
    instance_variable_get("@#{name}")
  end
end