yaml_b_sides 0.3.0 → 0.4.0

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: 290831d7f1b375fdc3677f8482747c435028ac5f
4
- data.tar.gz: 8ee7a67ada8164e8bbdbeb53fae5e49b9fc99f5c
3
+ metadata.gz: 7cce0a0ddcc46c69a3a4c195187c7be25009b4b2
4
+ data.tar.gz: b6d69e0b119b8e6696ce4fa3195c6847eae28d1c
5
5
  SHA512:
6
- metadata.gz: a9e7b2bd03c35870aec5feb6ba9a42ffd79a139f05969a79b8a6f58d9454928261c139dec9a40289a1fe9e75c3855823b9ecc3d5f3046e37b1525d9d48992118
7
- data.tar.gz: 9066c18974a19653f8adb232a53daea75ce2dcceb26a6c88b9eb3e14c67bd7871e6e7841555852277d8a16f8625104fb49c4c51cd3978f22addf9590c29c2a6e
6
+ metadata.gz: 46477bf5fabecaaac51a0bbb457fa7b60720141a5a4e426191c4ba54578814b04f928a37044438cf144305db733bd3dcfda9831e0fa16f8a8bb0f4abec4ef115
7
+ data.tar.gz: c3636b15c10d3621df75184df1fabe107a4c1d0238e6e5244ee78f4087de385ba1e14083fdd7047dc45ce8a46e706ad491d7323d54befde928eb8165a56f32e5
data/README.md CHANGED
@@ -75,6 +75,45 @@ You can define simple associations that behave very much like ActiveRecord assoc
75
75
  * `has_many`: the association object has the id of the base.
76
76
  * will return an array
77
77
 
78
+ #### Assocaition Options
79
+
80
+ Associations have some of the standard ActiveRecord options. Namely:
81
+ * `class`: specifies the class to find the record in.
82
+
83
+ ```ruby
84
+ has_one :special_thing, class: Thing
85
+ ```
86
+
87
+ * `class_name`: specifies the class w.o having to have the class defined. Handy for circular dependencies
88
+
89
+ ```ruby
90
+ class Person < YamlBSides::Base
91
+ has_one :nickname, class_name: "Pesudonym"
92
+ end
93
+
94
+ class Pseudonym < YamlBSides::Base
95
+ belongs_to :bearer, class_name: "Person"
96
+ end
97
+ ```
98
+
99
+ * `through`: many to may association helper.
100
+
101
+ ```ruby
102
+ class Person < YamlBSides::Base
103
+ has_many :person_foods
104
+ has_many :favorite_foods, through: :person_foods, class_name: "Food"
105
+ end
106
+
107
+ class PersonFood < YamlBSides::Base
108
+ belongs_to :person
109
+ belongs_to :food
110
+ end
111
+
112
+ class Food < YamlBSides::Base
113
+ end
114
+ ```
115
+
116
+ __NOTE__: only works for `has_one` and `has_many`
78
117
 
79
118
  ### Example
80
119
 
@@ -4,5 +4,6 @@ module YamlBSides
4
4
  autoload :BelongsTo, 'yaml_b_sides/associations/belongs_to'
5
5
  autoload :HasMany, 'yaml_b_sides/associations/has_many'
6
6
  autoload :HasOne, 'yaml_b_sides/associations/has_one'
7
+ autoload :Through, 'yaml_b_sides/associations/through'
7
8
  end
8
9
  end
@@ -7,7 +7,7 @@ module YamlBSides
7
7
  def initialize( base_class, name, opts = {} )
8
8
  self.name = name
9
9
  self.opts = opts
10
- self.key = "#{base_class.to_s.demodulize.underscore}_id"
10
+ self.key = idify base_class
11
11
  end
12
12
 
13
13
  def klass
@@ -19,15 +19,17 @@ module YamlBSides
19
19
  def association_class( name, opts = {} )
20
20
  a_class = opts[:class] if opts.has_key? :class
21
21
  a_class_name = opts[:class_name].constantize if opts.has_key? :class_name
22
- derived_class = derived_class_for name
23
22
 
24
- a_class || a_class_name || derived_class
23
+ a_class || a_class_name || derived_class_for( name )
25
24
  end
26
25
 
27
26
  def derived_class_for( name )
28
27
  name.to_s.singularize.classify.constantize
29
28
  end
30
29
 
30
+ def idify( class_name )
31
+ "#{class_name.to_s.demodulize.underscore}_id"
32
+ end
31
33
  end
