class Cda::Base

Public Class Methods

new(attrs = {}) { |self| ... } click to toggle source
Calls superclass method Cda::StrictAttributes::new
# File lib/cda/base.rb, line 7
def initialize(attrs = {}, &_)
  attrs = {_text: attrs} if attrs.is_a?(String) && respond_to?(:_text)
  raise "Can't initialize #{self.class.name} with #{attrs.class.name} (#{attrs.inspect})" unless attrs.is_a?(Hash)
  attrs_with_defaults = Utility.merge_json(attrs.with_indifferent_access, defaults)
  super(Utility.mk_class(attrs_with_defaults))
  yield self if block_given?
end

Public Instance Methods

==(other) click to toggle source
# File lib/cda/base.rb, line 15
def ==(other)
  self.class == other.class && self.serialize == other.serialize
end
mixed?() click to toggle source
# File lib/cda/base.rb, line 25
def mixed?
  respond_to?(:_text)
end
serialize() click to toggle source
# File lib/cda/base.rb, line 19
def serialize
  self.class.attribute_set.each_with_object({}) do |attr, hash|
    hash[attr.name] = serialize_value(self.send(attr.name))
  end.merge(_type: self.class.name)
end
to_s() click to toggle source
Calls superclass method
# File lib/cda/base.rb, line 29
def to_s
  if !mixed?
    super
  elsif _text.present?
    _text
  else
    super
  end
end

Private Instance Methods

serialize_value(value) click to toggle source
# File lib/cda/base.rb, line 41
def serialize_value(value)
  if value.is_a?(Array)
    value.map { |v| serialize_value(v) }
  elsif value.respond_to?(:independent?) && value.independent?
    nil
  else
    value.respond_to?(:serialize) ? value.serialize : value
  end
end