Module: Locomotive::Mounter::Fields::ClassMethods

Defined in:
lib/locomotive/mounter/fields.rb

Instance Method Summary collapse

Instance Method Details

#field(name, options = {}) ⇒ Object

Add a field to the current instance. It creates getter/setter methods related to that field. A field can have translations if the option named localized is set to true.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/locomotive/mounter/fields.rb', line 211

def field(name, options = {})
  options = { localized: false }.merge(options)

  @_fields ||= {} # initialize the list of fields if nil

  self._fields[name.to_sym] = options

  class_eval "    def \#{name}\n      self.getter '\#{name}', self.class._fields[:\#{name}]\n    end\n\n    def \#{name}=(value)\n      self.setter '\#{name}', value, self.class._fields[:\#{name}] #, \#{options[:localized]}\n    end\n\n    def \#{name}_localized?\n      \#{options[:localized]}\n    end\n  EOV\n\n  if options[:localized]\n    class_eval <<-EOV\n      def \#{name}_translations\n        @\#{name} || {}\n      end\n\n      def \#{name}_translations=(translations)\n        translations.each { |locale, value| self.add_locale(locale) }\n        @\#{name} = translations.symbolize_keys\n      end\n    EOV\n  end\nend\n"