Class: ActiveMDB::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_mdb/base.rb

Overview

Hi, I am an imitation of ActiveRecord.

Constant Summary collapse

@@pluralize_table_names =
true

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/active_mdb/base.rb', line 5

def initialize(attributes=nil)
  @attributes = attributes unless attributes.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object (private)



173
174
175
176
177
178
179
180
# File 'lib/active_mdb/base.rb', line 173

def method_missing(method_id, *args, &block)
  method_name = MDBTools.methodize(method_id.to_s)
  if @attributes.include?(method_name)
    value = @attributes[method_name]
  else
    super
  end
end

Class Attribute Details

.mdbObject (readonly)

Returns the value of attribute mdb.



14
15
16
# File 'lib/active_mdb/base.rb', line 14

def mdb
  @mdb
end

.mdb_fileObject

Returns the value of attribute mdb_file.



13
14
15
# File 'lib/active_mdb/base.rb', line 13

def mdb_file
  @mdb_file
end

Class Method Details

.column_for_field(column_name) ⇒ Object

given the name of a column, return the Column object



146
147
148
# File 'lib/active_mdb/base.rb', line 146

def column_for_field(column_name)
  columns.detect {|c| c.name == column_name.to_s}
end

.column_for_method(method_name) ⇒ Object



150
151
152
# File 'lib/active_mdb/base.rb', line 150

def column_for_method(method_name)
  columns.detect {|c| c.method_name == method_name.to_s}
end

.column_namesObject



44
45
46
# File 'lib/active_mdb/base.rb', line 44

def column_names
  @mdb.column_names(table_name)
end

.columnsObject

Returns an array of column objects for the table associated with this class.



36
37
38
39
40
41
42
# File 'lib/active_mdb/base.rb', line 36

def columns
  unless @columns
    @columns = @mdb.columns(table_name)
    # @columns.each {|column| column.primary = column.name == primary_key}
  end
  @columns
end

.conditions_from_hash(hash) ⇒ Object

the conditions hash keys are column names, the values are search values e.g. search_with_hash(:first_name => ‘Matthew’, :last_name => ‘King’)



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/active_mdb/base.rb', line 129

def conditions_from_hash(hash)
  MDBTools.compile_conditions(hash) do |column_name, value|
    column = column_for_method(column_name) || column_for_field(column_name)
    raise ArgumentError, "No column corresponding to #{column_name}" unless column
    case column.klass.to_s
    when 'Fixnum', 'Float'
      "#{column.name} = #{value}"
    when 'String'
      "#{column.name} LIKE '%#{value}%'"
    when 'Object'
      value = value ? 1 : 0
      "#{column.name} IS #{value}"
    end
  end
end

.countObject

How many? Requires that the primary_key return a valid field.



66
67
68
# File 'lib/active_mdb/base.rb', line 66

def count
  @mdb.count(table_name, primary_key)
end

.define_attr_method(name, value = nil, &block) ⇒ Object

borrowed from ActiveRecord.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_mdb/base.rb', line 90

def define_attr_method(name, value=nil, &block)
  sing = class << self; self; end
  sing.send :alias_method, "original_#{name}", name
  if block_given?
    sing.send :define_method, name, &block
  else
    # use eval instead of a block to work around a memory leak in dev
    # mode in fcgi
    sing.class_eval "def #{name}; #{value.to_s.inspect}; end"
  end
end

.find_all(conditions_hash) ⇒ Object

the conditions hash keys are column names, the values are search values find_all :superhero_name => ‘The Ironist’, :powers => ‘Wit’

mdb-sql doesn’t implement LIMIT yet, so this method pulls all results and calls Array#first on them. Ooky.



85
86
87
# File 'lib/active_mdb/base.rb', line 85

def find_all(conditions_hash)
  find_from_hash(conditions_hash)
end

.find_first(conditions_hash) ⇒ Object

the conditions hash keys are column names, the values are search values find_first :superhero_name => ‘The Ironist’, :powers => ‘Wit’

mdb-sql doesn’t implement LIMIT yet, so this method pulls all results and calls Array#first on them. Ooky.



75
76
77
78
# File 'lib/active_mdb/base.rb', line 75

def find_first(conditions_hash)
  # rekey_hash(conditions_hash)
  find_from_hash(conditions_hash).first 
end

.find_from_hash(hash) ⇒ Object

takes a hash where the keys are column names (string or symbol) and the values are the associated values.



122
123
124
125
# File 'lib/active_mdb/base.rb', line 122

def find_from_hash(hash)
  conditions = conditions_from_hash(hash)
  find_where(conditions)
end

.find_where(conditions) ⇒ Object

supply a conditions string that would nestle gently in a WHERE clause, after the WHERE but before the.



116
117
118
# File 'lib/active_mdb/base.rb', line 116

def find_where(conditions)
  MDBTools.sql_select_where(mdb_file, table_name, nil, conditions).collect! { |record| instantiate(record) }
end

.primary_keyObject

returns ‘id’ unless you overwrite this method using set_primary_key



49
50
51
# File 'lib/active_mdb/base.rb', line 49

def primary_key
  'id'
end

.rekey_hash(conditions_hash) ⇒ Object

relies on stuff that doesn’t work right now



103
104
105
106
107
108
109
110
111
112
# File 'lib/active_mdb/base.rb', line 103

def rekey_hash(conditions_hash)
  conditions_hash.each do |key,value|
    column = self[key.to_s]
    if column.boolean?
      key = column.name + '!'
    else
      key = column.name
    end
  end
end

.set_mdb_file(file) ⇒ Object

set the path to the MDB file



17
18
19
20
# File 'lib/active_mdb/base.rb', line 17

def set_mdb_file(file)
  @mdb_file = file
  @mdb = MDB.new(file)
end

.set_primary_key(value = nil, &block) ⇒ Object Also known as: primary_key=

specify the field to be used as a primary key for those operations that require one. At this time, that isn’t really anything except count.



56
57
58
# File 'lib/active_mdb/base.rb', line 56

def set_primary_key(value = nil, &block)
  define_attr_method :primary_key, value, &block
end

.set_table_name(value = nil, &block) ⇒ Object Also known as: table_name=

borrowed from ActiveRecord



30
31
32
# File 'lib/active_mdb/base.rb', line 30

def set_table_name(value = nil, &block)
  define_attr_method :table_name, value, &block
end

.table_exists?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/active_mdb/base.rb', line 61

def table_exists?
  @mdb.tables.include? table_name
end

.table_nameObject

set the name of the table in the MDB file



23
24
25
26
27
# File 'lib/active_mdb/base.rb', line 23

def table_name
  table_name = Inflector.underscore(Inflector.demodulize(self.to_s))
  table_name = Inflector.pluralize(table_name) if pluralize_table_names
  table_name
end