activefolder 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1586430b73f7e0e4ba890924542ff44431b7edff
4
- data.tar.gz: 3ee0b9be0f4439a3463ad80cf56bf993c90e50a2
3
+ metadata.gz: 5bdc8dd156be2cd626fb59f795a5f3efa00cd1ef
4
+ data.tar.gz: e51dcb7fe4f44018afcbf50cbba24b4de13fb3cb
5
5
  SHA512:
6
- metadata.gz: 8fc8458039e915617121801cf32132a23296f60bc637e451a4191c211923cdc69d4fffdfb04c5b81348e2176212152d02f7fbb95d504adf6456e869425ca7f84
7
- data.tar.gz: c4e5912880e2f07ed4c7cd41395b62bb4a462543f5dcbdd5e56ff8da9d5a4c30d025794f150ee96dca929edc43034c4f587b44e3a7670e9d6d445c8aa24e0bf6
6
+ metadata.gz: 06daf6a7afcd64c93e615d0912844dab4ff69cc595256918c22fc1e630b02516fa5358357a1af20e3bfd95cbccb21b54ab3d1aab53ee52901b40b56e92a4fdd1
7
+ data.tar.gz: 863793c1a6386a456042ace4063aa19e4f18b588fa9479be8ac7715e0fff90aeffe77c7b5b9ca0473e0e6e41471e5b3707fd244f73312a3e51a39f48112b8324
@@ -1,7 +1,11 @@
1
+ require 'active_folder/model/utilities/matcher'
2
+
1
3
  module ActiveFolder
2
4
  module Model
3
5
  module Traits
4
6
  module Enumeration
7
+ using Utilities::Matcher
8
+
5
9
  def find(name)
6
10
  results = all(name)
7
11
  results.count < 2 ? results.first : results
@@ -22,6 +26,14 @@ module ActiveFolder
22
26
  def first
23
27
  all.first
24
28
  end
29
+
30
+ def to_a
31
+ all
32
+ end
33
+
34
+ def where(**args)
35
+ all.select { |element| args.match(element) }
36
+ end
25
37
  end
26
38
  end
27
39
  end
@@ -1,4 +1,6 @@
1
- require 'active_folder/model/collections/has_many'
1
+ require 'pathname'
2
+
3
+ require 'active_folder/model/utilities/collection'
2
4
 
3
5
  module ActiveFolder
4
6
  module Model
@@ -9,9 +11,27 @@ module ActiveFolder
9
11
  class_methods do
10
12
  def has_many collection
11
13
  define_method collection do
12
- Collections::HasMany.new(self, collection.to_s)
14
+ Utilities::Collection.new(self, collection.to_s)
13
15
  end
14
16
  end
17
+
18
+ def belongs_to element
19
+ define_method element do
20
+ model_class = element.to_s.classify.constantize
21
+ model_class.current(self.path)
22
+ end
23
+ end
24
+
25
+ def current(path = Dir.pwd)
26
+ pathname = Pathname.new(path)
27
+
28
+ dir = pathname.ascend do |file|
29
+ parent_dir = file.parent.basename.to_s
30
+ break(file) if parent_dir == model_name
31
+ end
32
+
33
+ model_class.load(dir) if dir
34
+ end
15
35
  end
16
36
  end
17
37
  end
@@ -18,6 +18,19 @@ module ActiveFolder
18
18
  attributes_file.save(attributes)
19
19
  end
20
20
 
21
+ def update!(**args)
22
+ args.each { |k,v| self[k] = v }
23
+ self.save!
24
+ end
25
+
26
+ def update(**args)
27
+ args.each { |k,v| self[k] = v }
28
+ self.save
29
+ end
30
+
31
+ def load; self.load! end
32
+ def save; self.save! end
33
+
21
34
  private
22
35
 
23
36
  def attributes_file
@@ -28,9 +41,7 @@ module ActiveFolder
28
41
 
29
42
  class_methods do
30
43
  def load(path)
31
- params = { name: File.basename(path),
32
- base_dir: File.dirname(path) }
33
-
44
+ params = { name: File.basename(path), base_dir: File.dirname(path) }
34
45
  instance = self.new(**params)
35
46
  instance.load!; instance
36
47
  end
@@ -2,8 +2,8 @@ require 'active_folder/model/traits/collection'
2
2
 
3
3
  module ActiveFolder
4
4
  module Model
5
- module Collections
6
- class HasMany
5
+ module Utilities
6
+ class Collection
7
7
  include Model::Traits::Collection
8
8
 
9
9
  def initialize(owner, collection)
@@ -0,0 +1,35 @@
1
+ require 'active_folder/model/traits/enumeration'
2
+
3
+ module ActiveFolder
4
+ module Model
5
+ module Utilities
6
+ module Matcher
7
+ refine Object do
8
+ def match(object); self == object end
9
+ end
10
+
11
+ refine Hash do
12
+ def match(object)
13
+ keys.all? do |key|
14
+ if object.kind_of? OpenStruct
15
+ self[key].match object.send(key)
16
+ else
17
+ self[key].match object.to_h[key]
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ refine Array do
24
+ def match(object)
25
+ any? do |condition|
26
+ object.to_a.any? do |element|
27
+ condition.match(element)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activefolder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Thorner
@@ -46,12 +46,13 @@ files:
46
46
  - lib/active_folder/metal/connections/bare.rb
47
47
  - lib/active_folder/metal/errors.rb
48
48
  - lib/active_folder/metal/files/yaml.rb
49
- - lib/active_folder/model/collections/has_many.rb
50
49
  - lib/active_folder/model/errors.rb
51
50
  - lib/active_folder/model/traits/collection.rb
52
51
  - lib/active_folder/model/traits/enumeration.rb
53
52
  - lib/active_folder/model/traits/has_belongs.rb
54
53
  - lib/active_folder/model/traits/persistence.rb
54
+ - lib/active_folder/model/utilities/collection.rb
55
+ - lib/active_folder/model/utilities/matcher.rb
55
56
  homepage: https://github.com/benthorner/active_folder
56
57
  licenses:
57
58
  - GNU GPL v2.0