module RRRMatey::StringModel

Attributes

content_type[RW]
id[RW]

Public Class Methods

included(base) click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 13
def self.included(base)
    base.extend ConnectionMethods
    base.extend NamespacedKeyMethods
    base.extend FieldDefinitionMethods
    base.extend IndexMethods
    base.extend FindMethods
    base.extend DeleteMethods
    base.extend ConsumerAdapterMethods
end

Public Instance Methods

delete() click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 44
def delete()
    self.class.delete(id)
end
save() click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 30
def save()
    raise UnsupportedTypeError.new(content_type) if content_type == 'application/unknown'
    raise InvalidModelError if id.blank?
    raise InvalidModelError if has_valid? && !valid?
    h = to_hash()
    s = hash_to_typed_string(h)
    unless self.class.cache_proxy.nil?
        self.class.cache_proxy.with { |r|
            r.set(self.class.namespaced_key(id), s)
        }
    end
    self
end
to_json(opts = {}) click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 48
def to_json(opts = {})
    to_consumer_hash.to_json
end
to_xml(opts = {}) click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 52
def to_xml(opts = {})
    to_consumer_hash.to_xml(:root => self.class.item_name)
end

Private Instance Methods

has_valid?() click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 68
def has_valid?
    @has_valid ||= respond_to?(:valid?)
end
hash_to_typed_string(h) click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 80
def hash_to_typed_string(h)
    case content_type
    when 'application/json'
        { self.class.item_name => h }.to_json
    when 'application/xml'
        h.to_xml(:root => self.class.item_name, :skip_instruct => true,
                 :indent => 0)
    else
        raise UnknownContentTypeError
    end
end
to_consumer_hash() click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 58
def to_consumer_hash
    h = {
        'id' => id
    }
    self.class.consumer_fields.each do |f|
        h[f] = send(f.to_sym)
    end
    h
end
to_hash() click to toggle source
# File lib/rrrmatey/string_model/string_model.rb, line 72
def to_hash()
    h = {}
    unless self.class.fields.nil?
        self.class.fields.each {|f| h[f.to_s] = send(f) }
    end
    h
end