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.



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

def initialize(klass)
  @klass = klass

  # mappers, only storing symbol of mapped values
  @mappers_order = []

  @mappers = {}
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

#mappersObject (readonly)

Returns the value of attribute mappers.



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

def mappers
  @mappers
end

#mappers_orderObject (readonly)

Returns the value of attribute mappers_order.



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

def mappers_order
  @mappers_order
end

Instance Method Details

#add_mapper(mapper) ⇒ Object

Raises:



16
17
18
19
20
# File 'lib/jsoning/foundations/protocol.rb', line 16

def add_mapper(mapper)
  raise Jsoning::Error, "Mapper must be of class Jsoning::Mapper" unless mapper.is_a?(Jsoning::Mapper)
  @mappers_order << canonical_name(mapper.name)
  @mappers[canonical_name(mapper.name)] = mapper
end

#construct_hash_from(json_string) ⇒ Object

construct hash from given JSON



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jsoning/foundations/protocol.rb', line 57

def construct_hash_from(json_string)
  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]}]

  mappers_order.each do |mapper_sym|
    mapper = 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

#generate(object, options = {}) ⇒ Object

generate a JSON object options:

  • pretty: pretty print json data



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jsoning/foundations/protocol.rb', line 29

def generate(object, options = {})
  pretty = options[:pretty]
  pretty = options["pretty"] if pretty.nil?
  pretty = false if pretty.nil?

  data = retrieve_values_from(object)

  if pretty
    JSON.pretty_generate(data)
  else
    JSON.generate(data)
  end
end

#mapper_for(name) ⇒ Object



22
23
24
# File 'lib/jsoning/foundations/protocol.rb', line 22

def mapper_for(name)
  @mappers[canonical_name(name)]
end

#retrieve_values_from(object) ⇒ Object

construct the JSON from given object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jsoning/foundations/protocol.rb', line 44

def retrieve_values_from(object)
  # hold data here
  data = {}

  mappers_order.each do |mapper_sym|
    mapper = mapper_for(mapper_sym)
    mapper.extract(object, data)
  end

  data
end