Module: Serialist::ClassMethods
- Defined in:
- lib/serialist/serialist_module.rb
Instance Attribute Summary collapse
-
#serialist_field ⇒ Object
Returns the value of attribute serialist_field.
-
#serialist_options ⇒ Object
Returns the value of attribute serialist_options.
Instance Method Summary collapse
- #define_access_method(method) ⇒ Object
- #inherited(subclass) ⇒ Object
- #serialist(serialist_field, serialist_options = []) ⇒ Object
Instance Attribute Details
#serialist_field ⇒ Object
Returns the value of attribute serialist_field.
9 10 11 |
# File 'lib/serialist/serialist_module.rb', line 9 def serialist_field @serialist_field end |
#serialist_options ⇒ Object
Returns the value of attribute serialist_options.
8 9 10 |
# File 'lib/serialist/serialist_module.rb', line 8 def end |
Instance Method Details
#define_access_method(method) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/serialist/serialist_module.rb', line 51 def define_access_method(method) serialist_field = self.serialist_field case method.last when "?" define_method method do |*param| return false unless (slug = self.send(serialist_field)) if param.empty? ![nil, false, "false", :false, "0"].include?(slug[method[0..-2].to_sym]) else slug[method[0..-2].to_sym] == param.first end end when "=" define_method method do |param| self.send(serialist_field.to_s + "=", Hash.new) unless self.send(serialist_field) # needed to get dirty slug = self.send(serialist_field).clone slug[method[0..-2].to_sym] = param write_attribute(serialist_field.to_s, slug) end else define_method method do return nil unless (slug = self.send(serialist_field)) method.ends_with?("_id") ? slug[method.to_sym].to_i : slug[method.to_sym] end end end |
#inherited(subclass) ⇒ Object
79 80 81 82 83 |
# File 'lib/serialist/serialist_module.rb', line 79 def inherited(subclass) super subclass.instance_variable_set("@serialist_field", @serialist_field) subclass.instance_variable_set("@serialist_options", ) end |
#serialist(serialist_field, serialist_options = []) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/serialist/serialist_module.rb', line 11 def serialist(serialist_field, = []) @serialist_field = serialist_field = serialize(@serialist_field, Hash) include Serialist::DirtyAdditions class_eval do unless method_defined?(:changes) def changes(meth, *args, &block); super; end end alias_method :old_changes, :changes alias_method :changes, :serialist_changes end if .empty? # catch-all mode include Serialist::InstanceMethods class_eval do # alias method chaining unless method_defined? :method_missing def method_missing(meth, *args, &block); super; end end alias_method :old_method_missing, :method_missing alias_method :method_missing, :serialist_method_missing unless method_defined?(:attributes=) def attributes=(meth, *args, &block); super; end end alias_method :old_attributes=, :attributes= alias_method :attributes=, :serialist_attributes= end else .each do |field| cols = self.columns.map{|c|c.name.to_s} raise Exception.new("Column #{field} already exist for #{self.name}") if cols.include?(field.to_s) define_access_method(field.to_s) define_access_method(field.to_s + "?") define_access_method(field.to_s + "=") end end end |