Parent

Files

BasicStruct

BasicStruct

BasicObject is very similar to Ruby's own OpenStruct, but it offers some advantages. With OpenStruct, slots with the same name as predefined Object methods cannot be used. With BasicObject, almost any slot can be defined. BasicObject is a subclass of BasicObject to ensure all method slots, except those that are absolutely essential, are open for use.

Unlike a Hash, all BasicObject's keys are symbols and all keys are converted to such using to_sym on the fly.

Public Class Methods

[](hash=nil) click to toggle source

PUBLIC_METHODS = /(^__|^instance_|^object_|^W|^as$|^send$|^class$|?$)/ protected(*public_instance_methods.select{ |m| m !~ PUBLIC_METHODS })

# File lib/hashery/basicstruct.rb, line 34
def self.[](hash=nil)
  new(hash)
end
new(hash=nil, &yld) click to toggle source

Inititalizer for BasicObject is slightly different than that of Hash. It does not take a default parameter, but an initial priming Hash, like OpenStruct. The initializer can still take a default block however. To set the default value use #default!(value).

BasicObject.new(:a=>1).default!(0)
# File lib/hashery/basicstruct.rb, line 45
def initialize(hash=nil, &yld)
  @table = Hash.new(&yld)
  if hash
    hash.each{ |k,v| store(k,v) }
  end
end

Public Instance Methods

<<(x) click to toggle source
# File lib/hashery/basicstruct.rb, line 156
def <<(x)
  case x
  when Hash
    @table.update(x)
  when Array
    x.each_slice(2) do |(k,v)|
      @table[k] = v
    end
  end
end
==( other ) click to toggle source

Check equality.

# File lib/hashery/basicstruct.rb, line 130
def ==( other )
  case other
  when BasicObject
    @table == other.as_hash
  when Hash
    @table == other
  else
    if other.respond_to?(:to_hash)
      @table == other.to_hash
    else
      false
    end
  end
end
[](key) click to toggle source
# File lib/hashery/basicstruct.rb, line 173
def [](key)
  @table[key.to_sym]
end
[]=(key, value) click to toggle source
# File lib/hashery/basicstruct.rb, line 168
def []=(key, value)
  @table[key.to_sym] = value
end
as_hash() click to toggle source

NOT SURE ABOUT THIS

# File lib/hashery/basicstruct.rb, line 103
def as_hash
  @table
end
default!(default) click to toggle source

Set the default value.

# File lib/hashery/basicstruct.rb, line 125
def default!(default)
  @table.default = default
end
each(&yld) click to toggle source

Iterate over each key-value pair.

# File lib/hashery/basicstruct.rb, line 120
def each(&yld)
  @table.each(&yld)
end
eql?( other ) click to toggle source
# File lib/hashery/basicstruct.rb, line 146
def eql?( other )
  case other
  when BasicObject
    @table.eql?(other.as_hash)
  else
    false
  end
end
initialize_copy(orig) click to toggle source
# File lib/hashery/basicstruct.rb, line 53
def initialize_copy(orig)
  orig.each{ |k,v| store(k,v) }
end
inspect() click to toggle source

Object inspection. TODO: Need to get __class__ and __id__ in hex form.

# File lib/hashery/basicstruct.rb, line 59
def inspect
  #@table.inspect
  hexid = __id__
  klass = "BasicObject" # __class__
  "#<#{klass}:#{hexid} #{@table.inspect}>"
end
is_a?(klass) click to toggle source
# File lib/hashery/basicstruct.rb, line 113
def is_a?(klass)
  return true if klass == Hash  # TODO: Is this wise? How to fake a subclass?
  return true if klass == BasicObject
  false
end
key?(key) click to toggle source

Is a given key defined?

# File lib/hashery/basicstruct.rb, line 108
def key?(key)
  @table.key?(key.to_sym)
end
merge!(other) click to toggle source
# File lib/hashery/basicstruct.rb, line 178
def merge!(other)
  BasicObject.new(@table.merge!(other))
end
to_a() click to toggle source

Convert to an associative array.

# File lib/hashery/basicstruct.rb, line 67
def to_a
  @table.to_a
end
to_basicstruct() click to toggle source
# File lib/hashery/basicstruct.rb, line 82
def to_basicstruct
  self
end
to_h() click to toggle source
# File lib/hashery/basicstruct.rb, line 72
def to_h
  @table.dup
end
to_hash() click to toggle source
# File lib/hashery/basicstruct.rb, line 77
def to_hash
  @table.dup
end
to_proc(response=false) click to toggle source

Convert to an assignment procedure.

# File lib/hashery/basicstruct.rb, line 87
def to_proc(response=false)
  hash = @table
  if response
    lambda do |o|
      hash.each do |k,v|
        o.__send__("#{k}=", v) rescue nil
      end
    end
  else
    lambda do |o|
      hash.each{ |k,v| o.__send__("#{k}=", v) }
    end
  end
end
update!(other) click to toggle source
# File lib/hashery/basicstruct.rb, line 183
def update!(other)
  @table.update(other)
  self
end

Protected Instance Methods

fetch(k, *d, &b) click to toggle source
# File lib/hashery/basicstruct.rb, line 194
def fetch(k, *d, &b)
  @table.fetch(k.to_sym, *d, &b)
end
method_missing(sym, *args, &blk) click to toggle source

def protect_slot( key )

 (class << self; self; end).class_eval {
   protected key rescue nil
}

end

# File lib/hashery/basicstruct.rb, line 220
def method_missing(sym, *args, &blk)
  type = sym.to_s[-1,1]
  key  = sym.to_s.sub(/[=?!]$/,'').to_sym
  case type
  when '='
    store(key, args[0])
  when '!'
    @table.__send__(key, *args, &blk)
  #  if key?(key)
  #    fetch(key)
  #  else
  #    store(key, BasicObject.new)
  #  end
  when '?'
    fetch(key)
  else
    fetch(key)
  end
end
store(k, v) click to toggle source
# File lib/hashery/basicstruct.rb, line 190
def store(k, v)
  @table.store(k.to_sym, v)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.