active_hash_ext 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0b391b4def9555b2a7043e9130c4b2adb11510b
4
+ data.tar.gz: 26ebe7692c6e972ac5a8238214fc74b7e3fcbf63
5
+ SHA512:
6
+ metadata.gz: ba385e1f62f137d2564f24471dcf30cac926603560b4997fa04140d4af3c186e5de0bdc93ca46f34fe66e7a0381344e89ff1d5c3dc133653e64858548753e740
7
+ data.tar.gz: a94a166b81ae12e48a12a5706c33c74933f8d38043b1358cc3398879c2beae2a378aaef76ebf0f1adc03bd2692eb448bc80b2bb8f4177a353baf0fd2722255e2
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_hash_ext.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dustin Zeisler
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,76 @@
1
+ # ActiveHashExt
2
+ This is an extension module to [ActiveHash](https://github.com/zilkey/active_hash)
3
+
4
+ ActiveHashExt includes the ability to read a schema.rb
5
+ file from ActiveRecord and set those attributes on a class.
6
+ It also adds methods from ActiveRecord like update and destroy_all with more to come.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'active_hash_ext'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install active_hash_ext
21
+
22
+ ## Usage
23
+
24
+ describe 'ActiveHashExt' do
25
+
26
+ before do
27
+
28
+ class TestHash < ActiveHash::Base
29
+ include ActiveHashExt
30
+ fields :name, :age
31
+ end
32
+
33
+ TestHash.delete_all
34
+ end
35
+
36
+ it '#update' do
37
+
38
+ test_hash = TestHash.new
39
+ test_hash.update(age: 34, name: 'Sam')
40
+ expect(test_hash.age).to eq 34
41
+ expect(test_hash.name).to eq 'Sam'
42
+ end
43
+
44
+ it '#destroy_all' do
45
+ TestHash.create(name: 'Jane', age: 56)
46
+ expect(TestHash.count).to eq 1
47
+ TestHash.destroy_all
48
+ expect(TestHash.count).to eq 0
49
+ end
50
+
51
+ context 'read_schema' do
52
+
53
+ it 'returns attributes from schema file' do
54
+ expect(TestHash.read_schema('custodians', 'schema.rb')).to eq [:name, :age]
55
+ end
56
+
57
+ it 'when passed to fields it will set attributes on class from schema' do
58
+ class SchemaHash < ActiveHash::Base
59
+ include ActiveHashExt
60
+ fields *read_schema('custodians', File.new('schema.rb', 'r'))
61
+ end
62
+ SchemaHash.create(name: 'Fred')
63
+ expect(SchemaHash.first.name).to eq 'Fred'
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_hash_ext/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_hash_ext"
8
+ spec.version = ActiveHashExt::VERSION
9
+ spec.authors = ["Dustin Zeisler"]
10
+ spec.email = ["dustin@zive.me"]
11
+ spec.description = %q{This is an extension module to ActiveHash}
12
+ spec.summary = %q{ActiveHashExt includes the ability to read a schema.rb
13
+ file from ActiveRecord and set those attributes on a class.
14
+ It also adds methods from ActiveRecord like update and destroy_all with more to come.}
15
+ spec.homepage = "https://github.com/zeisler/active_hash_ext"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(spec|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency 'active_hash', '~>1.2.3'
27
+
28
+ end
@@ -0,0 +1,95 @@
1
+ require "active_hash_ext/version"
2
+
3
+ module ActiveHashExt
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def read_schema(table, path)
11
+ tables = File.read(path).split(/^\s*create_/)[1..-1].map {|table_data| Table.parse table_data }
12
+ table = tables.select do |_table|
13
+ _table.name == table
14
+ end
15
+ unless table.first.nil?
16
+ table.first.attributes.map do |attribute|
17
+ attribute.name.to_sym
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ def destroy_all
24
+ delete_all
25
+ end
26
+ end
27
+
28
+ def update(attributes)
29
+ self.attributes.merge(attributes).each do |attribute|
30
+ send("#{attribute.first}=",attribute.last)
31
+ end
32
+ end
33
+
34
+ def update_attributes(attributes)
35
+ update(attributes)
36
+ end
37
+
38
+
39
+
40
+ end
41
+
42
+
43
+
44
+ require 'active_support/inflector'
45
+
46
+ class Table
47
+
48
+ attr_reader :attributes, :name
49
+
50
+ def initialize(name, attributes)
51
+ @name, @attributes = name, attributes
52
+ end
53
+
54
+ def to_script(target)
55
+ return "rails generate #{target} #{modelize name} #{attributes.map(&:to_script).reject{|x| x.nil? || x.empty?}.join(' ')}" if target == "scaffold"
56
+ return "rails generate #{target} #{modelize name} #{attributes.map(&:to_script).reject{|x| x.nil? || x.empty?}.join(' ')}" if target == "factory_girl:model"
57
+ end
58
+
59
+ def self.parse(table_data)
60
+ return unless name = table_data[/table "(.+)"/]
61
+ name = $1
62
+ atts = table_data.lines.to_a.select {|line| line =~ /t\.\w+/ }.map {|att| Attribute.parse att }
63
+ Table.new(name, atts)
64
+ end
65
+
66
+ private
67
+ def modelize (string)
68
+ string.camelize.singularize
69
+ end
70
+
71
+ end
72
+
73
+
74
+ class Attribute
75
+
76
+ attr_reader :name, :type
77
+
78
+ def initialize(type, name)
79
+ @name, @type = name, type
80
+ end
81
+
82
+ def to_script
83
+ "#{name}:#{type}" unless ["created_at","updated_at"].include? name
84
+ end
85
+
86
+ def self.parse(attribute)
87
+ match = attribute.match(/t\.(\w+)\s+"(\w+)"/)
88
+ if match
89
+ Attribute.new(*match.captures)
90
+ end
91
+ end
92
+
93
+
94
+ end
95
+
@@ -0,0 +1,3 @@
1
+ module ActiveHashExt
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ ActiveRecord::Schema.define(version: 20131226214224) do
2
+
3
+ create_table "custodians", force: true do |t|
4
+ t.string "name"
5
+ t.datetime "created_at"
6
+ t.datetime "updated_at"
7
+ end
8
+
9
+ create_table "disclosure_deliveries", force: true do |t|
10
+ t.string "email_to"
11
+ t.string "email_cc"
12
+ t.string "email_bcc"
13
+ t.string "email_subject"
14
+ t.string "email_attachment"
15
+ t.text "email_body"
16
+ t.string "email_status", default: "New"
17
+ t.datetime "created_at"
18
+ t.datetime "updated_at"
19
+ t.integer "disclosure_id"
20
+ t.boolean "email_sent", default: false
21
+ end
22
+
23
+ end
@@ -0,0 +1,49 @@
1
+ require 'rspec'
2
+ require 'active_hash'
3
+ require_relative '../../lib/active_hash_ext'
4
+
5
+ describe 'ActiveHashExt' do
6
+
7
+ before do
8
+
9
+ class TestHash < ActiveHash::Base
10
+ include ActiveHashExt
11
+ fields :name, :age
12
+ end
13
+
14
+ TestHash.delete_all
15
+ end
16
+
17
+ it '#update' do
18
+
19
+ test_hash = TestHash.new
20
+ test_hash.update(age: 34, name: 'Sam')
21
+ expect(test_hash.age).to eq 34
22
+ expect(test_hash.name).to eq 'Sam'
23
+ end
24
+
25
+ it '#destroy_all' do
26
+ TestHash.create(name: 'Jane', age: 56)
27
+ expect(TestHash.count).to eq 1
28
+ TestHash.destroy_all
29
+ expect(TestHash.count).to eq 0
30
+ end
31
+
32
+ context 'read_schema' do
33
+
34
+ it 'returns attributes from schema file' do
35
+ expect(TestHash.read_schema('custodians', File.new('schema.rb', 'r'))).to eq [:name, :created_at, :updated_at]
36
+ end
37
+
38
+ it 'when passed to fields it will set attributes on class from schema' do
39
+ class SchemaHash < ActiveHash::Base
40
+ include ActiveHashExt
41
+ fields *read_schema('custodians', File.new('schema.rb', 'r'))
42
+ end
43
+ SchemaHash.create(name: 'Fred')
44
+ expect(SchemaHash.first.name).to eq 'Fred'
45
+ end
46
+
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_hash_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dustin Zeisler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: active_hash
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.3
69
+ description: This is an extension module to ActiveHash
70
+ email:
71
+ - dustin@zive.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - active_hash_ext.gemspec
82
+ - lib/active_hash_ext.rb
83
+ - lib/active_hash_ext/version.rb
84
+ - schema.rb
85
+ - spec/lib/active_hash_ext_spec.rb
86
+ homepage: https://github.com/zeisler/active_hash_ext
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.1.5
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: ActiveHashExt includes the ability to read a schema.rb file from ActiveRecord
110
+ and set those attributes on a class. It also adds methods from ActiveRecord like
111
+ update and destroy_all with more to come.
112
+ test_files:
113
+ - spec/lib/active_hash_ext_spec.rb
114
+ has_rdoc: