Commit c1e38114 by ivan Lan

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

parent 888f221b
require 'weather-model/version' require 'weather-model/version'
module WeatherModel module WeatherModel
require 'weather-model/source_schema'
def self.create_classes Dir[File.dirname(__FILE__) + '/weather-model/*.rb'].each { |file| require file }
Weather::Model::SourceSchema.each do |source|
Object.const_set("#{source}::Mysql", Class.new(Source::Mysql)) def self.included base
Object.const_set("#{source}::Ohm", Class.new(Source::Ohm)) 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 end
end end
module WeatherModel module WeatherModel
class Engine < Rails::Engine require 'rails/engine'
class Engine < ::Rails::Engine
end end
end end
\ No newline at end of file
module WeatherModel module WeatherModel
module SourceSchema module SourceSchema
def self.sorts
['Forecast']
end
ShanghaiTen = { ShanghaiTen = {
sort: 'Forecast',
table_name: :forecasts, table_name: :forecasts,
schema: { schema: {
datetime: :datetime, datetime: :datetime,
......
module WeatherModel module WeatherModel
module Storer module Storer
class Mysql < ActiveRecord::Base require 'active_record'
@@source = self.name[0..-8] require 'ohm'
@@settings = Source::Schema.const_get(self.name[0..-8])
self.table_name = @@settings[:table_name]
end
class Ohm < Ohm::Model
require "ohm/expire" require "ohm/expire"
include Ohm::Expire
TTL = 5
attribute Schema.keys class Mysql < ::ActiveRecord::Base
[:index, :unique].each do |x| after_save :update_ohm
method(x).call Schema::Setting[x] def self.use_settings settings
self.table_name = settings[:table_name]
Ohm.use_settings settings
end end
expire TTL 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
private private
def son his_son # input like :Post def son his_son # input like :Post
...@@ -31,5 +36,13 @@ module WeatherModel ...@@ -31,5 +36,13 @@ module WeatherModel
reference(his_dad.to_s.underscore.to_sym, his_dad )if his_dad SourceSchema.const_defined?(his_son) reference(his_dad.to_s.underscore.to_sym, his_dad )if his_dad SourceSchema.const_defined?(his_son)
end end
end end
private
def update_ohm
# Ohm.create()
end
end
end end
end end
require "bundler/setup" require "bundler/setup"
require "weather/model" require "weather-model"
require 'rails'
RSpec.configure do |config| RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure # Enable flags like --only-failures and --next-failure
......
require "spec_helper" require "spec_helper"
require 'active_record' require 'active_record'
require "ohm"
RSpec.describe WeatherModel do RSpec.describe WeatherModel do
it "has a version number" do include WeatherModel
expect(WeatherModel::VERSION).not_to be nil
end
it "does something useful" do # start an ohm
expect(false).to eq(true) 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 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( end
# :adapter => 'sqlite3',
# :database => 'weather-model-test.sqlite3'
# )
# CreateForecasts.migrate(:up)
# end
end end
...@@ -42,4 +42,6 @@ Gem::Specification.new do |spec| ...@@ -42,4 +42,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.14" spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0" spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency 'rspec-rails'
end 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