class CRStruct::Open

Public Class Methods

new(h={}) click to toggle source
h should support interface ###

raise “need Hash(Symbol,Value)” unless

[:to_h, :keys, :has_key?, :[], :[]=].all?{|_|h.respond_to?_} and
h.keys.all?{|_|_.is_a? Symbol}
# File lib/crstruct/open.rb, line 7
def initialize(h={})
  @h = h
end

Public Instance Methods

free?(k) click to toggle source
free?, set!, and get? ###

make purposefull access to @h possible, and easier to subclass CRStruct.

# File lib/crstruct/open.rb, line 23
def free?(k)
  !(@h.has_key?(k) or respond_to?(k))
end
get?(k) click to toggle source
# File lib/crstruct/open.rb, line 29
def get?(k)
  @h[k]
end
method_missing(key, *args, &proc) click to toggle source
Calls superclass method
# File lib/crstruct/open.rb, line 33
def method_missing(key, *args, &proc)
  if proc.nil?
    case args.length
    when 0
      return get?(key) if @h.has_key? key
    when 1
      if key=~/^\w+=$/
        k = key[0..-2].to_sym
        return set!(k, args[0]) if free?(k)
      end
    end
  end
  super
end
set!(k, v) click to toggle source
# File lib/crstruct/open.rb, line 26
def set!(k, v)
  @h[k]=v
end
to_h() click to toggle source

Can it somehow be converted into a Hash? Most likely.

# File lib/crstruct/open.rb, line 13
def to_h
  @h.to_h
end