Class: JSONArraySerializer

Inherits:
Array
  • Object
show all
Defined in:
lib/json_array_serializer.rb,
lib/json_array_serializer/version.rb

Constant Summary collapse

VERSION =
'0.0.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ JSONArraySerializer

Class -> void (hook) Sets up the new JSONArraySerializer with it’s elements class. The element_class is what will be used to represent each element of the stored JSON.

The option :array_class MUST implement two methods:

A.new : Array -> A a.to_a : -> Array (where a is an instance of A)

The option :element_class MUST implement two methods:

A.new : Hash -> A a.to_h : -> Hash (where a is an instance of A)

The option :column_type MUST be one of :string | :text | :array.

The option :allow_nil is a boolean which determines whether or not to load/dump nil values. If set to true (the default) it will load/dump nil when given nil. If set to false it will treat nil as an empty array.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/json_array_serializer.rb', line 28

def initialize(options = {})
  options = {
    array_class: Array,
    element_class: Hash,
    column_type: :text,
    allow_nil: true
  }.merge(options)

  @array_class   = options[:array_class]
  @element_class = options[:element_class]
  @column_type   = options[:column_type]
  @allow_nil     = options[:allow_nil]
end

Instance Attribute Details

#array_classObject

Returns the value of attribute array_class.



5
6
7
# File 'lib/json_array_serializer.rb', line 5

def array_class
  @array_class
end

#element_classObject

Returns the value of attribute element_class.



5
6
7
# File 'lib/json_array_serializer.rb', line 5

def element_class
  @element_class
end

Instance Method Details

#dump(data) ⇒ Object

array_class<element_class> -> [JSON String] || JSON String Takes an instance of ‘array_class` with `element_class` elements and dumps them either a array of JSON or JSON itself for the database.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/json_array_serializer.rb', line 71

def dump(data)
  case @column_type
  when :array
    if data.nil?
      @allow_nil ? nil : []
    else
      data.to_a.map { |e| JSON.dump(e.to_h) }
    end
  when :string, :text
    if data.nil?
      @allow_nil ? nil : '[]'
    else
      JSON.dump(data.to_a.map { |e| e.to_h })
    end
  end
end

#load(data) ⇒ Object

JSON String

|| JSON String -> array_class<element_class>

Takes the data from the database, and loads them into an instance of ‘array_class` with elements of `element_class`.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/json_array_serializer.rb', line 46

def load(data)
  return (@allow_nil ? nil : []) if data.nil?

  array =
    case @column_type
    when :array
      data.map do |json|
        hash = JSON.load(json)
        (element_class == Hash) ? hash : element_class.new(hash)
      end
    when :string, :text
      if element_class == Hash
        JSON.load(data)
      else
        JSON.load(data).map { |hash| element_class.new(hash) }
      end
    end

  (array_class == Array) ? array : array_class.new(array)
end