Commit 95905da0 by ivan Lan

Add basic frame

parent b172bf70
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"
class CreateForecasts < ActiveRecord::Migration[5.0]
def change
create_table :forecasts do |t|
t.datetime :datetime
t.string :speed
t.integer :temp_low
t.integer :temp_high
t.string :weathertext
t.string :weatherpic
t.string :cityname
t.string :serialnumber
t.string :win_dir
t.string :win_speed
t.timestamps
end
# 两个index 一起查询会不会导致其中一个作用几乎消失
# 需不需要按城市来分表
add_index :forecasts, :cityname
add_index :forecasts, :datetime
end
end
# require 'rails/generators/active_record'
# module ActiveRecord
# module Generators
# class WeatherModelGenerator < ActiveRecord::Generators::Base
# # argument :attributes, type: :array, default: [], banner: "field:type field:type"
# # include Devise::Generators::OrmHelpers
# source_root File.expand_path("../templates", __FILE__)
# def copy_devise_migration
# if (behavior == :invoke && model_exists?) || (behavior == :revoke && migration_exists?(table_name))
# migration_template "migration_existing.rb", "db/migrate/update_weather_model.rb", migration_version: migration_version
# else
# migration_template "migration.rb", "db/migrate/create_weather_model.rb", migration_version: migration_version
# end
# end
# def generate_model
# invoke "active_record:model", [name], migration: false unless model_exists? && behavior == :invoke
# end
# def inject_devise_content
# content = model_contents
# class_path = if namespaced?
# class_name.to_s.split("::")
# else
# [class_name]
# end
# indent_depth = class_path.size - 1
# content = content.split("\n").map { |line| " " * indent_depth + line } .join("\n") << "\n"
# inject_into_class(model_path, class_path.last, content) if model_exists?
# end
# def migration_data
# <<RUBY
# ## Database authenticatable
# t.string :email, null: false, default: ""
# t.string :encrypted_password, null: false, default: ""
# ## Recoverable
# t.string :reset_password_token
# t.datetime :reset_password_sent_at
# ## Rememberable
# t.datetime :remember_created_at
# ## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.#{ip_column} :current_sign_in_ip
# t.#{ip_column} :last_sign_in_ip
# ## Confirmable
# # t.string :confirmation_token
# # t.datetime :confirmed_at
# # t.datetime :confirmation_sent_at
# # t.string :unconfirmed_email # Only if using reconfirmable
# ## Lockable
# # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# # t.string :unlock_token # Only if unlock strategy is :email or :both
# # t.datetime :locked_at
# RUBY
# end
# def ip_column
# # Padded with spaces so it aligns nicely with the rest of the columns.
# "%-8s" % (inet? ? "inet" : "string")
# end
# def inet?
# postgresql?
# end
# def rails5?
# Rails.version.start_with? '5'
# end
# def postgresql?
# config = ActiveRecord::Base.configurations[Rails.env]
# config && config['adapter'] == 'postgresql'
# end
# def migration_version
# if rails5?
# "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
# end
# end
# end
# end
# end
# class WeatherModelCreate < ActiveRecord::Migration[5.0]
# def change
# create_table :<%= table_name %> do |t|
# <%= migration_data -%>
# <% attributes.each do |attribute| -%>
# t.<%= attribute.type %> :<%= attribute.name %>
# <% end -%>
# t.timestamps null: false
# end
# # add_index :<%= table_name %>, :email, unique: true
# # add_index :<%= table_name %>, :reset_password_token, unique: true
# # add_index :<%= table_name %>, :confirmation_token, unique: true
# # add_index :<%= table_name %>, :unlock_token, unique: true
# end
# end
\ No newline at end of file
# class AddDeviseTo<%= table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
# def self.up
# change_table :<%= table_name %> do |t|
# <%= migration_data -%>
# <% attributes.each do |attribute| -%>
# t.<%= attribute.type %> :<%= attribute.name %>
# <% end -%>
# # Uncomment below if timestamps were not included in your original model.
# # t.timestamps null: false
# end
# add_index :<%= table_name %>, :email, unique: true
# add_index :<%= table_name %>, :reset_password_token, unique: true
# # add_index :<%= table_name %>, :confirmation_token, unique: true
# # add_index :<%= table_name %>, :unlock_token, unique: true
# end
# def self.down
# # By default, we don't want to make any assumption about how to roll back a migration when your
# # model already existed. Please edit below which fields you would like to remove in this migration.
# raise ActiveRecord::IrreversibleMigration
# end
# end
\ No newline at end of file
require "weather/model/version"
require 'weather/model/version'
module Weather
module Model
# Your code goes here...
require 'source_schema'
def 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
module Weather
module Model
module SourceSchema
ShanghaiTen = {
table_name: :forecasts,
schema: {
datetime: :datetime,
cityname: :string,
temp_high: :string,
temp_low: :string,
weather_text: :string,
weather_pic: :string,
win_dir: :string,
win_speed: :string,
},
options: {
index: [:cityname],
father: nil,
son: nil
}
}
end
end
end
module Weather
module Model
module Storer
class Mysql < ActiveRecord::Base
@@source = self.name[0..-8]
@@settings = Source::Schema.const_get(self.name[0..-8])
self.table_name = @@settings[:table_name]
end
class Ohm < Ohm::Model
require "ohm/expire"
include Ohm::Expire
TTL = 5
attribute Schema.keys
[:index, :unique].each do |x|
method(x).call Schema::Setting[x]
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 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
end
......@@ -6,18 +6,18 @@ require 'weather/model/version'
Gem::Specification.new do |spec|
spec.name = "weather-model"
spec.version = Weather::Model::VERSION
spec.authors = ["liyijie"]
spec.email = ["liyijie825@gmail.com"]
spec.authors = ["Ivan Lan"]
spec.email = ["mumumumushu@gmail.com"]
spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.summary = %q{Weather Model}
spec.description = %q{Weather Model}
spec.homepage = "https://git.tallty.com/weather/weather-model"
spec.license = "MIT"
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
spec.metadata['allowed_push_host'] = "https://git.tallty.com/weather/weather-model"
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
......@@ -30,6 +30,15 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
# Object-Hash Mapping for Redis
# https://github.com/soveran/ohm
spec.add_dependency "ohm"
# Ohm plugin that exposes ttl control over modules
# https://github.com/joseairosa/ohm-expire
spec.add_dependency "ohm-expire"
spec.add_dependency "activerecord"
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
......
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