Module: Serialist::InstanceMethods

Defined in:
lib/serialist/serialist_module.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/serialist/serialist_module.rb', line 69

def method_missing(method, *args, &block)
  begin
    super
  rescue NoMethodError
    slug = self.send(self.class.serialist_field)
    case method.to_s.last
    when "?"
      if args.empty?
        slug && ![nil, false, "false", :false].include?(slug[method.to_s[0..-2].to_sym])
      else
        slug && (slug[method.to_s[0..-2].to_sym] == args.first)
      end
    when "="
      self.send(self.class.serialist_field.to_s + "=", Hash.new) unless slug
      self.send(self.class.serialist_field)[method.to_s[0..-2].to_sym] = args.first
    else
      slug && slug[method.to_sym]
    end
  end
end

Instance Method Details

#attributes=(new_attributes, guard_protected_attributes = true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/serialist/serialist_module.rb', line 51

def attributes=(new_attributes, guard_protected_attributes = true)
  return if new_attributes.nil? 
  attributes = new_attributes.dup
  attributes.stringify_keys!
  attributes.each do |k, v|
    unless k.include?("(") || respond_to?(:"#{k}")
      self.class.send(:define_method, :"#{k}=") do |param|
        self.send(self.class.serialist_field.to_s + "=", Hash.new) unless self.send(self.class.serialist_field)
        self.send(self.class.serialist_field)[k.to_sym] = param
      end
      self.class.send(:define_method, :"#{k}") do 
        self.send(self.class.serialist_field)[k.to_sym]
      end
    end
  end
  super
end