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 +4 -4
- data/README.md +39 -0
- data/lib/yaml_b_sides/associations.rb +1 -0
- data/lib/yaml_b_sides/associations/base.rb +5 -3
- data/lib/yaml_b_sides/associations/has_many.rb +2 -4
- data/lib/yaml_b_sides/associations/has_one.rb +2 -4
- data/lib/yaml_b_sides/associations/through.rb +36 -0
- data/lib/yaml_b_sides/index.rb +9 -2
- data/lib/yaml_b_sides/indexable.rb +2 -2
- data/lib/yaml_b_sides/queriable.rb +4 -4
- data/lib/yaml_b_sides/version.rb +1 -1
- data/yaml_b_sides.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cce0a0ddcc46c69a3a4c195187c7be25009b4b2
|
4
|
+
data.tar.gz: b6d69e0b119b8e6696ce4fa3195c6847eae28d1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
|
@@ -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 =
|
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 ||
|
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
|
@@ -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
|
data/lib/yaml_b_sides/index.rb
CHANGED
@@ -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
|
-
|
13
|
-
|
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,
|
15
|
-
keys = Array(index_for(field).find(
|
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,
|
46
|
-
datum[param]
|
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,
|
54
|
-
sets << find_in_index(index,
|
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
|
data/lib/yaml_b_sides/version.rb
CHANGED
data/yaml_b_sides.gemspec
CHANGED
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.
|
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-
|
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
|