32
34
  end
33
35
  end
@@ -1,13 +1,11 @@
1
1
  module YamlBSides
2
2
  module Associations
3
3
  class HasMany < Base
4
+ include Through
5
+
4
6
  def action
5
7
  :where
6
8
  end
7
-
8
- def query( instance )
9
- { key => instance.id }
10
- end
11
9
  end
12
10
  end
13
11
  end
@@ -1,13 +1,11 @@
1
1
  module YamlBSides
2
2
  module Associations
3
3
  class HasOne < Base
4
+ include Through
5
+
4
6
  def action
5
7
  :find_by
6
8
  end
7
-
8
- def query( instance )
9
- { key => instance.id }
10
- end
11
9
  end
12
10
  end
13
11
  end
@@ -0,0 +1,36 @@
1
+ module YamlBSides
2
+ module Associations
3
+ module Through
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+
8
+ def query( instance )
9
+ unless opts.has_key? :through
10
+ { key => instance.id }
11
+ else
12
+ { id: target_ids( instance ) }
13
+ end
14
+ end
15
+
16
+ protected
17
+
18
+ def through
19
+ opts[:through]
20
+ end
21
+
22
+ def target_ids( instance )
23
+ intermadiates = Array(instance.send( through ) )
24
+
25
+ intermadiates.map do | intermediate |
26
+ intermediate.send( through_key )
27
+ end
28
+ end
29
+
30
+ def through_key
31
+ idify klass
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -5,12 +5,19 @@ module YamlBSides
5
5
  data.each do |id, datum|
6
6
  key = datum[field]
7
7
 
8
+ # storing key => array of matching ids
8
9
  @indexed_data[key] = Array(@indexed_data[key]) + [id]
9
10
  end
10
11
  end
11
12
 
12
- def find(value)
13
- @indexed_data[value]
13
+ # returns ids
14
+ def find(values)
15
+ # find all the arrays of ids for the values,
16
+ # get rid of nils (value not present),
17
+ # and compact for a single array result
18
+ values.map do |value|
19
+ @indexed_data[value]
20
+ end.compact.flatten
14
21
  end
15
22
  end
16
23
  end
@@ -11,8 +11,8 @@ module YamlBSides
11
11
  self.__indices = __indices.merge( { field => Index.new(field, @data) } )
12
12
  end
13
13
 
14
- def find_in_index(field, value)
15
- keys = Array(index_for(field).find(value))
14
+ def find_in_index(field, values)
15
+ keys = Array(index_for(field).find(values))
16
16
 
17
17
  keys.map do |id|
18
18
  @data[id]
@@ -42,16 +42,16 @@ module YamlBSides
42
42
 
43
43
  def find_by_scan(params)
44
44
  @data.values.select do |datum|
45
- params.all? do |param, expcted_value|
46
- datum[param] == expcted_value
45
+ params.all? do |param, expected_value|
46
+ val = Array(expected_value).include? datum[param]
47
47
  end
48
48
  end
49
49
  end
50
50
 
51
51
  def find_by_indexed(params)
52
52
  sets = []
53
- params.each do |index, value|
54
- sets << find_in_index(index, value)
53
+ params.each do |index, values|
54
+ sets << find_in_index(index, Array(values))
55
55
  end
56
56
 
57
57
  # find the intersection of all the sets
@@ -1,3 +1,3 @@
1
1
  module YamlBSides
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/yaml_b_sides.gemspec CHANGED
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "bundler", "~> 1.12"
26
26
  spec.add_development_dependency "rake", "~> 10.0"
27
27
  spec.add_development_dependency "minitest", "~> 5.0"
28
+ spec.add_development_dependency "m"
28
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_b_sides
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Orlov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-14 00:00:00.000000000 Z
11
+ date: 2016-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: m
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'
83
97
  description: An ActiveRecord-like interface for reading from & queryeing flat yaml
84
98
  files
85
99
  email:
@@ -103,6 +117,7 @@ files:
103
117
  - lib/yaml_b_sides/associations/belongs_to.rb
104
118
  - lib/yaml_b_sides/associations/has_many.rb
105
119
  - lib/yaml_b_sides/associations/has_one.rb
120
+ - lib/yaml_b_sides/associations/through.rb
106
121
  - lib/yaml_b_sides/base.rb
107
122
  - lib/yaml_b_sides/cacheable.rb
108
123
  - lib/yaml_b_sides/index.rb