class PropSet

Properties of properties (such as required, default, type, etc.)

TODO: change this into a mixin module? Make it more like a simple add-on to openstruct? (and put in its own file)

Public Class Methods

new(*opts) click to toggle source
# File lib/proplist.rb, line 17
def initialize(*opts)
  @data = {}
  merge_opts(*opts)
end

Protected Class Methods

default(kvpairs) click to toggle source
# File lib/proplist.rb, line 14
def default(kvpairs) defaults.merge!(kvpairs) end
inverse(kvpairs) click to toggle source
# File lib/proplist.rb, line 13
def inverse(kvpairs) inverses.merge!(kvpairs) end

Public Instance Methods

dup() click to toggle source
# File lib/proplist.rb, line 82
def dup() Marshal.load(Marshal.dump(self)) end
inspect() click to toggle source
# File lib/proplist.rb, line 22
def inspect() "#<#{self.class.name} #{@data.inspect}>" end
merge_opts(*opts) click to toggle source
# File lib/proplist.rb, line 25
def merge_opts(*opts)
  opts = [opts].flatten.reduce({}){|h,o| Hash === o ? h.merge(o) : h.merge({o.to_sym => true})}
  opts.each do |k, v|
    k_str = k.to_s
    k_sym = k.to_sym
    if k_str.starts_with?('no_')
      if v = true
        k_to_remove = k_str[3..-1].to_sym
        @data.delete(k_to_remove)
        @data.delete(inverses[k_to_remove])
      else
        error ArgumentError, "Parameter aspects that start with 'no_' are for unsetting that aspect of the property. For example, `no_default: true`- Always expecting 'true' as the value."
      end
    else
      if inverses[k_sym]
        error ArgumentError, "Expected true or false value for #{k_sym} aspect of property #{@name}" unless v == !!v
        v = !v
        k_sym = inverses[k_sym]
      end
      @data[k_sym] = v
    end
  end
end
method_missing(m, *a, &b) click to toggle source
# File lib/proplist.rb, line 49
def method_missing(m, *a, &b)
  # TODO: check @data for key before checking it for respond_to to solve future problems like this :default hack
  return @data.send(m, *a, &b) if @data.respond_to?(m) && ![:default,:default=].include?(m.to_sym)
  case m.to_s
  when /^(.+)=$/      then merge_opts($1 => (a.size == 1 ? a[0] : a))
  when /^has_(.+)\?$/ then @data.has_key?($1.to_sym) || @data.has_key?(inverses[$1.to_sym])
  when /^(.+)\?$/     then !!val_for($1)
  else                     val_for(m)
  end
end
realize(val) click to toggle source

When the property is given a value specify (via overriding this method) anything special that needs to happen to the PropSet instance.

# File lib/proplist.rb, line 74
def realize(val) self.value = val end
realized() click to toggle source
# File lib/proplist.rb, line 76
def realized
  return self.value   if self.has_value?
  return self.default if self.has_default?
  nil
end
realized?() click to toggle source
# File lib/proplist.rb, line 75
def realized?()  self.has_value?  end
to_hash() click to toggle source
# File lib/proplist.rb, line 23
def to_hash() @data end
val_for(key) click to toggle source
# File lib/proplist.rb, line 60
def val_for(key)
  key = key.to_sym
  rev = inverses.has_key?(key)
  key = inverses[key] if rev
  val = nil
  if @data.has_key?(key) || defaults.has_key?(key)
    val = @data.has_key?(key) ? @data[key] : defaults[key]
    val = !val if rev
  end
  val
end