Module: DB::Model::Record::Base
- Defined in:
- lib/db/model/record.rb
Instance Attribute Summary collapse
-
#key_columns ⇒ Object
readonly
Returns the value of attribute key_columns.
-
#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
-
#create(context, **attributes) ⇒ Object
Directly create one record.
-
#find(context, *key) ⇒ Object
Find records which match the given primary key.
- #find_predicate(*key) ⇒ Object
-
#insert(context, keys, rows, **attributes) ⇒ Object
Directly insert one or more records.
- #primary_key ⇒ Object
- #property(name, klass = nil) ⇒ Object
- #where(context, *arguments, **options, &block) ⇒ Object
Instance Attribute Details
#key_columns ⇒ Object (readonly)
Returns the value of attribute key_columns.
58 59 60 |
# File 'lib/db/model/record.rb', line 58 def key_columns @key_columns end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
56 57 58 |
# File 'lib/db/model/record.rb', line 56 def properties @properties end |
#relationships ⇒ Object (readonly)
Returns the value of attribute relationships.
57 58 59 |
# File 'lib/db/model/record.rb', line 57 def relationships @relationships end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
55 56 57 |
# File 'lib/db/model/record.rb', line 55 def type @type end |
Class Method Details
.extended(klass) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/db/model/record.rb', line 45 def self.extended(klass) klass.instance_variable_set(:@properties, {}) klass.instance_variable_set(:@relationships, {}) klass.instance_variable_set(:@key_columns, [:id].freeze) default_type = klass.name.split('::').last.gsub(/(.)([A-Z])/,'\1_\2').downcase!.to_sym klass.instance_variable_set(:@type, default_type) end |
Instance Method Details
#create(context, **attributes) ⇒ Object
Directly create one record.
67 68 69 70 71 72 |
# File 'lib/db/model/record.rb', line 67 def create(context, **attributes) Statement::Insert.new(self, Statement::Fields.new(attributes.keys), Statement::Tuple.new(attributes.values) ).to_a(context).first end |
#find(context, *key) ⇒ Object
Find records which match the given primary key.
89 90 91 92 93 94 |
# File 'lib/db/model/record.rb', line 89 def find(context, *key) Statement::Select.new(self, where: find_predicate(*key), limit: Statement::Limit::ONE ).to_a(context).first end |
#find_predicate(*key) ⇒ Object
96 97 98 |
# File 'lib/db/model/record.rb', line 96 def find_predicate(*key) Statement::Equal.new(self, self.primary_key, key) end |
#insert(context, keys, rows, **attributes) ⇒ Object
Directly insert one or more records.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/db/model/record.rb', line 75 def insert(context, keys, rows, **attributes) if attributes.any? fields = Statement::Fields.new(attributes.keys + keys) values = attributes.values tuples = rows.map{|row| Statement::Tuple.new(values + row)} else fields = Statement::Fields.new(keys) tuples = rows.map{|row| Statement::Tuple.new(row)} end return Statement::Insert.new(self, fields, Statement::Multiple.new(tuples)).to_a(context) end |
#primary_key ⇒ Object
60 61 62 63 64 |
# File 'lib/db/model/record.rb', line 60 def primary_key @key_columns.map do |name| DB::Identifier[@type, name] end end |
#property(name, klass = nil) ⇒ Object
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/db/model/record.rb', line 104 def property(name, klass = nil) if @properties.key?(name) raise ArgumentError.new("Property #{name.inspect} already defined!") end @properties[name] = klass if klass self.define_method(name) do if @changed&.key?(name) return @changed[name] elsif @attributes.key?(name) value = @attributes[name] klass.load(value) else nil end end else self.define_method(name) do if @changed&.key?(name) return @changed[name] else @attributes[name] end end end self.define_method(:"#{name}=") do |value| @changed ||= Hash.new @changed[name] = value end self.define_method(:"#{name}?") do value = self.send(name) if !value false elsif value.respond_to?(:empty?) !value.empty? else true end end end |