Module: Conglomerate::Particle

Included in:
Collection, Command, Datum, Error, Item, Link, Query, Root, Template
Defined in:
lib/conglomerate/particle.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(object) ⇒ Object



3
4
5
# File 'lib/conglomerate/particle.rb', line 3

def self.included(object)
  object.send(:extend, ClassMethods)
end

Instance Method Details

#initialize(attributes = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/conglomerate/particle.rb', line 7

def initialize(attributes = {})
  self.class.attributes.each do |attr, |
    if [:required]
      raise "MissingAttribute" unless attributes[attr] || attributes[attr.to_s]
    end

    if [:type] == :array
      self.instance_variable_set("@#{attr}", Conglomerate::Array.new([:contains]))
    end
  end

  attributes.each do |key, value|
    attrs = self.class.attributes
    if attrs[key.to_sym] && attrs[key.to_sym][:type] == :array
      array = value
      value = Conglomerate::Array.new(attrs[key.to_sym][:contains])
      array.each { |item| value << item }
      self.instance_variable_set("@#{key}", value)
    end

    self.send("#{key}=", value) if self.respond_to?("#{key}=")
  end
end