Sha256: da730c9e59f2be32ead9efe11d42b36b12264401889af6d0fd8b3fe051e9e78d

Contents?: true

Size: 893 Bytes

Versions: 22

Compression:

Stored size: 893 Bytes

Contents

require 'wukong/extensions/class'
module Wukong

  module HashlikeClass
    module ClassMethods
      def has_members *members
        self.members ||= []
        self.members = members.map(&:to_s) + self.members
        self.members.each do |member|
          attr_accessor member.to_sym
        end
      end
      alias_method :has_member, :has_members
      def keys
        members
      end
    end

    def [](key)
      self.send(key)
    end

    def []=(key, val)
      self.send("#{key}=", val)
    end

    def to_a
      values_of(*members)
    end

    def to_flat
      to_a.map(&:to_flat).flatten
    end

    def self.included base
      base.class_eval do
        extend ClassMethods
        include HashLike
        class_inheritable_accessor :members

        def to_hash *args
          super(*args).merge 'type' => self.class.to_s
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
wukong-0.1.4 lib/wukong/extensions/hashlike_class.rb
wukong-0.1.1 lib/wukong/extensions/hashlike_class.rb