Sha256: 1f62747e46889c6768a4d62f63ed17081b411aae124facbf7bef49bcc2bfcf25

Contents?: true

Size: 765 Bytes

Versions: 5

Compression:

Stored size: 765 Bytes

Contents

module JsonbAccessor
  class NestedBase
    include Helpers

    attr_accessor :attributes, :parent
    alias_method :to_h, :attributes

    delegate :[], to: :attributes
    delegate :nested_classes, :attribute_on_parent_name, to: :class

    def initialize(attributes = {})
      self.attributes = {}.with_indifferent_access

      nested_classes.keys.each do |key|
        send("#{key}=", nil)
      end

      attributes.each do |name, value|
        send("#{name}=", value)
      end
    end

    def update_parent
      parent.send("#{attribute_on_parent_name}=", self) if parent
    end

    def []=(key, value)
      send("#{key}=", value)
    end

    def ==(suspect)
      self.class == suspect.class && attributes == suspect.attributes
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jsonb_accessor-0.4.0.beta lib/jsonb_accessor/nested_base.rb
jsonb_accessor-0.3.3 lib/jsonb_accessor/nested_base.rb
jsonb_accessor-0.3.2 lib/jsonb_accessor/nested_base.rb
jsonb_accessor-0.3.1 lib/jsonb_accessor/nested_base.rb
jsonb_accessor-0.3.0 lib/jsonb_accessor/nested_base.rb