Sha256: d3f2d718d7c4846f69e9a330e124bfb9669fae7bbc13c39e1902ababbdc7cc23

Contents?: true

Size: 793 Bytes

Versions: 6

Compression:

Stored size: 793 Bytes

Contents

module Usable
  def self.Struct(attributes = {})
    Class.new do
      extend Usable
      self.usables = Usable::Config.new(attributes)
      define_usable_accessors
      attributes.keys.map(&:to_sym).each do |key|
        define_method(key) { @attrs[key] }
        define_method("#{key}=") { |new_val| @attrs[key] = new_val }
      end

      attr_accessor :attrs

      def initialize(attrs = {})
        @attrs = usables.merge(attrs)
      end

      def [](key)
        @attrs[key]
      end

      def []=(key, val)
        @attrs[key] = val
      end

      def each(&block)
        @attrs.each(&block)
      end

      def to_h
        @attrs.dup
      end

      alias to_hash to_h

      def merge(other)
        to_h.merge!(other)
      end

      alias + merge
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
usable-3.10.0 lib/usable/struct.rb
usable-3.9.4 lib/usable/struct.rb
usable-3.9.3 lib/usable/struct.rb
usable-3.9.2 lib/usable/struct.rb
usable-3.9.1 lib/usable/struct.rb
usable-3.9.0 lib/usable/struct.rb