Commit 888f221b by ivan Lan

User WeatherModel instead of Weather::Model

parent ec609d63
module SourceSchema
ShanghaiTen = {
schema: {
date: :date,
cityname: :string,
temp_high: :string,
weather_text: :string,
},
options: {
index: [:cityname],
dad: nil,
son: nil
}
}
end
# ===> there need to be
# 先 定义类完成
# 再 检查对应数据表 并 创建 或 完善
Source::Mysql.subclasses.each do |source|
unless source.table_exists?
# create table
else
# update table
end
end
def self.check_table
# create_table
# 增减量
# attrs = self.attributes
# names_now = attrs.map(&:name)
# names_last = Schema.keys
# incr_column = ( names_now | names_last ) - names_last
# del_column = ( names_now | names_last ) - names_now
end
# ShanghaiTen
# create_table :deck_study_sessions do |t|
# t.integer :deck_id
# t.integer :study_session_id
# end
# class CreateDeckStudySessionsTable < ActiveRecord::Migration
# def up
# puts 'ran up method'
# end
# def down
# drop_table :deck_study_sessions
# puts 'ran down method'
# end
# end
# =====
# $ rake create migration
require "rails/generators/actions/create_migration"
migration_exists?('db/migrate/', 'create_sources')
migration_template local_file, "db/migrate/create_sources.rb"
require 'weather/model/version' require 'weather-model/version'
module Weather
module Model
require '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))
end
end
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))
end
end end
end end
module Weather module WeatherModel
module Model class Engine < Rails::Engine
class Engine < Rails::Engine
end
end end
end end
\ No newline at end of file
module Weather module WeatherModel
module Model module SourceSchema
module SourceSchema ShanghaiTen = {
ShanghaiTen = { table_name: :forecasts,
table_name: :forecasts, schema: {
schema: { datetime: :datetime,
datetime: :datetime, cityname: :string,
cityname: :string, temp_high: :string,
temp_high: :string, temp_low: :string,
temp_low: :string, weather_text: :string,
weather_text: :string, weather_pic: :string,
weather_pic: :string, win_dir: :string,
win_dir: :string, win_speed: :string,
win_speed: :string, },
}, options: {
options: { index: [:cityname],
index: [:cityname], father: nil,
father: nil, son: nil
son: nil }
} }
} end
end
end
end end
module Weather module WeatherModel
module Model module Storer
module Storer class Mysql < ActiveRecord::Base
class Mysql < ActiveRecord::Base @@source = self.name[0..-8]
@@source = self.name[0..-8]
@@settings = Source::Schema.const_get(self.name[0..-8]) @@settings = Source::Schema.const_get(self.name[0..-8])
self.table_name = @@settings[:table_name] self.table_name = @@settings[:table_name]
end end
class Ohm < Ohm::Model class Ohm < Ohm::Model
require "ohm/expire" require "ohm/expire"
include Ohm::Expire include Ohm::Expire
TTL = 5 TTL = 5
attribute Schema.keys attribute Schema.keys
[:index, :unique].each do |x| [:index, :unique].each do |x|
method(x).call Schema::Setting[x] method(x).call Schema::Setting[x]
end end
expire TTL expire TTL
private private
def son his_son # input like :Post def son his_son # input like :Post
# collection :posts, :Post # collection :posts, :Post
collection(his_son.to_s.underscore.pluralize.to_sym, his_son) if SourceSchema.const_defined?(his_son) collection(his_son.to_s.underscore.pluralize.to_sym, his_son) if SourceSchema.const_defined?(his_son)
end end
def dad his_dad # input like :Post def dad his_dad # input like :Post
# reference :post, :Post # reference :post, :Post
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
end
end end
end end
module Weather module WeatherModel
module Model VERSION = "0.1.0"
VERSION = "0.1.0"
end
end end
require "spec_helper" require "spec_helper"
require 'active_record'
RSpec.describe Weather::Model do RSpec.describe WeatherModel do
it "has a version number" do it "has a version number" do
expect(Weather::Model::VERSION).not_to be nil expect(WeatherModel::VERSION).not_to be nil
end end
it "does something useful" do it "does something useful" do
expect(false).to eq(true) expect(false).to eq(true)
end end
# before do
# # Dir['db/migrate/*.rb'].each { |file| require '../' + file }
# ActiveRecord::Base.establish_connection(
# :adapter => 'sqlite3',
# :database => 'weather-model-test.sqlite3'
# )
# CreateForecasts.migrate(:up)
# end
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