Class: Jsoning::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/jsoning/foundations/protocol.rb

Overview

takes care of the class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Protocol

Returns a new instance of Protocol.



6
7
8
9
10
11
12
13
14
# File 'lib/jsoning/foundations/protocol.rb', line 6

def initialize(klass)
  @klass = klass
  @version_instances = {}

  # each protocol has a default version named :default
  add_version(:default)

  self
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/jsoning/foundations/protocol.rb', line 3

def klass
  @klass
end

#version_instancesObject (readonly)

Returns the value of attribute version_instances.



4
5
6
# File 'lib/jsoning/foundations/protocol.rb', line 4

def version_instances
  @version_instances
end

Instance Method Details

#add_version(version_name) ⇒ Object

add a new version, a protocol can handle many version to export the JSON



18
19
20
21
22
23
24
25
26
# File 'lib/jsoning/foundations/protocol.rb', line 18

def add_version(version_name)
  unless version_name.is_a?(String) || version_name.is_a?(Symbol)
    fail "Version name must be either a String or a Symbol"
  end
  version = Jsoning::Version.new(self)
  version.version_name = version_name
  @version_instances[version.version_name] = version
  version
end

#construct_hash_from(json_string, version_name) ⇒ Object

construct hash from given JSON



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jsoning/foundations/protocol.rb', line 65

def construct_hash_from(json_string, version_name)
  data = {}

  # make all json obj keys to downcase, symbol
  json_obj = JSON.parse(json_string)
  json_obj = Hash[json_obj.map { |k, v| [k.to_s.downcase, v]}]

  version = get_version!(version_name)
  version.mappers_order.each do |mapper_sym|
    mapper = version.mapper_for(mapper_sym)
    mapper_key_name = mapper.name.to_s.downcase
    
    mapper_default_value = mapper.default_value
    mapper_key_value = json_obj[mapper_key_name]
    # retrieve value from key, if not available, from default
    value = mapper_key_value || mapper_default_value

    if value.nil? && !mapper.nullable?
      raise Jsoning::Error, "Constructing hash failed due to #{mapper_key_name} being nil when it is not allowed to" 
    end

    data[mapper_key_name] = value
  end

  data
end

#get_version(version_name) ⇒ Object

retrieve a defined version, or return nil if undefined



29
30
31
# File 'lib/jsoning/foundations/protocol.rb', line 29

def get_version(version_name)
  @version_instances[version_name.to_s]
end

#get_version!(version_name) ⇒ Object

retrieve a defined version, or fail if undefined



34
35
36
37
38
# File 'lib/jsoning/foundations/protocol.rb', line 34

def get_version!(version_name)
  version = get_version(version_name)
  fail Jsoning::Error, "Unknown version: #{version_name}" if version.nil?
  version
end

#retrieve_values_from(object, options) ⇒ Object

construct the JSON from given object options:

- version: the version to be used for processing


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jsoning/foundations/protocol.rb', line 43

def retrieve_values_from(object, options)
  # user will pass in version, rather than version_name, although actually
  # it is a version_name instead of version instance
  version_name = options[:version]
  # use default if version is undefined
  version = get_version(version_name) || get_version(:default)

  # hold data here
  data = {}

  version.mappers_order.each do |mapper_sym|
    mapper = version.mapper_for(mapper_sym)
    # version_name and version may be different, that is when the user uses version
    # that is not yet defined, therefore, will fall to default. therefore, user
    # version_name as this is what is requested by the caller
    mapper.extract(object, version_name, data)
  end

  data
end