module Relaxo::Model::Base
Attributes
keys[R]
primary_key[R]
properties[R]
relationships[R]
type[R]
Public Class Methods
extended(child)
click to toggle source
# File lib/relaxo/model/base.rb, line 52 def self.extended(child) # $stderr.puts "#{self} extended -> #{child} (setup Base)" child.instance_variable_set(:@properties, {}) child.instance_variable_set(:@relationships, {}) child.instance_variable_set(:@keys, {}) child.instance_variable_set(:@primary_key, nil) default_type = child.name.split('::').last.gsub(/(.)([A-Z])/,'\1_\2').downcase! child.instance_variable_set(:@type, default_type) end
Public Instance Methods
metaclass()
click to toggle source
# File lib/relaxo/model/base.rb, line 64 def metaclass class << self; self; end end
parent_type(klass)
click to toggle source
# File lib/relaxo/model/base.rb, line 75 def parent_type klass @type = [klass.type, self.type].join('/') end
property(name, klass = nil)
click to toggle source
# File lib/relaxo/model/base.rb, line 115 def property(name, klass = nil) if @properties.key? name raise ArgumentError.new("Property #{name.inspect} already defined!") end @properties[name] = klass self.send(:define_method, name) do if @changed.include?(name) return @changed[name] elsif @attributes.include?(name) if klass value = @attributes[name] @changed[name] = klass.convert_from_primative(dataset, value) else @changed[name] = @attributes[name] end else nil end end self.send(:define_method, "#{name}=") do |value| @changed[name] = value end self.send(:define_method, "#{name}?") do value = self.send(name) if value.nil? or !value false elsif value.respond_to? :empty? !value.empty? else true end end end
unique(*keys)
click to toggle source
# File lib/relaxo/model/base.rb, line 109 def unique(*keys) lambda do |arguments| Path.escape keys.map{|key| arguments[key] || self.send(key) || 'null'} end end
view(name, *path, klass: self, index: nil)
click to toggle source
# File lib/relaxo/model/base.rb, line 79 def view(name, *path, klass: self, index: nil) # Support array as 2nd argument, e.g. # view :by_owner, [:type, 'by_owner', ...] if path.empty? path = [:type, name.to_s, name.to_s.split('_', 2).last.to_sym] elsif path.count == 1 and path.first.is_a? Array warn "Legacy view for #{self}: #{name} -> #{path}" path = path.first end key = Key.new(path, Array(index)) if index @keys[name] = key @primary_key ||= key end self.metaclass.send(:define_method, name) do |dataset, **arguments| Recordset.new(dataset, key.prefix_path(self, **arguments), klass) end self.metaclass.send(:define_method, "fetch_#{name}") do |dataset, **arguments| self.fetch(dataset, key.object_path(self, **arguments)) end self.send(:define_method, name) do |**arguments| Recordset.new(self.dataset, key.object_path(self, **arguments), self.class) end end