class PopulateMe::Document
Attributes
settings[RW]
inheritable settings
_is_new[RW]
_old[RW]
id[RW]
Public Class Methods
cast(o={})
click to toggle source
# File lib/populate_me/document.rb, line 62 def cast o={}, &block target = block.arity==0 ? instance_eval(&block) : block.call(self) return nil if target.nil? return from_hash(target, o) if target.is_a?(Hash) return target.map{|t| from_hash(t,o)} if target.respond_to?(:map) raise(TypeError, "The block passed to #{self.name}::cast is meant to return a Hash or a list of Hash which respond to `map`") end
from_hash(hash, o={})
click to toggle source
# File lib/populate_me/document.rb, line 58 def from_hash hash, o={} self.new(_is_new: false).set_from_hash(hash, o).snapshot end
inherited(sub)
click to toggle source
Calls superclass method
# File lib/populate_me/document.rb, line 41 def inherited sub super sub.callbacks = WebUtils.deep_copy callbacks sub.settings = settings.dup # no deep copy because of Mongo.settings.db end
new(attributes=nil)
click to toggle source
# File lib/populate_me/document.rb, line 80 def initialize attributes=nil self._is_new = true set attributes if attributes self._errors = {} end
set(name, value)
click to toggle source
# File lib/populate_me/document.rb, line 72 def set name, value self.settings[name] = value end
to_s()
click to toggle source
Calls superclass method
# File lib/populate_me/document.rb, line 47 def to_s super.gsub(/[A-Z]/, ' \&')[1..-1].gsub('::','') end
to_s_plural()
click to toggle source
# File lib/populate_me/document.rb, line 55 def to_s_plural; WebUtils.pluralize(self.to_s); end
to_s_short()
click to toggle source
# File lib/populate_me/document.rb, line 51 def to_s_short self.name[/[^:]+$/].gsub(/[A-Z]/, ' \&')[1..-1] end
to_s_short_plural()
click to toggle source
# File lib/populate_me/document.rb, line 56 def to_s_short_plural; WebUtils.pluralize(self.to_s_short); end
Public Instance Methods
==(other)
click to toggle source
# File lib/populate_me/document.rb, line 126 def == other return false unless other.respond_to?(:to_h) other.to_h==to_h end
inspect()
click to toggle source
# File lib/populate_me/document.rb, line 86 def inspect "#<#{self.class.name}:#{to_h.inspect}>" end
nested_docs()
click to toggle source
# File lib/populate_me/document.rb, line 118 def nested_docs persistent_instance_variables.map do |var| instance_variable_get var end.find_all do |val| is_nested_docs?(val) end.flatten end
new?()
click to toggle source
# File lib/populate_me/document.rb, line 97 def new?; self._is_new; end
set(attributes)
click to toggle source
# File lib/populate_me/document.rb, line 131 def set attributes attributes.dup.each do |k,v| setter = "#{k}=" if respond_to? setter __send__ setter, v end end self end
set_defaults(o={})
click to toggle source
# File lib/populate_me/document.rb, line 141 def set_defaults o={} self.class.fields.each do |k,v| if v.key?(:default)&&(__send__(k).nil?||o[:force]) set k.to_sym => WebUtils.get_value(v[:default],self) end end self end
set_from_hash(hash, o={})
click to toggle source
# File lib/populate_me/document.rb, line 150 def set_from_hash hash, o={} raise(TypeError, "#{hash} is not a Hash") unless hash.is_a? Hash hash = hash.dup # Leave original untouched hash.delete('_class') hash.each do |k,v| getter = k.to_sym if is_nested_hash_docs?(v) break unless respond_to?(getter) __send__(getter).clear v.each do |d| obj = WebUtils.resolve_class_name(d['_class']).new.set_from_hash(d,o) __send__(getter) << obj end else v = typecast(getter,v) if o[:typecast] set getter => v end end self end
settings()
click to toggle source
class settings
# File lib/populate_me/document.rb, line 172 def settings self.class.settings end
snapshot()
click to toggle source
# File lib/populate_me/document.rb, line 113 def snapshot self._old = self.to_h self end
to_h()
click to toggle source
# File lib/populate_me/document.rb, line 99 def to_h persistent_instance_variables.inject({'_class'=>self.class.name}) do |h,var| k = var.to_s[1..-1] v = instance_variable_get var if is_nested_docs?(v) h[k] = v.map(&:to_h) else h[k] = v end h end end
Also aliased as: to_hash
to_s()
click to toggle source
# File lib/populate_me/document.rb, line 90 def to_s default = "#{self.class}#{' ' unless WebUtils.blank?(self.id)}#{self.id}" return default if self.class.label_field.nil? me = self.__send__(self.class.label_field).dup WebUtils.blank?(me) ? default : me end
Private Instance Methods
is_nested_docs?(val)
click to toggle source
# File lib/populate_me/document.rb, line 179 def is_nested_docs? val # Differenciate nested docs array from other king of array val.is_a?(Array) and !val.empty? and val[0].is_a?(PopulateMe::Document) end
is_nested_hash_docs?(val)
click to toggle source
# File lib/populate_me/document.rb, line 184 def is_nested_hash_docs? val # Differenciate nested docs array from other king of array # when from a hash val.is_a?(Array) and !val.empty? and val[0].is_a?(Hash) and val[0].has_key?('_class') end