wepo 0.1.0

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: 16e3a62c965dd473d4a5d9fb1ef02d0c06bb4e70
4
+ data.tar.gz: 231adaf9557a843cdeb23117d51deec42df89936
5
+ SHA512:
6
+ metadata.gz: da7d46f549ebc582bc419b95c3fb964842414c207e26e39e2e25df13b8e1d1c40922d43f04b930a66e6e3ed2fe1478badb11d197791ac50ad8d1ddd184113335
7
+ data.tar.gz: 188cf7657f40663bda556d165db971feda91410be34955e29cf593421ab03146ed5910fa393322f7e93cb31218cd5c61e732d8e32678c5a903b8494981f7fea8
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wepo.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # Wepo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/wepo`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'wepo'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install wepo
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/wepo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wepo"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ require "wepo/version"
2
+ require 'wepo/repo'
3
+ require 'wepo/mapper'
4
+ require 'wepo/adapters/active_record'
5
+
6
+ module Wepo
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,38 @@
1
+ module Wepo
2
+ module Adapters
3
+ class ActiveRecord
4
+
5
+ def all(model_class)
6
+ model_class.where(deleted_at: nil).all
7
+ end
8
+
9
+ def find(model_class, id)
10
+ model_class.find(id)
11
+ end
12
+
13
+ def find_or_initialize_by(model_class, params)
14
+ model_class.find_or_initialize_by(params)
15
+ end
16
+
17
+ def where(model_class, params)
18
+ model_class.where(params)
19
+ end
20
+
21
+ def create(model)
22
+ model.save
23
+ model
24
+ end
25
+
26
+ def update(model)
27
+ model.save
28
+ model
29
+ end
30
+
31
+ def delete(model)
32
+ model.deleted_at = Time.now
33
+ model.save
34
+ model
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,47 @@
1
+ module Wepo
2
+ class Mapper
3
+
4
+ def initialize(entity_class:, model_class:)
5
+ @entity_class = entity_class
6
+ @model_class = model_class
7
+ end
8
+
9
+ def map_model(model)
10
+ entity = @entity_class.new
11
+ entity.attributes.keys.each do |name|
12
+ value = model.send(name)
13
+ if value.respond_to? :each
14
+ values = value.map do |v|
15
+ entity_class = (v.class.name + "Entity").constantize
16
+ mapper = Mapper.new(entity_class: entity_class, model_class: v.class)
17
+ mapper.map_model(v)
18
+ end
19
+ entity.send(setter(name), values)
20
+ else
21
+ entity.send(setter(name), value)
22
+ end
23
+ end
24
+ entity
25
+ end
26
+
27
+ def map_entity(entity)
28
+ model = @model_class.find_or_initialize_by(id: entity.id)
29
+ model.attributes.keys.each do |name|
30
+ if name.include?('_id')
31
+ association = name.gsub(/_id/,'').to_sym
32
+ entity_association = entity.send(association)
33
+ model.send(setter(name), entity.send(association).id) if entity_association
34
+ else
35
+ model.send(setter(name), entity.send(name)) if entity.respond_to?(name)
36
+ end
37
+ end
38
+ model
39
+ end
40
+
41
+ private
42
+
43
+ def setter(attr)
44
+ "#{attr}=".to_sym
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,110 @@
1
+ require 'uber/inheritable_attr'
2
+
3
+ module Wepo
4
+ # Repo base class
5
+ #
6
+ # @version 0.1.0
7
+ # @since 0.1.0
8
+ # @author Josh Deeden <jdeeden@gmail.com>
9
+ module Repo
10
+
11
+
12
+ attr_reader :adapter
13
+
14
+ def initialize(adapter)
15
+ @adapter = adapter
16
+ end
17
+
18
+ def self.included(base)
19
+ base.instance_eval do
20
+ extend Uber::InheritableAttr
21
+ inheritable_attr :model_class
22
+ inheritable_attr :entity_class
23
+
24
+ self.model_class = nil
25
+ self.entity_class = nil
26
+
27
+ def model(*args)
28
+ if args[0]
29
+ self.model_class = args[0]
30
+ else
31
+ self.model_class
32
+ end
33
+ end
34
+
35
+ def entity(*args)
36
+ if args[0]
37
+ self.entity_class = args[0]
38
+ else
39
+ self.entity_class
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def all
46
+ adapter.all(model_class).map(&method(:map_model))
47
+ end
48
+
49
+ def find(id)
50
+ begin
51
+ adapter.find(model_class, id)
52
+ rescue
53
+ raise EntityNotFound, "Could not find #{model_class} with id: #{id}"
54
+ end
55
+ end
56
+
57
+ def find_or_initialize_by(params)
58
+ map_model adapter.find_or_initialize_by(model_class, params)
59
+ end
60
+
61
+ def where(params)
62
+ adapter.where(model_class, params).map(&method(:map_model))
63
+ end
64
+
65
+ def create(entity)
66
+ raise ArgumentError, 'entity cannot be nil' if entity.nil?
67
+ map_model adapter.create(map_entity(entity))
68
+ end
69
+
70
+ def update(entity)
71
+ raise ArgumentError, 'entity cannot be nil' if entity.nil?
72
+ map_model adapter.update(map_entity(entity))
73
+ end
74
+
75
+ def delete(entity)
76
+ raise ArgumentError, 'entity cannot be nil' if entity.nil?
77
+ map_model adapter.delete(map_entity(entity))
78
+ end
79
+
80
+ def save(entity)
81
+ entity.id.nil? ? create(entity) : update(entity)
82
+ end
83
+
84
+
85
+ private
86
+
87
+ def mapper
88
+ @mapper ||= Mapper.new(model_class: model_class, entity_class: entity_class)
89
+ end
90
+
91
+ def model_class
92
+ self.class.model_class
93
+ end
94
+
95
+ def entity_class
96
+ self.class.entity_class
97
+ end
98
+
99
+ def map_entity(entity)
100
+ mapper.map_entity(entity)
101
+ end
102
+
103
+ def map_model(model)
104
+ mapper.map_model(model)
105
+ end
106
+
107
+ end
108
+ class EntityNotFound < StandardError
109
+ end
110
+ end
@@ -0,0 +1,3 @@
1
+ module Wepo
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wepo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wepo"
8
+ spec.version = Wepo::VERSION
9
+ spec.authors = ["Josh Deeden"]
10
+ spec.email = ["jdeeden@gmail.com"]
11
+
12
+ spec.summary = %q{Repository abstraction}
13
+ spec.description = %q{Repository abstraction}
14
+ spec.homepage = "http://github.com/gangster/wepo"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ # end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.10"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec"
32
+ spec.add_development_dependency "rspec-collection_matchers"
33
+ spec.add_development_dependency "pry"
34
+ spec.add_development_dependency "activerecord"
35
+ spec.add_development_dependency "sqlite3"
36
+ spec.add_development_dependency "virtus"
37
+ spec.add_development_dependency "uber"
38
+
39
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wepo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Deeden
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-05 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: virtus
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: uber
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Repository abstraction
140
+ email:
141
+ - jdeeden@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - CODE_OF_CONDUCT.md
150
+ - Gemfile
151
+ - README.md
152
+ - Rakefile
153
+ - bin/console
154
+ - bin/setup
155
+ - lib/wepo.rb
156
+ - lib/wepo/adapters/active_record.rb
157
+ - lib/wepo/mapper.rb
158
+ - lib/wepo/repo.rb
159
+ - lib/wepo/version.rb
160
+ - wepo.gemspec
161
+ homepage: http://github.com/gangster/wepo
162
+ licenses: []
163
+ metadata: {}
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ requirements: []
179
+ rubyforge_project:
180
+ rubygems_version: 2.4.8
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Repository abstraction
184
+ test_files: []
185
+ has_rdoc: