Module: Magnetic::Field::DSL

Included in:
ActiveRecord::Base, Interface
Defined in:
lib/magnetic/field.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



205
206
207
208
209
# File 'lib/magnetic/field.rb', line 205

def self.included other
  super
ensure
  other.extend self
end

Instance Method Details

#field(*argv, &block) ⇒ Object

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/magnetic/field.rb', line 163

def field *argv, &block
  name = argv.delete_first{|arg| String === arg or Symbol === arg }
  base = argv.delete_first{|arg| Field::Base.class === arg}
  configuring = argv.delete_first{|arg| ['configure', :configure, 'configuring', :configuring].include? arg}
  options = argv.delete_first{|arg| Hash === arg}

  name ||= base.name
  raise ArgumentError, 'nameless field!' unless name
  name = name.to_s

  if options.nil? and base.nil? and block.nil?
    field = field_for name
    field.clone unless field.nil? 
  else
    options ||= {}
    options.to_options!
    field =
      if configuring
        field_for name
      else
        base ||= Field::Base
        field = base.clone
        field.name = name
        field.owner = self 
        field
      end

    field.configure options, &block

#p [ (field.owner.name rescue field.owner.class.name), field.name, (configuring ? :configuring : false), caller.first ]

    unless configuring
      raise ArgumentError, "duplicate field '#{ name }' in #{ fieldset[name].inspect }" if fieldset.has_key?(field.name)
      fieldset[field.name] = field
    end

    field
  end
end

#field_for(name) ⇒ Object



202
203
204
# File 'lib/magnetic/field.rb', line 202

def field_for name
  fieldset[name.to_s]
end

#fieldsetObject



160
161
162
# File 'lib/magnetic/field.rb', line 160

def fieldset
  @fieldset ||= Field::Set[]
end