Module: JSONConvertible::ClassMethods

Defined in:
lib/json_convertible.rb

Overview

add these as class methods

Instance Method Summary collapse

Instance Method Details

#attribute_key_name_mapHash

class method This method is intended to be overridden by any class that implements JSONConvertible and need to use a custom mapping of the attributes to JSON keys.

@example

  def self.attribute_key_name_map
    return { :@some_key => "some_key_in_json" }
  end

Returns:

  • (Hash)

    of mapping properties to keys in the JSON



116
117
118
# File 'lib/json_convertible.rb', line 116

def attribute_key_name_map
  return {}
end

#attribute_name_to_json_proc_mapHash

class method This method is intended to be overridden by any class that implements JSONConvertible and need to encode the result of the class attributes in a certain format into the JSON.

@example

  def self.attribute_name_to_json_proc_map
    timestamp_to_json_proc = proc { |timestamp|
      timestamp.strftime('%Q')
    }
    return { :@timestamp => timestamp_to_json_proc }
  end

Returns:

  • (Hash)

    of properties and procs formatting to JSON



136
137
138
# File 'lib/json_convertible.rb', line 136

def attribute_name_to_json_proc_map
  return {}
end

#attribute_to_type_mapHash

class method This method is intended to be overridden by any class that implements JSONConvertible and need to provide the encoder information about which types are each attribute of the class.

@example

  def attribute_to_type_map
    return { :@string_attribute => String, :@custom_class_attribute => CustomClass }
  end

Returns:

  • (Hash)

    of properties and their types



174
175
176
# File 'lib/json_convertible.rb', line 174

def attribute_to_type_map
  return {}
end

#from_json!(json_object) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/json_convertible.rb', line 76

def from_json!(json_object)
  instance, json_object = _initialize_using!(json_object)
  json_object.each do |var, val|
    # If we encounter with a value that is represented by an array, iterate over it.
    if val.kind_of?(Array)
      # For each of the objects in the array, we call the protected method to build the object array.
      array_property = []
      array_name = nil
      val.each do |array_val|
        array_name, this_instance = _from_json!(var, array_val, is_iterable: true)
        array_property << this_instance
      end
      if array_name.nil?
        if attribute_key_name_map.key(var)
          array_name = attribute_key_name_map.key(var).to_sym
        else
          array_name = "@#{var}".to_sym
        end
      end
      instance.instance_variable_set(array_name, array_property)
    else
      var_name, var_value = _from_json!(var, val)
      instance.instance_variable_set(var_name, var_value)
    end
  end
  return instance
end

#json_to_attribute_name_proc_mapHash

class method This method is intended to be overridden by any class that implements JSONConvertible and need to decode the JSON values back to the original types of the class attributes.

@example

  def self.json_to_attribute_name_proc_map
    json_to_timestamp_proc = proc { |json|
      Time.at(json.to_i)
    }
    return { :@timestamp => json_to_timestamp_proc }
  end

Returns:

  • (Hash)

    of properties and procs formatting from JSON



156
157
158
# File 'lib/json_convertible.rb', line 156

def json_to_attribute_name_proc_map
  return {}
end

#map_enumerable_type(enumerable_property_name: nil, current_json_object: nil) ⇒ Any

class method This method is intended to be overridden by any class that implements JSONConvertible and need to provide a custom mapping for a enumerable property in the JSON.

Parameters:

  • enumerable_property_name (Any) (defaults to: nil)

    the property name of the object.

  • current_json_object (Hash) (defaults to: nil)

    the hash object of enumerable_property_name for a given iteration step.

    @example

    def map_enumerable_type(enumerable_property_name: nil, current_json_object: nil)
      if enumerable_property_name == :@job_triggers
        JobTrigger needs a factory method that reads `json[:type]` and instantiates the proper type
        return  FastlaneCI::JobTrigger.create(json: current_json_object)
      end
    end
    

Returns:

  • (Any)

    object in the array by the given property_name and json_object.



196
197
198
# File 'lib/json_convertible.rb', line 196

def map_enumerable_type(enumerable_property_name: nil, current_json_object: nil)
  return nil
end