lib/transcriber/resource.rb in transcriber-0.0.1 vs lib/transcriber/resource.rb in transcriber-0.0.2
- old
+ new
@@ -1,32 +1,52 @@
module Transcriber
class Resource
- autoload :Serialization, 'transcriber/resource/serialization'
- autoload :Properties, 'transcriber/resource/properties'
- autoload :Property, 'transcriber/resource/properties/property'
- autoload :Embeddables, 'transcriber/resource/embeddables'
- autoload :Embeddable, 'transcriber/resource/embeddables/embeddable'
- autoload :Parser, 'transcriber/resource/parser'
- autoload :Responses, 'transcriber/resource/responses'
-
- extend Properties
- extend Embeddables
+ extend Builder
extend Parser
+ extend Response
include Serialization
+ cattr_accessor :id_key
+ cattr_writer :resource_name
+
def initialize(attrs = {})
attrs.map {|name, value| send("#{name}=", value)}
end
def self.keys
@keys ||= []
end
- def self.resources(entries)
- {entries: entries.map(&:resource)}
+ def self.relations
+ @keys.find_all {|k| k.kind_of?(Relation)}
end
- def resource
- self.class.keys.inject({}) {|buffer, key| buffer.merge key.to_resource(self)}
+ def self.resource_name(custom_name = nil)
+ @resource_name ||= custom_name or to_s.demodulize.tableize
+ end
+
+ def resource_id
+ __send__(id_key.name)
+ end
+
+ def resource(options = {})
+ root = options.fetch(:root, true)
+ {}.tap do |resource|
+ self.class.keys.inject(resource) {|buffer, key| buffer.merge!(key.to_resource(self))}
+ resource.merge!({links: links}) if root and self.class.relations.any?
+ end
+ end
+
+ def links
+ self.class.relations.map {|key| key.to_relation(self)}
+ end
+
+ def self.method_added(method_name)
+ return unless not_allowed_names.include?(method_name.to_s)
+ puts "warning: redefining '#{method_name}' may cause serious problems"
+ end
+
+ def self.not_allowed_names
+ ["resource_id", "resource", "links"]
end
end
end