activefolder 0.0.2

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: 1586430b73f7e0e4ba890924542ff44431b7edff
4
+ data.tar.gz: 3ee0b9be0f4439a3463ad80cf56bf993c90e50a2
5
+ SHA512:
6
+ metadata.gz: 8fc8458039e915617121801cf32132a23296f60bc637e451a4191c211923cdc69d4fffdfb04c5b81348e2176212152d02f7fbb95d504adf6456e869425ca7f84
7
+ data.tar.gz: c4e5912880e2f07ed4c7cd41395b62bb4a462543f5dcbdd5e56ff8da9d5a4c30d025794f150ee96dca929edc43034c4f587b44e3a7670e9d6d445c8aa24e0bf6
@@ -0,0 +1,23 @@
1
+ require 'active_support/all'
2
+
3
+ require 'active_folder/base'
4
+ require 'active_folder/config'
5
+ require 'active_folder/metal/client'
6
+
7
+ module ActiveFolder
8
+ class << self
9
+ def setup
10
+ yield config
11
+ end
12
+
13
+ def client
14
+ @client ||= Metal::Client.new(config.client)
15
+ end
16
+
17
+ private
18
+
19
+ def config
20
+ @config ||= Config.new
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ require 'ostruct'
2
+
3
+ require 'active_folder/model/traits/persistence'
4
+ require 'active_folder/model/traits/has_belongs'
5
+ require 'active_folder/model/errors'
6
+
7
+ module ActiveFolder
8
+ class Base < OpenStruct
9
+ include Model::Traits::Persistence
10
+ include Model::Traits::HasBelongs
11
+ extend Model::Traits::Collection
12
+
13
+ class << self
14
+ def model_name
15
+ model = name.demodulize
16
+ model.underscore.pluralize
17
+ end
18
+
19
+ def model_class; self; end
20
+ def model_base_dir; '/' end
21
+ end
22
+
23
+ def path
24
+ required(:name); required(:base_dir)
25
+ File.join(base_dir, name)
26
+ end
27
+
28
+ def attributes
29
+ self.to_h.except(:name, :base_dir)
30
+ end
31
+
32
+ private
33
+
34
+ def required(attr)
35
+ return unless attr.to_s.empty?
36
+ raise MissingAttributeError.new(attr)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_folder/metal/config'
2
+
3
+ module ActiveFolder
4
+ class Config
5
+ attr_reader :client
6
+
7
+ def initialize
8
+ @client = Metal::Config.new
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveFolder
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'fileutils'
2
+
3
+ require 'active_folder/metal/errors'
4
+
5
+ module ActiveFolder
6
+ module Metal
7
+ module Adapters
8
+ class Bare
9
+ def initialize(connection)
10
+ @root = connection.root_path
11
+ end
12
+
13
+ def read(path:)
14
+ File.read full_path(path)
15
+ rescue SystemCallError => e
16
+ raise SystemError.new(e)
17
+ end
18
+
19
+ def write(path:, data:)
20
+ dir = File.dirname full_path(path)
21
+ FileUtils.mkdir_p dir
22
+ File.write(full_path(path), data)
23
+ rescue SystemCallError => e
24
+ raise SystemError.new(e)
25
+ end
26
+
27
+ def glob(path:)
28
+ paths = Dir.glob full_path(path)
29
+ paths.map { |p| relative_path(p) }
30
+ rescue SystemCallError => e
31
+ raise SystemError.new(e)
32
+ end
33
+
34
+ private
35
+
36
+ def full_path(path)
37
+ File.join(@root, path)
38
+ end
39
+
40
+ def relative_path(path)
41
+ path.sub(@root, '')
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,34 @@
1
+ require 'active_folder/metal/adapters/bare'
2
+ require 'active_folder/metal/connections/bare'
3
+
4
+ module ActiveFolder
5
+ module Metal
6
+ class Client
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def load(**args)
12
+ adapter.read(**args)
13
+ end
14
+
15
+ def save(**args)
16
+ adapter.write(**args)
17
+ end
18
+
19
+ def glob(**args)
20
+ adapter.glob(**args)
21
+ end
22
+
23
+ private
24
+
25
+ def adapter
26
+ @adapter ||= Adapters::Bare.new(connection)
27
+ end
28
+
29
+ def connection
30
+ @connection ||= Connections::Bare.new(@config)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveFolder
2
+ module Metal
3
+ class Config
4
+ attr_accessor :root_path
5
+
6
+ def initialize
7
+ self.root_path = '.'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveFolder
2
+ module Metal
3
+ module Connections
4
+ class Bare
5
+ def initialize(config)
6
+ @config = config
7
+ end
8
+
9
+ def root_path
10
+ @config.root_path
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_folder/errors'
2
+
3
+ module ActiveFolder
4
+ module Metal
5
+ class SyntaxError < Error; end
6
+ class SystemError < Error; end
7
+ end
8
+ end
@@ -0,0 +1,36 @@
1
+ require 'yaml'
2
+
3
+ require 'active_folder/metal/errors'
4
+
5
+ module ActiveFolder
6
+ module Metal
7
+ module Files
8
+ class Yaml
9
+ def initialize(dir:, name:)
10
+ @dir = dir; @name = name
11
+ end
12
+
13
+ def load
14
+ YAML.load client.load(path: path)
15
+ rescue Psych::SyntaxError => e
16
+ raise SyntaxError.new(e)
17
+ end
18
+
19
+ def save(content)
20
+ data = content.to_yaml
21
+ client.save(path: path, data: data)
22
+ end
23
+
24
+ private
25
+
26
+ def path
27
+ File.join(@dir, "#{@name}.yaml")
28
+ end
29
+
30
+ def client
31
+ ActiveFolder.client
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_folder/model/traits/collection'
2
+
3
+ module ActiveFolder
4
+ module Model
5
+ module Collections
6
+ class HasMany
7
+ include Model::Traits::Collection
8
+
9
+ def initialize(owner, collection)
10
+ @owner = owner; @collection = collection
11
+ end
12
+
13
+ def model_class
14
+ @collection.classify.constantize
15
+ end
16
+
17
+ def model_name; @collection end
18
+ def model_base_dir; @owner.path end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_folder/errors'
2
+
3
+ module ActiveFolder
4
+ module Model
5
+ class MissingAttributeError < Error; end
6
+ class TypeError < Error; end
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_folder/model/traits/enumeration'
2
+
3
+ module ActiveFolder
4
+ module Model
5
+ module Traits
6
+ module Collection
7
+ include Enumeration
8
+
9
+ def build(**args)
10
+ dir = File.join(model_base_dir, model_name)
11
+ model_class.new(**args.merge(base_dir: dir))
12
+ end
13
+
14
+ def create(**args)
15
+ instance = build(**args); instance.save!; instance
16
+ end
17
+
18
+ def find_or_create(**args)
19
+ find(args[:name]) || create(**args)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveFolder
2
+ module Model
3
+ module Traits
4
+ module Enumeration
5
+ def find(name)
6
+ results = all(name)
7
+ results.count < 2 ? results.first : results
8
+ end
9
+
10
+ def all(name = '*')
11
+ element = File.join(model_name, name)
12
+ query = File.join(model_base_dir, '**', element)
13
+
14
+ paths = ActiveFolder.client.glob(path: query)
15
+ paths.map { |path| model_class.load(path) }
16
+ end
17
+
18
+ def last
19
+ all.last
20
+ end
21
+
22
+ def first
23
+ all.first
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_folder/model/collections/has_many'
2
+
3
+ module ActiveFolder
4
+ module Model
5
+ module Traits
6
+ module HasBelongs
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def has_many collection
11
+ define_method collection do
12
+ Collections::HasMany.new(self, collection.to_s)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_folder/metal/files/yaml'
2
+ require 'active_folder/model/errors'
3
+
4
+ module ActiveFolder
5
+ module Model
6
+ module Traits
7
+ module Persistence
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ def load!
12
+ attrs = attributes_file.load
13
+ raise TypeError.new(attrs) unless attrs.respond_to?(:each_pair)
14
+ attrs.each { |k,v| self[k] = v }
15
+ end
16
+
17
+ def save!
18
+ attributes_file.save(attributes)
19
+ end
20
+
21
+ private
22
+
23
+ def attributes_file
24
+ params = { dir: path, name: 'attributes' }
25
+ Metal::Files::Yaml.new(**params)
26
+ end
27
+ end
28
+
29
+ class_methods do
30
+ def load(path)
31
+ params = { name: File.basename(path),
32
+ base_dir: File.dirname(path) }
33
+
34
+ instance = self.new(**params)
35
+ instance.load!; instance
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activefolder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ben Thorner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-29 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: '4.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.2.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.2.5
33
+ description: A file system ORM
34
+ email: benthorner@outlook.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/active_folder.rb
40
+ - lib/active_folder/base.rb
41
+ - lib/active_folder/config.rb
42
+ - lib/active_folder/errors.rb
43
+ - lib/active_folder/metal/adapters/bare.rb
44
+ - lib/active_folder/metal/client.rb
45
+ - lib/active_folder/metal/config.rb
46
+ - lib/active_folder/metal/connections/bare.rb
47
+ - lib/active_folder/metal/errors.rb
48
+ - lib/active_folder/metal/files/yaml.rb
49
+ - lib/active_folder/model/collections/has_many.rb
50
+ - lib/active_folder/model/errors.rb
51
+ - lib/active_folder/model/traits/collection.rb
52
+ - lib/active_folder/model/traits/enumeration.rb
53
+ - lib/active_folder/model/traits/has_belongs.rb
54
+ - lib/active_folder/model/traits/persistence.rb
55
+ homepage: https://github.com/benthorner/active_folder
56
+ licenses:
57
+ - GNU GPL v2.0
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.2.2
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.4.5
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Active Folder
79
+ test_files: []