ars-familiar 0.1.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf7740b326c13d105941cbcd87e85993aa38eb4b
4
+ data.tar.gz: 69f80e976950ab531dbe8efc2beaef70f2065e9d
5
+ SHA512:
6
+ metadata.gz: 426c996b6ff9082e06e021329455f3aa5fd3ae994b8919c9c6cb33ab9a6987b981348fb86d0ed1c9003c3e8bafb5a127b813b04e48c762dfbe99fb92e725845a
7
+ data.tar.gz: 73ef33c480791ccd1cff810da5b0b9ece921f98b41efe73a6215ffd64729bb6924bd3818679e8ae9bb642476f438ccd3e1b928712ddf9f693b2fd294acb5759f
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /Gemfile.local
11
+ /.rvmrc
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
15
+
16
+ # Emacs
17
+ *~
18
+ \#*\#
19
+ .\#*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.5
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ars-familiar.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 cunningfolk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Michael Lee Vazquez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Ars::Familiar
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/ars/familiar`. 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 'ars-familiar'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ars-familiar
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 spec` 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/Michael Lee Vazquez/ars-familiar.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -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,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ars/familiar/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ars-familiar"
8
+ spec.version = Ars::Familiar::VERSION
9
+ spec.authors = ["Michael Lee Vazquez"]
10
+ spec.email = ["magnus.nothus@gmail.com"]
11
+
12
+ spec.summary = %q{ars-familiar-#{Ars::Familiar::VERSION}}
13
+ spec.description = %q{Ruby Object Metadata Reflector}
14
+ spec.homepage = "http://github.com/cunningfolk/ars-familiar"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ %w[].each do |lib_name|
25
+ if Ars::Familiar::VERSION =~ /[a-zA-Z]+/
26
+ spec.add_runtime_dependency "ars-#{lib_name}", "= #{Ars::Familiar::VERSION}"
27
+ else
28
+ spec.add_runtime_dependency "ars-#{lib_name}", "~> #{Ars::Familiar::VERSION.sub(/^((?:\d+\.){2}).*$/, '\1.0')}"
29
+ end
30
+ end
31
+
32
+ spec.add_runtime_dependency 'activesupport'
33
+
34
+ spec.add_development_dependency 'rspec'
35
+ spec.add_development_dependency 'aruba'
36
+ spec.add_development_dependency 'rake'
37
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ars/familiar"
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__)
data/bin/setup ADDED
@@ -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
@@ -0,0 +1,58 @@
1
+ module Ars
2
+ module Familiar
3
+ class Base
4
+
5
+ def scribe(dump)
6
+ if dump
7
+ %i{ attributes_to_define relationships_to_define primary_key id }.each do |attr|
8
+ const_set "@#{attr}", dump[attr]
9
+ end
10
+ end
11
+ {
12
+ attributes_to_define: @attributes_to_define,
13
+ relationships_to_define: @relationships_to_define,
14
+ primary_key: @primary_key,
15
+ id: @id
16
+ }
17
+ end
18
+
19
+ def id
20
+ @id || id!
21
+ end
22
+
23
+ def attributes_to_define
24
+ @attributes_to_define || attributes_to_define!
25
+ end
26
+
27
+ def relationships_to_define(**opts)
28
+ @relationships_to_define || relationships_to_define!(**opts)
29
+ end
30
+
31
+ def extract_options(**opts)
32
+ class_map = map_classes(opts)
33
+
34
+ only = opts.fetch(:only, :pass).entries.each_with_object(class_map) {|val, hash| hash.store(*val) }.keys
35
+ class_map
36
+
37
+ filter = proc do |relation|
38
+ name = relation[:name]
39
+
40
+ only == :pass || only.include?(name)
41
+ end
42
+
43
+ [filter, class_map]
44
+ end
45
+
46
+ def map_classes(**opts)
47
+ class_base = opts.fetch(:class_base, nil)
48
+ class_name = opts.fetch(:class_name, false)
49
+ class_names = opts.fetch(:class_names, {})
50
+
51
+ Hash.new {|hash, key| hash[key] = [class_base, key].constantize }.
52
+ tap {|this| class_name && this.default = class_name}.
53
+ merge(class_names)
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ class ConstantizableArray < Array
2
+ ConstantizableArray::EMPTY_PARAM = "EMPTY_PARM".freeze
3
+ def constantize(wedge = EMPTY_PARAM)
4
+ wedge == EMPTY_PARAM && wedge = '::'
5
+ self.compact.join(wedge).constantize
6
+ end
7
+ end
@@ -0,0 +1,78 @@
1
+ module Ars
2
+ module Familiar
3
+ class Mongoid < Base
4
+
5
+ attr_reader :model, :model_name, :class_map
6
+
7
+ def [](*args)
8
+ ConstantizableArray[args]
9
+ end
10
+
11
+ def initialize(model)
12
+ @model_name = model
13
+ if Object.const_defined? model.to_s, true
14
+ @model = model.constantize
15
+ end
16
+ end
17
+
18
+ def primary_key!
19
+ @primary_key =
20
+ case
21
+ when model.respond_to?(:primary_key)
22
+ model.primary_key
23
+ when model.respond_to?(:guid), model.public_method_defined?(:guid)
24
+ :guid
25
+ end
26
+ end
27
+
28
+ def primary_key
29
+ @primary_key || primary_key!
30
+ end
31
+
32
+ def primary_key?
33
+ !!primary_key
34
+ end
35
+
36
+ def id!
37
+ @id = primary_key? ? primary_key : :id
38
+ end
39
+
40
+ def attributes_to_define!
41
+ @attributes_to_define = standard_fields.keys.map(&:to_sym)
42
+ end
43
+
44
+ def standard_fields
45
+ model.fields.select{|name, field| field.class.name == 'Mongoid::Fields::Standard'}.
46
+ tap{|fields| fields[id] = fields.delete("_id") }
47
+ end
48
+
49
+ def relationships_to_define!(**opts)
50
+ filter, class_map = extract_options(opts)
51
+
52
+ @relationships_to_define =
53
+ model.relations.values.select(&filter).flat_map { |relation|
54
+ name = relation.fetch(:name)
55
+
56
+ [*relation_switch(relation.fetch(:relation))].
57
+ map{|rel| [rel, name, class_name: class_map[name]] }.
58
+ compact
59
+ }.compact
60
+ end
61
+
62
+ def relation_switch(name)
63
+ case name.name
64
+ when 'Mongoid::Relations::Embedded::Many', 'Mongoid::Relations::Referenced::Many'
65
+ :has_many
66
+ when 'Mongoid::Relations::Embedded::One', 'Mongoid::Relations::Referenced::One'
67
+ :has_one
68
+ when 'Mongoid::Relations::Referenced::ManyToMany'
69
+ return :has_many, :belongs_to
70
+ else
71
+ raise StandardError, "Unknown Relationship: #{name}"
72
+ end
73
+ end
74
+
75
+ end
76
+ end
77
+ end
78
+
@@ -0,0 +1,86 @@
1
+ require 'active_support/inflector'
2
+ require 'fileutils'
3
+
4
+ module Ars
5
+ module Familiar
6
+ module Scribe
7
+
8
+ class Entry < String
9
+ def initialize(string)
10
+ super(string.to_s)
11
+ end
12
+
13
+ def to_filename
14
+ string = self.dup
15
+ string.gsub!(/::/, '/')
16
+ string.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
17
+ string.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
18
+ string.tr!("-", "_")
19
+ string.downcase!
20
+ string << '.dump'
21
+ string
22
+ end
23
+
24
+ def to_class
25
+ self.respond_to?(:name) ? self : self.constantize
26
+ end
27
+ end
28
+
29
+ module Scribable
30
+ def scribable(bool = nil)
31
+ !bool.nil? && @scribable = bool
32
+ end
33
+ def scribable?
34
+ @scribable
35
+ end
36
+ def self.extended(subclass)
37
+ subclass.scribable true
38
+ Scribe.register subclass
39
+ end
40
+ end
41
+
42
+ @register = []
43
+
44
+ def self.register(class_name = nil)
45
+ return @register # bypass
46
+ return @register unless class_name
47
+ return @register if @register.member?(class_name)
48
+ return @register unless class_name.builder.respond_to? :scribe
49
+ @register << Entry.new(class_name)
50
+ @register
51
+ end
52
+
53
+ def self.commit
54
+ register.each do |entry|
55
+ dump = Marshal.dump(entry.to_class.scribe)
56
+
57
+ filename = File.join(Ars::Medium.configuration.root, 'lib/portable_models', model.to_filename)
58
+ dirname = File.dirname filename
59
+ FileUtils.mkdir_p dirname
60
+ File.open(filename, 'w') do |file|
61
+ file << dump
62
+ end
63
+
64
+ end
65
+ end
66
+
67
+ def self.read_for(model)
68
+ model = Entry.new(model.class.name)
69
+
70
+ filename = File.join(Ars::Medium.configuration.root, 'lib/portable_models', model.to_filename)
71
+ dirname = File.dirname filename
72
+ FileUtils.mkdir_p dirname
73
+ if File.exist? filename
74
+ File.open(filename, 'r') do |file|
75
+ model.scribe Marshal.load(file)
76
+ end
77
+ end
78
+ end
79
+
80
+ def self.included(subclass)
81
+ subclass.extend Scribable
82
+ end
83
+
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ module Ars
2
+ module Familiar
3
+ VERSION = "0.1.0.alpha2"
4
+ end
5
+ end
@@ -0,0 +1,39 @@
1
+ require "ars/familiar/version"
2
+ require 'active_support/inflector'
3
+
4
+ module Ars
5
+ module Familiar
6
+ autoload :Mongoid, 'ars/familiar/mongoid'
7
+ autoload :Base, 'ars/familiar/base'
8
+ autoload :Scribe, 'ars/familiar/scribe'
9
+
10
+ module DSL
11
+ attr_reader :builder
12
+
13
+ def shadow(model)
14
+ consume = model.dup
15
+ model = Familiar::Mongoid.new(model)
16
+ consumed = consume.constantize rescue false
17
+ unless consumed
18
+ Scribe.read_for model
19
+ end
20
+ @builder = model
21
+ end
22
+
23
+ def realize_attributes!
24
+ primary_key builder.primary_key if builder.primary_key?
25
+ attributes builder.attributes_to_define
26
+ end
27
+
28
+ def realize_relationships!(**opts)
29
+ builder.relationships_to_define(opts).each do |relationship|
30
+ self.send(*relationship)
31
+ end
32
+ end
33
+ end
34
+
35
+ def self.included(subclass)
36
+ subclass.extend DSL
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ars-familiar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.alpha2
5
+ platform: ruby
6
+ authors:
7
+ - Michael Lee Vazquez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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: aruba
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: rake
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
+ description: Ruby Object Metadata Reflector
70
+ email:
71
+ - magnus.nothus@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - ars-familiar.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/ars/familiar.rb
88
+ - lib/ars/familiar/base.rb
89
+ - lib/ars/familiar/constantizable_array.rb
90
+ - lib/ars/familiar/mongoid.rb
91
+ - lib/ars/familiar/scribe.rb
92
+ - lib/ars/familiar/version.rb
93
+ homepage: http://github.com/cunningfolk/ars-familiar
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.3.1
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.2.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: ars-familiar-#{Ars::Familiar::VERSION}
117
+ test_files: []