Sha256: d6e42fe07229429c02d26f9f1d07048d6b8826334fa9851fa116c0ed24e79c36

Contents?: true

Size: 884 Bytes

Versions: 2

Compression:

Stored size: 884 Bytes

Contents

module ClassMattr
  class Proxy
    MATTRS   ||= {}
    @@mattrs ||= []

    def initialize host
      @host = host
    end

    def _set name
      while el = @@mattrs.shift
        klass, trait, opts = el[0], el[1].to_sym, el[2]

        MATTRS[klass] ||= {}
        el = MATTRS[klass][name.to_sym] ||= {}

        # default value if true if no argument provided
        el[trait] = opts.nil? ? true : opts
      end
    end

    def _get name
      for klass in @host.ancestors.map(&:to_s)
        return {} if klass == 'ClassMattr'

        hash = MATTRS.dig(klass, name.to_sym)
        return hash if hash
      end

      raise 'ClassMattr not included?'
    end

    def get_hash
      name = :__mattr_tmp
      _set name
      MATTRS[@host.to_s].delete(name)
    end

    def method_missing name, value=nil
      @@mattrs.push [@host.to_s, name.to_s, value]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
class-mattr-0.3.1 ./lib/lib/proxy.rb
class-mattr-0.3.0 ./lib/lib/proxy.rb