From e6fc705e6a47a42bf9d48ada8549b0554ec54f55 Mon Sep 17 00:00:00 2001 From: Aart van Halteren Date: Wed, 26 Jun 2013 09:00:08 +0200 Subject: [PATCH] Initial implementation --- .ruby-gemset | 1 + .ruby-version | 1 + Gemfile | 6 ++++++ Gemfile.lock | 31 ++++++++++++++++++++++++++++ README.md | 36 +++++++++++++++++++++++++++++++++ Rakefile | 34 +++++++++++++++++++++++++++++++ app/models/page.rb | 3 +++ ar-no-rails.rb | 14 +++++++++++++ config/database.yml | 5 +++++ db/migrate/001_creates_pages.rb | 8 ++++++++ smartmeter.rb | 18 +++++++++++++++++ 11 files changed, 157 insertions(+) create mode 100644 .ruby-gemset create mode 100644 .ruby-version create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 README.md create mode 100644 Rakefile create mode 100644 app/models/page.rb create mode 100644 ar-no-rails.rb create mode 100644 config/database.yml create mode 100644 db/migrate/001_creates_pages.rb create mode 100644 smartmeter.rb diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 0000000..032c1c1 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +smartmeter diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..970977c --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-1.9.3-p125 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..8919c11 --- /dev/null +++ b/Gemfile @@ -0,0 +1,6 @@ +source "https://rubygems.org" + +gem "activerecord", "3.2.13" +gem "mysql2" +gem "serialport" +gem "state_pattern" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..35e0b20 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,31 @@ +GEM + remote: https://rubygems.org/ + specs: + activemodel (3.2.13) + activesupport (= 3.2.13) + builder (~> 3.0.0) + activerecord (3.2.13) + activemodel (= 3.2.13) + activesupport (= 3.2.13) + arel (~> 3.0.2) + tzinfo (~> 0.3.29) + activesupport (3.2.13) + i18n (= 0.6.1) + multi_json (~> 1.0) + arel (3.0.2) + builder (3.0.4) + i18n (0.6.1) + multi_json (1.7.7) + mysql2 (0.3.11) + serialport (1.1.0) + state_pattern (2.0.1) + tzinfo (0.3.37) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (= 3.2.13) + mysql2 + serialport + state_pattern diff --git a/README.md b/README.md new file mode 100644 index 0000000..6eab570 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +ActiveRecord Without Rails +========================== + +Just a simple example of using ActiveRecord migrations without Rails + +tasks you can do: + +* `rake db:create` +* `rake db:migrate` +* `rake db:drop` + +Or, you can run the thing to show that it'll connect + +``` +ruby ar-no-rails +``` + +Output: +> Count of Pages: 0 + +Lastly, you can IRB it to do stuff: + +$ irb + +``` +>> require "./ar-no-rails" +=> true +>> Page.new +=> # +>> Page.create content: "the-content" +=> # +``` + +Copyright +--------- +None. Really. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..04146e4 --- /dev/null +++ b/Rakefile @@ -0,0 +1,34 @@ +require "rubygems" +require "bundler/setup" + +require 'mysql2' +require 'active_record' +require 'yaml' + +namespace :db do + + desc "Migrate the db" + task :migrate do + connection_details = YAML::load(File.open('config/database.yml')) + ActiveRecord::Base.establish_connection(connection_details) + ActiveRecord::Migrator.migrate("db/migrate/") + end + + desc "Create the db" + task :create do + connection_details = YAML::load(File.open('config/database.yml')) + admin_connection = connection_details.merge({'database'=> 'mysql', + 'schema_search_path'=> 'public'}) + ActiveRecord::Base.establish_connection(admin_connection) + ActiveRecord::Base.connection.create_database(connection_details.fetch('database')) + end + + desc "drop the db" + task :drop do + connection_details = YAML::load(File.open('config/database.yml')) + admin_connection = connection_details.merge({'database'=> 'mysql', + 'schema_search_path'=> 'public'}) + ActiveRecord::Base.establish_connection(admin_connection) + ActiveRecord::Base.connection.drop_database(connection_details.fetch('database')) + end +end diff --git a/app/models/page.rb b/app/models/page.rb new file mode 100644 index 0000000..fd711fe --- /dev/null +++ b/app/models/page.rb @@ -0,0 +1,3 @@ +class Page < ActiveRecord::Base + +end diff --git a/ar-no-rails.rb b/ar-no-rails.rb new file mode 100644 index 0000000..3ff54ec --- /dev/null +++ b/ar-no-rails.rb @@ -0,0 +1,14 @@ +require "rubygems" +require "bundler/setup" +require "active_record" + +project_root = File.dirname(File.absolute_path(__FILE__)) +Dir.glob(project_root + "/app/models/*.rb").each{|f| require f} + +connection_details = YAML::load(File.open('config/database.yml')) +ActiveRecord::Base.establish_connection(connection_details) + + +if __FILE__ == $0 + puts "Count of Pages: #{Page.count}" +end diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 0000000..e2dbeb2 --- /dev/null +++ b/config/database.yml @@ -0,0 +1,5 @@ +host: 'localhost' +adapter: 'mysql2' +database: 'smartmeter' +username: 'root' +pool: 5 diff --git a/db/migrate/001_creates_pages.rb b/db/migrate/001_creates_pages.rb new file mode 100644 index 0000000..eed18e4 --- /dev/null +++ b/db/migrate/001_creates_pages.rb @@ -0,0 +1,8 @@ +class CreatesPages < ActiveRecord::Migration + def change + create_table :pages do |t| + t.text :content + t.boolean :published, default: false + end + end +end diff --git a/smartmeter.rb b/smartmeter.rb new file mode 100644 index 0000000..e815fb8 --- /dev/null +++ b/smartmeter.rb @@ -0,0 +1,18 @@ +require "rubygems" +require "bundler/setup" +require "active_record" +require "serialport" + +project_root = File.dirname(File.absolute_path(__FILE__)) +Dir.glob(project_root + "/app/models/*.rb").each{|f| require f} + +connection_details = YAML::load(File.open('config/database.yml')) +ActiveRecord::Base.establish_connection(connection_details) + +# Open connection to serial port +ser = SerialPort.new("/dev/ttyUSB1", 9600, 7, 1, SerialPort::EVEN) + +# read until newline +response = ser.readline("\r") +response.chomp! +print "#{response}\n"