class User
include Elasticsearch::Persistence::Model
def initialize(attrs = {})
set_attributes(attrs)
super attrs
end
def set_attributes(attrs = {})
attrs.each do |attr, value|
set_attribute(attr, value)
end
end
def set_attribute(attribute, value)
attribute = attribute.to_sym
unless self.class.attribute_set.map { |attr| attr.name }.include? attribute
self.class.attribute attribute, value.class
end
send "#{attribute.to_s}=", value
end
def []=(attribute, value)
set_attribute(attribute, value)
end
def method_missing(meth, *args, &block)
if meth.to_s =~ /(.+)=$/
set_attribute($1, args.first)
else
super
end
end
end
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme