Class: CollectionUtils::HashDeserializedObject
- Inherits:
-
Object
- Object
- CollectionUtils::HashDeserializedObject
- Defined in:
- lib/collection_utils/hash_deserialized_object.rb
Instance Method Summary collapse
-
#delete(name) ⇒ Object
Delete the key value pair from deserialized object.
-
#get_serialized_object ⇒ Hash
Get original Hash used to build the object.
-
#initialize(hash = {}) ⇒ HashDeserializedObject
constructor
A new instance of HashDeserializedObject.
-
#insert(name, value) ⇒ Object
Insert new key value pair in deserialized object.
-
#method_missing(argument_name, *args) ⇒ Object
Add arguments on the runtime.
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”}}}
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”
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_object ⇒ Hash
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”
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
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 |