Commit c1e38114 by ivan Lan

Connect database successfully & Solve the problem of creating constant having “::” within its name

parent 888f221b
require 'weather-model/version'
module WeatherModel
require 'weather-model/source_schema'
def self.create_classes
Weather::Model::SourceSchema.each do |source|
Object.const_set("#{source}::Mysql", Class.new(Source::Mysql))
Object.const_set("#{source}::Ohm", Class.new(Source::Ohm))
Dir[File.dirname(__FILE__) + '/weather-model/*.rb'].each { |file| require file }
def self.included base
WeatherModel::SourceSchema.sorts.each { |sort| Object.const_set(sort, Module.new)}
WeatherModel::SourceSchema.constants.each do |source|
settings = SourceSchema.const_get(source)
Object.const_get(settings[:sort]).const_set("#{source.to_s.gsub('_', '::')}", Class.new(Storer::Mysql)).use_settings(settings)
end
end
end
module WeatherModel
class Engine < Rails::Engine
require 'rails/engine'
class Engine < ::Rails::Engine
end
end
\ No newline at end of file
module WeatherModel
module SourceSchema
def self.sorts
['Forecast']
end
ShanghaiTen = {
sort: 'Forecast',
table_name: :forecasts,
schema: {
datetime: :datetime,
......
module WeatherModel
module Storer
class Mysql < ActiveRecord::Base
@@source = self.name[0..-8]
require 'active_record'
require 'ohm'
require "ohm/expire"
@@settings = Source::Schema.const_get(self.name[0..-8])
self.table_name = @@settings[:table_name]
end
class Mysql < ::ActiveRecord::Base
after_save :update_ohm
def self.use_settings settings
self.table_name = settings[:table_name]
Ohm.use_settings settings
end
class Ohm < Ohm::Model
require "ohm/expire"
include Ohm::Expire
TTL = 5
class Ohm < ::Ohm::Model
def self.use_settings settings
include ::Ohm::Expire
# TTL = 5
# attribute :mysqlid
settings.keys.each { |x|
attribute x
}
[:index, :unique, :expire].each do |x|
method(x).call settings[x]
end
# expire TTL
end
attribute Schema.keys
[:index, :unique].each do |x|
method(x).call Schema::Setting[x]
private
def son his_son # input like :Post
# collection :posts, :Post
collection(his_son.to_s.underscore.pluralize.to_sym, his_son) if SourceSchema.const_defined?(his_son)
end
def dad his_dad # input like :Post
# reference :post, :Post
reference(his_dad.to_s.underscore.to_sym, his_dad )if his_dad SourceSchema.const_defined?(his_son)
end
end
expire TTL
private
def son his_son # input like :Post
# collection :posts, :Post
collection(his_son.to_s.underscore.pluralize.to_sym, his_son) if SourceSchema.const_defined?(his_son)
end
def update_ohm
# Ohm.create()
end
end
def dad his_dad # input like :Post
# reference :post, :Post
reference(his_dad.to_s.underscore.to_sym, his_dad )if his_dad SourceSchema.const_defined?(his_son)
end
end
end
end
require "bundler/setup"
require "weather/model"
require "weather-model"
require 'rails'
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
......
require "spec_helper"
require 'active_record'
require "ohm"
RSpec.describe WeatherModel do
it "has a version number" do
expect(WeatherModel::VERSION).not_to be nil
end
include WeatherModel
it "does something useful" do
expect(false).to eq(true)
# start an ohm
Ohm.redis = Redic.new("redis://127.0.0.1:6379")
# run migration
Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file }
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'weather-model-test.sqlite3'
)
ActiveRecord::Migration[5.0].subclasses.each do |migrate|
migrate.migrate rescue nil
end
# before do
it "has a version number" do
expect(WeatherModel::VERSION).not_to be nil
end
# # Dir['db/migrate/*.rb'].each { |file| require '../' + file }
it 'Forecast' do
p Forecast::ShanghaiTen.all
# ActiveRecord::Base.establish_connection(
# :adapter => 'sqlite3',
# :database => 'weather-model-test.sqlite3'
# )
# CreateForecasts.migrate(:up)
# end
end
end
......@@ -42,4 +42,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency 'rspec-rails'
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment