xf 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/lib/xf/identity.rb +1 -1
- data/lib/xf/scope.rb +10 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de2a5575576ea006a32330169f46e8283b69daf1f94d8b5714978e37fd981c9b
|
4
|
+
data.tar.gz: f89fa8eb3e35a23ffed94f516f750e3dd84ec814f9ae4817ef5adc206cda01f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0715912967cc2c85bf01a6486cfe4714784e28d1a6a20cde1c480ec07416d9e62d25ab6e8d9973262b9d762122f4bbe1f81c2f55a07ca0a69152a7edf0531d6f'
|
7
|
+
data.tar.gz: 01c1f8b67288c6da4fe374ecae5804d8cda543c8038ef661a19de76f83271e315878bf3b0d51f9ce70de5298511ddab841f6827d8049d033cabade000a746a7f
|
data/README.md
CHANGED
@@ -65,6 +65,9 @@ people = [{name: "Robert", age: 22}, {name: "Roberta", age: 22}, {name: "Foo", a
|
|
65
65
|
|
66
66
|
people.map(&Xf.scope(:age).get)
|
67
67
|
# => [22, 22, 42, 18]
|
68
|
+
|
69
|
+
people.map(&Xf.scope(:age).get { |v| v > 20 })
|
70
|
+
# => [true, true, true, false]
|
68
71
|
```
|
69
72
|
|
70
73
|
Let's try setting a value:
|
@@ -129,6 +132,13 @@ first_name_trace = Xf.trace('first')
|
|
129
132
|
people.map(&first_name_trace.get)
|
130
133
|
# => [["Erickson"], ["Pugh"], ["Mullen"], ["Jacquelyn"], ["Miller"], ["Jolene"]]
|
131
134
|
|
135
|
+
# It can take a function too that gives back the parent hash and key as well.
|
136
|
+
#
|
137
|
+
# Why no shorthand? Arity of the function, and you can't have more than one `&`
|
138
|
+
# in the same code block. Ruby's no fan of it.
|
139
|
+
people.map(&first_name_trace.get { |h, k, v| v.downcase })
|
140
|
+
# => [["erickson"], ["pugh"], ["mullen"], ["jacquelyn"], ["miller"], ["jolene"]]
|
141
|
+
|
132
142
|
# You can even compose them if you want to. Just remember compose
|
133
143
|
people.flat_map(&Xf.compose(first_name_trace.set('Spartacus'), first_name_trace.get))
|
134
144
|
# => ["Spartacus", "Spartacus", "Spartacus", "Spartacus", "Spartacus", "Spartacus"]
|
data/lib/xf/identity.rb
CHANGED
data/lib/xf/scope.rb
CHANGED
@@ -51,11 +51,20 @@ module Xf
|
|
51
51
|
# Direct value getter, though it may be wiser to use Hash#dig here
|
52
52
|
# instead if you're concerned about speed.
|
53
53
|
#
|
54
|
+
# If the object does not respond to dig, Xf will attempt to use an
|
55
|
+
# array bracket accessor dive instead for compatability reasons.
|
56
|
+
#
|
54
57
|
# @param hash [Hash] Hash to get value from
|
55
58
|
#
|
56
59
|
# @return [Any]
|
57
60
|
def get_value(hash, &fn)
|
58
|
-
value = hash.dig
|
61
|
+
value = if hash.respond_to?(:dig)
|
62
|
+
hash.dig(*@paths)
|
63
|
+
else
|
64
|
+
new_hash = hash
|
65
|
+
@paths.each { |s| new_hash = new_hash[s] }
|
66
|
+
new_hash
|
67
|
+
end
|
59
68
|
|
60
69
|
block_given? ? fn[value] : value
|
61
70
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Weaver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gemsmith
|