Class: CDB::Struct

Inherits:
Struct
  • Object
show all
Defined in:
lib/cdb/struct.rb

Overview

Modifications to Ruby’s Struct class for use within the CDB module. Must be called ‘Struct’ to play nice with YARD’s @attr documentation.

Direct Known Subclasses

Issue, Series

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ Struct

Override Struct’s initialize method to accept a hash of members instead.



7
8
9
# File 'lib/cdb/struct.rb', line 7

def initialize(h={})
  h.each{|k,v| send("#{k}=", v)}
end

Instance Method Details

#as_jsonObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cdb/struct.rb', line 11

def as_json(*)
  members.each_with_object({}){|m, map|
    next map unless self[m]
    case self[m]
    when Array
      map[m] = self[m].collect(&:as_json)
    else
      map[m] = self[m]
    end
  }
end

#to_json(opts = {}, depth = 0) ⇒ Object



23
24
25
26
27
# File 'lib/cdb/struct.rb', line 23

def to_json(opts={}, depth=0)
  opts = opts.to_h rescue opts
  opts = {space:' ', object_nl:' '}.merge(opts)
  self.as_json.to_json(opts)
end