Class: PopulateMe::Document

Direct Known Subclasses

Mongo

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from PopulateMe::DocumentMixins::Validation

#_errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PopulateMe::DocumentMixins::Persistence

#attachment, #delete, included, #perform_create, #perform_delete, #perform_update, #persistent_instance_variables, #save

Methods included from PopulateMe::DocumentMixins::Validation

#error_on, #error_report, #errors, #valid?, #validate

Methods included from PopulateMe::DocumentMixins::Callbacks

#ensure_delete_attachments, #ensure_delete_related, #ensure_id, #ensure_new, #ensure_not_new, #ensure_position, #exec_callback, included, #recurse_callback

Methods included from PopulateMe::DocumentMixins::AdminAdapter

#admin_image_url, included, #to_admin_form, #to_admin_list_item, #to_admin_url

Methods included from PopulateMe::DocumentMixins::Schema

#field_applicable?, included, #relationship_applicable?

Methods included from PopulateMe::DocumentMixins::Outcasting

#outcast, #outcast_attachment, #outcast_list, #outcast_price, #outcast_select, #outcast_string

Methods included from PopulateMe::DocumentMixins::Typecasting

#typecast, #typecast_attachment, #typecast_date, #typecast_datetime, #typecast_integer, #typecast_price, #typecast_select

Constructor Details

#initialize(attributes = nil) ⇒ Document

Returns a new instance of Document.



80
81
82
83
84
# File 'lib/populate_me/document.rb', line 80

def initialize attributes=nil 
  self._is_new = true
  set attributes if attributes
  self._errors = {}
end

Class Attribute Details

.settingsObject

inheritable settings



71
72
73
# File 'lib/populate_me/document.rb', line 71

def settings
  @settings
end

Instance Attribute Details

#_is_newObject

Returns the value of attribute _is_new.



78
79
80
# File 'lib/populate_me/document.rb', line 78

def _is_new
  @_is_new
end

#_oldObject

Returns the value of attribute _old.



78
79
80
# File 'lib/populate_me/document.rb', line 78

def _old
  @_old
end

#idObject

Returns the value of attribute id.



78
79
80
# File 'lib/populate_me/document.rb', line 78

def id
  @id
end

Class Method Details

.cast(o = {}, &block) ⇒ Object

Raises:

  • (TypeError)


62
63
64
65
66
67
68
# 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 = {}) ⇒ Object



58
59
60
# 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) ⇒ Object



41
42
43
44
45
# 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

.set(name, value) ⇒ Object



72
73
74
# File 'lib/populate_me/document.rb', line 72

def set name, value
  self.settings[name] = value
end

.to_sObject



47
48
49
# File 'lib/populate_me/document.rb', line 47

def to_s
  super.gsub(/[A-Z]/, ' \&')[1..-1].gsub('::','')
end

.to_s_pluralObject



55
# File 'lib/populate_me/document.rb', line 55

def to_s_plural; WebUtils.pluralize(self.to_s); end

.to_s_shortObject



51
52
53
# File 'lib/populate_me/document.rb', line 51

def to_s_short
  self.name[/[^:]+$/].gsub(/[A-Z]/, ' \&')[1..-1]
end

.to_s_short_pluralObject



56
# File 'lib/populate_me/document.rb', line 56

def to_s_short_plural; WebUtils.pluralize(self.to_s_short); end

Instance Method Details

#==(other) ⇒ Object



126
127
128
129
# File 'lib/populate_me/document.rb', line 126

def == other
  return false unless other.respond_to?(:to_h)
  other.to_h==to_h
end

#inspectObject



86
87
88
# File 'lib/populate_me/document.rb', line 86

def inspect
  "#<#{self.class.name}:#{to_h.inspect}>"
end

#nested_docsObject



118
119
120
121
122
123
124
# 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?Boolean

Returns:

  • (Boolean)


97
# File 'lib/populate_me/document.rb', line 97

def new?; self._is_new; end

#set(attributes) ⇒ Object



131
132
133
134
135
136
137
138
139
# 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 = {}) ⇒ Object



141
142
143
144
145
146
147
148
# 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 = {}) ⇒ Object

Raises:

  • (TypeError)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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

#settingsObject

class settings



172
173
174
# File 'lib/populate_me/document.rb', line 172

def settings
  self.class.settings
end

#snapshotObject



113
114
115
116
# File 'lib/populate_me/document.rb', line 113

def snapshot
  self._old = self.to_h
  self
end

#to_hObject Also known as: to_hash



99
100
101
102
103
104
105
106
107
108
109
110
# 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

#to_sObject



90
91
92
93
94
95
# 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