Skip to content

Commit ff95bee

Browse files
committed
Gem structure overhaul
1 parent 9e0a965 commit ff95bee

File tree

14 files changed

+373
-265
lines changed

14 files changed

+373
-265
lines changed

CODE_OF_CONDUCT.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, gender identity and expression, level of experience,
9+
nationality, personal appearance, race, religion, or sexual identity and
10+
orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at [email protected]. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at [http://contributor-covenant.org/version/1/4][version]
72+
73+
[homepage]: http://contributor-covenant.org
74+
[version]: http://contributor-covenant.org/version/1/4/

bin/console

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "activerecord/tablefree"
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require "irb"
14+
IRB.start(__FILE__)

bin/setup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

lib/activerecord-tablefree.rb

Lines changed: 2 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -1,235 +1,2 @@
1-
require 'cgi'
2-
require 'active_record'
3-
4-
require 'activerecord-tablefree/version'
5-
require 'activerecord-tablefree/cast_type'
6-
require 'activerecord-tablefree/schema_cache'
7-
require 'activerecord-tablefree/connection'
8-
require 'activerecord-tablefree/transaction'
9-
10-
module ActiveRecord
11-
# = ActiveRecord::Tablefree
12-
#
13-
# Allow classes to behave like ActiveRecord models, but without an associated
14-
# database table. A great way to capitalize on validations. Based on the
15-
# original post at http://www.railsweenie.com/forums/2/topics/724 (which seems
16-
# to have disappeared from the face of the earth).
17-
#
18-
# = Example usage
19-
#
20-
# class ContactMessage < ActiveRecord::Base
21-
#
22-
# has_no_table
23-
#
24-
# column :name, :string
25-
# column :email, :string
26-
# column :message, :string
27-
#
28-
# end
29-
#
30-
# msg = ContactMessage.new( params[:msg] )
31-
# if msg.valid?
32-
# ContactMessageSender.deliver_message( msg )
33-
# redirect_to :action => :sent
34-
# end
35-
#
36-
module Tablefree
37-
38-
class NoDatabase < StandardError; end
39-
class Unsupported < StandardError; end
40-
41-
def self.included(base) #:nodoc:
42-
base.send :extend, ActsMethods
43-
end
44-
45-
module ActsMethods #:nodoc:
46-
# A model that needs to be tablefree will call this method to indicate
47-
# it.
48-
def has_no_table(options = { database: :fail_fast })
49-
raise ArgumentError, "Invalid database option '#{options[:database]}'" unless %i[fail_fast pretend_success].member? options[:database]
50-
# keep our options handy
51-
class_attribute :tablefree_options
52-
self.tablefree_options = {
53-
database: options[:database],
54-
columns_hash: {}
55-
}
56-
57-
# extend
58-
extend ActiveRecord::Tablefree::SingletonMethods
59-
extend ActiveRecord::Tablefree::ClassMethods
60-
61-
# include
62-
include ActiveRecord::Tablefree::InstanceMethods
63-
64-
# setup columns
65-
include ActiveModel::AttributeAssignment
66-
include ActiveRecord::ModelSchema
67-
end
68-
69-
def tablefree?
70-
false
71-
end
72-
end
73-
74-
module SingletonMethods
75-
# Used internally by ActiveRecord 5. This is the special hook that makes everything else work.
76-
def load_schema!
77-
@columns_hash = tablefree_options[:columns_hash].except(*ignored_columns)
78-
@columns_hash.each do |name, column|
79-
define_attribute(
80-
name,
81-
connection.lookup_cast_type_from_column(column),
82-
default: column.default,
83-
user_provided_default: false
84-
)
85-
end
86-
end
87-
88-
# Register a new column.
89-
def column(name, sql_type = nil, default = nil, null = true)
90-
cast_type = "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize.new
91-
tablefree_options[:columns_hash][name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, sql_type.to_s, null)
92-
end
93-
94-
# Register a set of columns with the same SQL type
95-
def add_columns(sql_type, *args)
96-
args.each do |col|
97-
column col, sql_type
98-
end
99-
end
100-
101-
def destroy(*_args)
102-
case tablefree_options[:database]
103-
when :pretend_success
104-
new
105-
when :fail_fast
106-
raise NoDatabase, "Can't #destroy on Tablefree class"
107-
end
108-
end
109-
110-
def destroy_all(*_args)
111-
case tablefree_options[:database]
112-
when :pretend_success
113-
[]
114-
when :fail_fast
115-
raise NoDatabase, "Can't #destroy_all on Tablefree class"
116-
end
117-
end
118-
119-
case ActiveRecord::VERSION::MAJOR
120-
when 5
121-
def find_by_sql(*_args)
122-
case tablefree_options[:database]
123-
when :pretend_success
124-
[]
125-
when :fail_fast
126-
raise NoDatabase, "Can't #find_by_sql on Tablefree class"
127-
end
128-
end
129-
else
130-
raise Unsupported, 'Unsupported ActiveRecord version'
131-
end
132-
133-
def transaction
134-
# case tablefree_options[:database]
135-
# when :pretend_success
136-
@_current_transaction_records ||= []
137-
yield
138-
# when :fail_fast
139-
# raise NoDatabase.new("Can't #transaction on Tablefree class")
140-
# end
141-
end
142-
143-
def tablefree?
144-
true
145-
end
146-
147-
def table_exists?
148-
false
149-
end
150-
end
151-
152-
module ClassMethods
153-
def from_query_string(query_string)
154-
if query_string.blank?
155-
new
156-
else
157-
params = query_string.split('&').collect do |chunk|
158-
next if chunk.empty?
159-
key, value = chunk.split('=', 2)
160-
next if key.empty?
161-
value = value.nil? ? nil : CGI.unescape(value)
162-
[CGI.unescape(key), value]
163-
end.compact.to_h
164-
165-
new(params)
166-
end
167-
end
168-
169-
def connection
170-
@_connection ||= ActiveRecord::Tablefree::Connection.new
171-
end
172-
end
173-
174-
module InstanceMethods
175-
def to_query_string(prefix = nil)
176-
attributes.to_a.collect { |(name, value)| escaped_var_name(name, prefix) + '=' + escape_for_url(value) if value }.compact.join('&')
177-
end
178-
179-
def quote_value(_value, _column = nil)
180-
''
181-
end
182-
183-
%w[create create_record _create_record update update_record _update_record].each do |method_name|
184-
define_method(method_name) do |*_args|
185-
case self.class.tablefree_options[:database]
186-
when :pretend_success
187-
true
188-
when :fail_fast
189-
raise NoDatabase, "Can't ##{method_name} a Tablefree object"
190-
end
191-
end
192-
end
193-
194-
def destroy
195-
case self.class.tablefree_options[:database]
196-
when :pretend_success
197-
@destroyed = true
198-
freeze
199-
when :fail_fast
200-
raise NoDatabase, "Can't #destroy a Tablefree object"
201-
end
202-
end
203-
204-
def reload(*_args)
205-
case self.class.tablefree_options[:database]
206-
when :pretend_success
207-
self
208-
when :fail_fast
209-
raise NoDatabase, "Can't #reload a Tablefree object"
210-
end
211-
end
212-
213-
def add_to_transaction; end
214-
215-
private
216-
217-
def escaped_var_name(name, prefix = nil)
218-
prefix ? "#{CGI.escape(prefix)}[#{CGI.escape(name)}]" : CGI.escape(name)
219-
end
220-
221-
def escape_for_url(value)
222-
case value
223-
when true then '1'
224-
when false then '0'
225-
when nil then ''
226-
else CGI.escape(value.to_s)
227-
end
228-
rescue
229-
''
230-
end
231-
end
232-
end
233-
end
234-
235-
ActiveRecord::Base.send(:include, ActiveRecord::Tablefree)
1+
# legacy automatic require support
2+
require 'activerecord/tablefree'

lib/activerecord-tablefree/version.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)