Method: ActiveCouch::Base#initialize
- Defined in:
- lib/active_couch/base.rb
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 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 |
# File 'lib/active_couch/base.rb', line 6 def initialize(params = {}) # Object instance variable @attributes = {}; @associations = {}; @callbacks = Hash.new; @connection = self.class.connection klass_atts = self.class.attributes; klass_assocs = self.class.associations; klass_callbacks = self.class.callbacks # ActiveCouch::Connection object will be readable in every # object instantiated from a subclass of ActiveCouch::Base SPECIAL_MEMBERS.each do |k| self.instance_eval "def #{k}; @#{k}; end" end klass_atts.each_key do |k| @attributes[k] = klass_atts[k].dup self.instance_eval "def #{k}; attributes[:#{k}].value; end" self.instance_eval "def #{k}=(val); attributes[:#{k}].value = val; end" end klass_assocs.each_key do |k| @associations[k] = HasManyAssociation.new(klass_assocs[k].name, :class => klass_assocs[k].klass) self.instance_eval "def #{k}; associations[:#{k}].container; end" # If you have has_many :people, this will add a method called add_person to the object instantiated # from the class self.instance_eval "def add_#{Inflector.singularize(k)}(val); associations[:#{k}].push(val); end" end klass_callbacks.each_key do |k| @callbacks[k] = klass_callbacks[k].dup end DEFAULT_ATTRIBUTES.each do |x| self.instance_eval "def #{x}; _#{x}; end" self.instance_eval "def #{x}=(val); self._#{x}=(val); end" end # Set any instance variables if any, which are present in the params hash from_hash(params) end |