Class: Hemp::Orm::RecordInterface
- Defined in:
- lib/hemp/orm/record_interface.rb
Direct Known Subclasses
Constant Summary collapse
Class Attribute Summary collapse
-
.internal_props ⇒ Object
Returns the value of attribute internal_props.
-
.properties ⇒ Object
Returns the value of attribute properties.
-
.sql_properties ⇒ Object
Returns the value of attribute sql_properties.
-
.table_name ⇒ Object
Returns the value of attribute table_name.
Class Method Summary collapse
- .all ⇒ Object
- .construct_sql_properties ⇒ Object
- .count ⇒ Object
- .create(hash_arg) ⇒ Object
- .create_table ⇒ Object
- .destroy(id) ⇒ Object
- .destroy_all ⇒ Object
- .expose_instance_vars ⇒ Object
- .find(id) ⇒ Object
- .get_properties ⇒ Object
- .initialize_model(row) ⇒ Object
- .property(name, options) ⇒ Object
- .set_instance_vars(scope, row) ⇒ Object
- .to_table(name) ⇒ Object
Class Attribute Details
.internal_props ⇒ Object
Returns the value of attribute internal_props.
7 8 9 |
# File 'lib/hemp/orm/record_interface.rb', line 7 def internal_props @internal_props end |
.properties ⇒ Object
Returns the value of attribute properties.
7 8 9 |
# File 'lib/hemp/orm/record_interface.rb', line 7 def properties @properties end |
.sql_properties ⇒ Object
Returns the value of attribute sql_properties.
7 8 9 |
# File 'lib/hemp/orm/record_interface.rb', line 7 def sql_properties @sql_properties end |
.table_name ⇒ Object
Returns the value of attribute table_name.
7 8 9 |
# File 'lib/hemp/orm/record_interface.rb', line 7 def table_name @table_name end |
Class Method Details
.all ⇒ Object
83 84 85 86 |
# File 'lib/hemp/orm/record_interface.rb', line 83 def all rows = SqlHelper.execute "select * from #{@table_name}" rows.map { |row| initialize_model row } end |
.construct_sql_properties ⇒ Object
45 46 47 48 |
# File 'lib/hemp/orm/record_interface.rb', line 45 def construct_sql_properties @internal_props = get_properties @sql_properties = @internal_props.join(", ") end |
.count ⇒ Object
77 78 79 80 81 |
# File 'lib/hemp/orm/record_interface.rb', line 77 def count row = SqlHelper.execute "select count(*) from #{@table_name}" row.first.first end |
.create(hash_arg) ⇒ Object
15 16 17 18 19 |
# File 'lib/hemp/orm/record_interface.rb', line 15 def create(hash_arg) new_model = const_get(name).new(hash_arg) new_model.save ? new_model : false end |
.create_table ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/hemp/orm/record_interface.rb', line 37 def create_table SqlHelper.execute "create table if not exists "\ "#{@table_name} (#{@properties.join(', ')})" construct_sql_properties expose_instance_vars end |
.destroy(id) ⇒ Object
88 89 90 91 |
# File 'lib/hemp/orm/record_interface.rb', line 88 def destroy(id) SqlHelper.execute "delete from #{@table_name} "\ "where id = ?", [id] end |
.destroy_all ⇒ Object
93 94 95 |
# File 'lib/hemp/orm/record_interface.rb', line 93 def destroy_all SqlHelper.execute "delete from #{@table_name}" end |
.expose_instance_vars ⇒ Object
50 51 52 53 54 |
# File 'lib/hemp/orm/record_interface.rb', line 50 def expose_instance_vars @properties.map(&:name).each do |name| const_get(self.name).class_eval { attr_accessor name.to_sym } end end |
.find(id) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/hemp/orm/record_interface.rb', line 69 def find(id) row = SqlHelper.execute "select * from #{@table_name} "\ "where id = ?", [id] return initialize_model(row.first) unless row.empty? nil end |
.get_properties ⇒ Object
30 31 32 33 34 35 |
# File 'lib/hemp/orm/record_interface.rb', line 30 def get_properties props = @properties.map(&:name) props.delete :id props end |
.initialize_model(row) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/hemp/orm/record_interface.rb', line 62 def initialize_model(row) model = const_get(name).new set_instance_vars model, row model end |
.property(name, options) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/hemp/orm/record_interface.rb', line 21 def property(name, ) @properties ||= [] new_property = Property.new(name, ) @properties << new_property unless @properties.include? new_property rescue DatabaseError => e puts e. exit end |
.set_instance_vars(scope, row) ⇒ Object
56 57 58 59 60 |
# File 'lib/hemp/orm/record_interface.rb', line 56 def set_instance_vars(scope, row) @properties.map(&:name).each_with_index do |name, index| scope.instance_variable_set "@#{name}", row[index] end end |
.to_table(name) ⇒ Object
10 11 12 13 |
# File 'lib/hemp/orm/record_interface.rb', line 10 def to_table(name) @table_name = name property :id, type: :integer, primary_key: true end |