Initial implementation
This commit is contained in:
1
.ruby-gemset
Normal file
1
.ruby-gemset
Normal file
@@ -0,0 +1 @@
|
|||||||
|
smartmeter
|
||||||
1
.ruby-version
Normal file
1
.ruby-version
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ruby-1.9.3-p125
|
||||||
6
Gemfile
Normal file
6
Gemfile
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "activerecord", "3.2.13"
|
||||||
|
gem "mysql2"
|
||||||
|
gem "serialport"
|
||||||
|
gem "state_pattern"
|
||||||
31
Gemfile.lock
Normal file
31
Gemfile.lock
Normal file
@@ -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
|
||||||
36
README.md
Normal file
36
README.md
Normal file
@@ -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 id: nil, content: nil, published: false>
|
||||||
|
>> Page.create content: "the-content"
|
||||||
|
=> #<Page id: 1, content: "the-content", published: false>
|
||||||
|
```
|
||||||
|
|
||||||
|
Copyright
|
||||||
|
---------
|
||||||
|
None. Really.
|
||||||
34
Rakefile
Normal file
34
Rakefile
Normal file
@@ -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
|
||||||
3
app/models/page.rb
Normal file
3
app/models/page.rb
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
class Page < ActiveRecord::Base
|
||||||
|
|
||||||
|
end
|
||||||
14
ar-no-rails.rb
Normal file
14
ar-no-rails.rb
Normal file
@@ -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
|
||||||
5
config/database.yml
Normal file
5
config/database.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
host: 'localhost'
|
||||||
|
adapter: 'mysql2'
|
||||||
|
database: 'smartmeter'
|
||||||
|
username: 'root'
|
||||||
|
pool: 5
|
||||||
8
db/migrate/001_creates_pages.rb
Normal file
8
db/migrate/001_creates_pages.rb
Normal file
@@ -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
|
||||||
18
smartmeter.rb
Normal file
18
smartmeter.rb
Normal file
@@ -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"
|
||||||
Reference in New Issue
Block a user