lib/rasti/db/collection.rb in rasti-db-2.0.1 vs lib/rasti/db/collection.rb in rasti-db-2.1.0

- old
+ new

@@ -12,11 +12,11 @@ def collection_name @collection_name ||= underscore(demodulize(name)).to_sym end def collection_attributes - @collection_attributes ||= model.attributes - relations.keys + @collection_attributes ||= model.attributes - relations.keys - computed_attributes.keys end def primary_key @primary_key ||= :id end @@ -45,10 +45,14 @@ def queries @queries ||= Hash::Indifferent.new end + def computed_attributes + @computed_attributes ||= Hash::Indifferent.new + end + private def set_collection_name(collection_name) @collection_name = collection_name.to_sym end @@ -87,9 +91,14 @@ queries[name] = lambda || block define_method name do |*args| default_query.instance_exec(*args, &self.class.queries.fetch(name)) end + end + + def computed_attribute(name, &block) + raise "Computed Attribute #{name} already exists" if computed_attributes.key? name + computed_attributes[name] = block.call end end def initialize(environment) \ No newline at end of file