module Conglomerate::Particle::ClassMethods

Public Class Methods

extended(klass) click to toggle source
# File lib/conglomerate/particle.rb, line 34
def self.extended(klass)
  klass.instance_variable_set("@attributes", {})
end

Public Instance Methods

attributes() click to toggle source
# File lib/conglomerate/particle.rb, line 38
def attributes
  instance_variable_get("@attributes").dup
end

Private Instance Methods

array(attr, options = {}) click to toggle source
# File lib/conglomerate/particle.rb, line 44
def array(attr, options = {})
  contains = options.fetch(:contains, nil)
  cull = options.fetch(:cull, true)

  attribute(attr, :type => :array, :contains => contains, :cull => cull)
end
attribute(attr, options = {}) click to toggle source
# File lib/conglomerate/particle.rb, line 51
def attribute(attr, options = {})
  default = options.fetch(:default, nil)
  type = options.fetch(:type, nil)
  contains = options.fetch(:contains, nil)
  cull = options.fetch(:cull, true)
  required = options.fetch(:required, nil)

  instance_variable_get("@attributes")[attr] = {
    :default => default,
    :type => type,
    :contains => contains,
    :cull => cull,
    :required => required
  }

  self.send(:attr_reader, attr)

  if type != :array
    self.send(:define_method, :"#{attr}=") do |val|
      attr_metadata = self.class.attributes[attr]

      if type = attr_metadata[:type]
        raise "TypeMismatch" if !val.is_a?(type) && val != nil
      end

      self.instance_variable_set("@#{attr}", val)
    end
  end
end