Module: Relaxo::Model::Base
- Defined in:
- lib/relaxo/model/base.rb
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
-
#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
- #parent_type(klass) ⇒ Object
- #property(name, klass = nil) ⇒ Object
- #unique(*keys) ⇒ Object
- #view(name, *path, klass: self, index: nil) ⇒ Object
Instance Attribute Details
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
72 73 74 |
# File 'lib/relaxo/model/base.rb', line 72 def keys @keys end |
#primary_key ⇒ Object (readonly)
Returns the value of attribute primary_key.
73 74 75 |
# File 'lib/relaxo/model/base.rb', line 73 def primary_key @primary_key end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
69 70 71 |
# File 'lib/relaxo/model/base.rb', line 69 def properties @properties end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
70 71 72 |
# File 'lib/relaxo/model/base.rb', line 70 def relationships @relationships end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
68 69 70 |
# File 'lib/relaxo/model/base.rb', line 68 def type @type end |
Class Method Details
.extended(child) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# 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 |
Instance Method Details
#metaclass ⇒ Object
64 65 66 |
# File 'lib/relaxo/model/base.rb', line 64 def class << self; self; end end |
#parent_type(klass) ⇒ Object
75 76 77 |
# File 'lib/relaxo/model/base.rb', line 75 def parent_type klass @type = [klass.type, self.type].join('/') end |
#property(name, klass = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# 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) ⇒ Object
109 110 111 112 113 |
# 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) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# 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..send(:define_method, name) do |dataset, **arguments| Recordset.new(dataset, key.prefix_path(self, **arguments), klass) end self..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 |