module Superb::AttributeMagic

Public Class Methods

field_name_for(k) click to toggle source
# File lib/superb.rb, line 13
def field_name_for(k)
  ("@" + k.to_s.gsub(/^@*/, "")).to_sym
end
key_name_for(k) click to toggle source
# File lib/superb.rb, line 8
def key_name_for(k)
  k.to_s.gsub(/^@*/, "").to_sym
end

Public Instance Methods

[](key) click to toggle source
# File lib/superb.rb, line 18
def [](key)
  self.instance_variable_get(field_name_for(key))
end
[]=(key, value) click to toggle source
# File lib/superb.rb, line 22
def []=(key, value)
  self.instance_variable_set(field_name_for(key), value)
end
merge(hashable) click to toggle source
# File lib/superb.rb, line 51
def merge(hashable)
  self.dup.merge!(hashable)
end
merge!(hashable) click to toggle source
# File lib/superb.rb, line 43
def merge!(hashable)
  hashable.to_hash.each do |k, v|
    self[k] = v
  end

  return self
end
method_missing(name, *args, &block) click to toggle source
# File lib/superb.rb, line 26
def method_missing(name, *args, &block)
  name_str = name.to_s

  if name_str.end_with? "="

    self.[]=(name[0...-1].to_sym, *args, &block)
  else
    self.[](name, *args, &block)
  end
end
respond_to?(name) click to toggle source
# File lib/superb.rb, line 37
def respond_to?(name)
  # TODO: Should only return true when string can be
  #       used as a method name.
  true
end
to_hash() click to toggle source
# File lib/superb.rb, line 55
def to_hash
  retval = {}
  instance_variables.each do |k|
    retval[key_name_for(k)] = self.instance_variable_get(k)
  end

  return retval
end

Private Instance Methods

field_name_for(k) click to toggle source
# File lib/superb.rb, line 13
def field_name_for(k)
  ("@" + k.to_s.gsub(/^@*/, "")).to_sym
end
key_name_for(k) click to toggle source
# File lib/superb.rb, line 8
def key_name_for(k)
  k.to_s.gsub(/^@*/, "").to_sym
end