Module: Relaxo::Model::Base
- Defined in:
- lib/relaxo/model/base.rb
Constant Summary collapse
- DEFAULT_VIEW_OPTIONS =
{:include_docs => true}
- DEFAULT_RELATIONSHIP_OPTIONS =
{ :key => lambda {|object, query| query[:key] = object.id}, :include_docs => true }
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#relationships ⇒ Object
readonly
Returns the value of attribute relationships.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #metaclass ⇒ Object
- #property(name, klass = nil) ⇒ Object
- #relationship(name, path, *args, &block) ⇒ Object
- #view(name, path, *args) ⇒ Object
Instance Attribute Details
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
40 41 42 |
# File 'lib/relaxo/model/base.rb', line 40 def properties @properties end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
41 42 43 |
# File 'lib/relaxo/model/base.rb', line 41 def relationships @relationships end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
39 40 41 |
# File 'lib/relaxo/model/base.rb', line 39 def type @type end |
Class Method Details
.extended(child) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/relaxo/model/base.rb', line 26 def self.extended(child) # $stderr.puts "#{self} extended -> #{child} (setup Base)" child.instance_variable_set(:@properties, {}) child.instance_variable_set(:@relationships, {}) default_type = child.name.split('::').last.gsub(/(.)([A-Z])/,'\1_\2').downcase! child.instance_variable_set(:@type, default_type) end |
Instance Method Details
#metaclass ⇒ Object
35 36 37 |
# File 'lib/relaxo/model/base.rb', line 35 def class << self; self; end end |
#property(name, klass = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/relaxo/model/base.rb', line 94 def property(name, klass = nil) name = name.to_s @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(@database, 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 || value == false false elsif value.respond_to? :empty? !value.empty? else true end end end |
#relationship(name, path, *args, &block) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/relaxo/model/base.rb', line 60 def relationship(name, path, *args, &block) = Hash === args.last ? args.pop : DEFAULT_RELATIONSHIP_OPTIONS klass = block || args.pop || [:class] @relationships[name] = # This reduction returns a single result, so just provide the first row directly: reduction = .delete(:reduction) = .dup update_key_function(, :key) update_key_function(, :startkey) update_key_function(, :endkey) self.send(:define_method, name) do |query = {}| query = query.merge() [:key, :startkey, :endkey].each do |name| if [name].respond_to? :call [name].call(self, query) end end recordset = Recordset.new(@database, @database.view(path, query), klass) if reduction == :first recordset.first else recordset end end end |
#view(name, path, *args) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/relaxo/model/base.rb', line 45 def view(name, path, *args) = Hash === args.last ? args.pop : DEFAULT_VIEW_OPTIONS klass = args.pop || [:class] self..send(:define_method, name) do |database, query = {}| records = database.view(path, query.merge()) Recordset.new(database, records, klass) end end |