lib/praxis-mapper/resource.rb in praxis-mapper-4.1.2 vs lib/praxis-mapper/resource.rb in praxis-mapper-4.2

- old
+ new

@@ -27,13 +27,16 @@ class Resource extend Finalizable attr_accessor :record + @properties = {} + class << self attr_reader :model_map attr_reader :decorations + attr_reader :properties end # TODO: also support an attribute of sorts on the versioned resource module. ie, V1::Resources.api_version. # replacing the self.superclass == Praxis::Mapper::Resource condition below. def self.inherited(klass) @@ -44,14 +47,15 @@ # will have a common Base class, and so should share # a model_map if self.superclass == Praxis::Mapper::Resource @model_map = Hash.new else - @model_map = self.superclass.model_map + @model_map = self.superclass.model_map end @decorations = {} + @properties = self.superclass.properties.clone end end #TODO: Take symbol/string and resolve the klass (but lazily, so we don't care about load order) @@ -67,10 +71,14 @@ def self.decorate(name, &block) self.decorations[name] = Class.new(ResourceDecorator, &block) end + def self.property(name, **options) + self.properties[name] = options + end + def self._finalize! finalize_resource_delegates define_model_accessors define_decorators @@ -106,14 +114,14 @@ end end def self.define_decorator(name, block) unless self.instance_methods.include?(name) - # assume it'll be a regular accessor and create it + # assume it'll be a regular accessor and create it self.define_accessor(name) end - # alias original method and wrap it + # alias original method and wrap it raw_name = "_raw_#{name}" alias_method(raw_name.to_sym, name) module_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} @@ -135,20 +143,19 @@ raise "No resource class corresponding to the model class '#{record.class}' is defined. (Did you forget to define '#{version}::#{resource_name}'?)" end end - def self.wrap(records) - case records - when nil - return [] - when Enumerable - return records.compact.collect { |record| self.for_record(record) } - else - return self.for_record(records) + def self.wrap(records) + case records + when nil + return [] + when Enumerable + return records.compact.collect { |record| self.for_record(record) } + else + return self.for_record(records) + end end - end - def self.get(condition) record = self.model.get(condition)