Method: Genesis.new_atome

Defined in:
lib/atome/genesis/genesis.rb

.new_atome(element, &method_proc) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/atome/genesis/genesis.rb', line 118

def new_atome(element, &method_proc)

  # the method define below is the slowest but params are analysed and sanitized
  Atome.define_method element do |params = nil, &user_proc|
    instance_exec(params, user_proc, &method_proc) if method_proc.is_a?(Proc)
    if params
      params = atome_sanitizer(element, params, &user_proc)
      atome_processor(element, params, &user_proc)
    else
      # when no params passed whe assume teh user want a getter,
      # as getter should give us all atome of a given within the atome
      # ex : puts a.shape => return all atome with the type 'shape' in this atome
      collected_atomes = []
      if Universe.applicable_atomes.include?(element)
        # we do the same for apply to be able to retrieve 'color' and other atome that apply instead of being fasten
        @apply.each do |fasten_atome|
          collected_atomes << fasten_atome if grab(fasten_atome).type.to_sym == element.to_sym
        end
      elsif fasten
        fasten.each do |fasten_atome|
          child_found=grab(fasten_atome)
            collected_atomes << fasten_atome if child_found.type.to_sym == element.to_sym
        end
      end
      # TODO/ FIXME : potential problem with group  here"
      collected_atomes
    end
  end

  # the method define below is the fastest params are passed directly
  Atome.define_method "set_#{element}" do |params, &user_proc|
    # we generate the corresponding module here:
    # Object.const_set(element, Module.new)
    # we add the newly created atome to the list of "child in it's category, eg if it's a shape we add the new atome
    # to the shape particles list : @!atome[:shape] << params[:id]

    if Universe.atomes[params[:id]]
      # if atome id already exist we grab the previous one
      # this prevent the creation of new atome if the atome already exist
      previous_atome = grab(params[:id])
      # now we must re-affect affected atomes
      previous_atome.affect(params[:affect])
      previous_atome
    else
      Atome.new(params, &user_proc)
    end
    # Now we return the newly created atome instead of the current atome that is the parent cf: b=box; c=b.circle
  end

end