Sha256: 2ff9c00b5c9a084124b4c7b345e4f698b9e3a941ee152a924911740b887b580b

Contents?: true

Size: 1.53 KB

Versions: 40

Compression:

Stored size: 1.53 KB

Contents

module Eco::Data::Locations
  module NodeBase
    require_relative 'node_base/tag_validations'
    include Eco::Data::Locations::NodeBase::TagValidations

    require_relative 'node_base/treeify'
    require_relative 'node_base/parsing'
    require_relative 'node_base/serial'
    require_relative 'node_base/csv_convert'
    require_relative 'node_base/builder'
    extend Eco::Data::Locations::NodeBase::Builder

    ALL_ATTRS = [].freeze

    attr_accessor :tracked_level, :parent

    def copy
      self.class.new.set_attrs(**to_h)
    end

    def attr(sym)
      send(sym.to_sym)
    end

    def attr?(sym)
      !attr(sym).to_s.strip.empty?
    end

    def set_attrs(**kargs)
      kargs.each {|attr, value| set_attr(attr, value)}
      self
    end

    def set_attr(attr, value)
      send("#{attr}=", value)
    end

    def values_at(*attrs)
      attrs.map {|a| attr(a)}
    end

    # @yield [node, json] optional custom serializer
    # @yieldparam node [Node] self
    # @yieldparam json [Hash] the default serialization
    # @yieldreturn [Hash] the serialized Node
    def node_hash(stringify_keys: true)
      json = to_h(:id, :name, :parent_id)
      json.transform_keys!(&:to_s)   if stringify_keys
      json.merge!(yield(self, json)) if block_given?
      json
    end

    def to_h(*attrs)
      attrs = self.class::ALL_ATTRS if attrs.empty?
      attrs.zip(values_at(*attrs)).to_h
    end

    def slice(*attrs)
      return {} if attrs.empty?
      to_h(*attrs)
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.20 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.19 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.18 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.17 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.16 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.15 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.14 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.13 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.12 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.11 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.10 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.9 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.8 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.7 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.6 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.5 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.4 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.3 lib/eco/data/locations/node_base.rb
eco-helpers-3.0.2 lib/eco/data/locations/node_base.rb