bbattribute_filters 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/bbattribute_filters.rb +91 -0
- data/lib/bbattribute_filters/attribute.rb +49 -0
- data/lib/bbattribute_filters/builders/conditions.rb +24 -0
- data/lib/bbattribute_filters/conditions/condition.rb +57 -0
- data/lib/bbattribute_filters/conditions/if.rb +23 -0
- data/lib/bbattribute_filters/conditions/none.rb +18 -0
- data/lib/bbattribute_filters/conditions/unless.rb +23 -0
- data/lib/bbattribute_filters/handlers/block.rb +19 -0
- data/lib/bbattribute_filters/handlers/evaluation.rb +21 -0
- data/lib/bbattribute_filters/handlers/proc.rb +21 -0
- data/lib/bbattribute_filters/handlers/symbol.rb +21 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f5d2c3f9c55564f4b937904bc3ccb2a8b66f94a36bab34306f4fa9c0892b48c3
|
4
|
+
data.tar.gz: f45828c0965ab56e4dde4b6c8811609676e2a7827f66d2403d5eb1ec8ce5c81d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ca778e871b25c98b993d33bedd0541b6ee5db38636e3d9a1522dba6ad6c309418c1caf2e3c72a94f4d8da1793d50758a7e2bd891044681b885e31c338e6c74a3
|
7
|
+
data.tar.gz: 44ed0c2d7dd3973c30e728dd060f41353fb1885d8ba04934a196cbec6411b6db085d45a5988752d76115f29930b92af76e20e565c434a24100ae14c0fca4c2e8
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'bbattribute_filters/attribute'
|
4
|
+
|
5
|
+
# The main BBAttributeFilters module. Should be included to
|
6
|
+
module BBAttributeFilters
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
base.class_eval do
|
10
|
+
# Gets all of the class Attributes. Please note this is the internal attributes
|
11
|
+
def filterable_attributes
|
12
|
+
self.class._get_fiterable_attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
# @abstract Subclass is expected to implement #evaluate_attribute
|
16
|
+
# @!method evaluate_attribute(name)
|
17
|
+
# Evaluate the value of the attribute using the symbol provided
|
18
|
+
|
19
|
+
# Gets all of the attributes as long as they pass the include? condition
|
20
|
+
# check
|
21
|
+
def all_attributes
|
22
|
+
filterable_attributes.each_with_object({}) do |(key, attr), to_return|
|
23
|
+
to_return[key] = attr.evaluate(self) if attr.include?(self)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Gets a single attribute via the name, this will run the include? check on
|
28
|
+
# the attribute before evaulating it
|
29
|
+
# @param name {Symbol} The name of the attribute to retrieve
|
30
|
+
def attribute(name)
|
31
|
+
attr = filterable_attributes[name]
|
32
|
+
attr.evaluate(self) if attr&.include?(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Gets a single attribute via the name but bypassed the included? check
|
36
|
+
# @param name {Symbol} The name of the attribute to retrieve
|
37
|
+
def attribute_forced(name)
|
38
|
+
attr = filterable_attributes[name]
|
39
|
+
attr ? attr.evaluate(self) : nil
|
40
|
+
end
|
41
|
+
|
42
|
+
# Gets a list of specific attributes based on the names give
|
43
|
+
def only_attributes(*only)
|
44
|
+
only.each_with_object({}) do |name, to_return|
|
45
|
+
to_return[name] = attribute(name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Rather than wasting time with an if check each time we access the attributes hash we instead
|
51
|
+
# set it up front upon include
|
52
|
+
base._set_fiterable_attributes({})
|
53
|
+
end
|
54
|
+
|
55
|
+
# The class methods on BBAttributeFilters
|
56
|
+
module ClassMethods
|
57
|
+
def inherited(base)
|
58
|
+
super
|
59
|
+
base._set_fiterable_attributes(@_filterable_attributes ? @_filterable_attributes.dup : {})
|
60
|
+
end
|
61
|
+
|
62
|
+
# Assigns a large set of attributes without options
|
63
|
+
def attributes(*attrs)
|
64
|
+
attrs.each do |attr|
|
65
|
+
attribute(attr)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Assigns a single attribute
|
70
|
+
def attribute(name, options = {}, &block)
|
71
|
+
_add_filterable_attribute(options.fetch(:key, name),
|
72
|
+
BBAttributeFilters::Attribute.new(name, options, &block))
|
73
|
+
end
|
74
|
+
|
75
|
+
# Sets the serialized attributes variable
|
76
|
+
def _set_fiterable_attributes(attrs)
|
77
|
+
@_filterable_attributes = attrs
|
78
|
+
end
|
79
|
+
|
80
|
+
# Gets the serialized attributes variable
|
81
|
+
def _get_fiterable_attributes
|
82
|
+
@_filterable_attributes
|
83
|
+
end
|
84
|
+
|
85
|
+
# Adds a single serialized attribute. This is an internal method
|
86
|
+
# used by the attribute method
|
87
|
+
def _add_filterable_attribute(key, attr)
|
88
|
+
@_filterable_attributes[key] = attr
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative('./builders/conditions')
|
4
|
+
require_relative('./handlers/block')
|
5
|
+
require_relative('./handlers/symbol')
|
6
|
+
|
7
|
+
module BBAttributeFilters
|
8
|
+
# The attribute class. Wraps up a single instances of an attribute with the
|
9
|
+
# handler and condition
|
10
|
+
class Attribute
|
11
|
+
def initialize(name, options = {}, &block)
|
12
|
+
raise ArgumentError, 'BBAttributeFilters::Attribute - Name should be a symbol' unless name.is_a?(::Symbol)
|
13
|
+
|
14
|
+
@name = name
|
15
|
+
@handler = build_handler(name, &block)
|
16
|
+
@condition = build_condition(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def include?(serializer)
|
20
|
+
@condition.include?(serializer)
|
21
|
+
end
|
22
|
+
|
23
|
+
def exclude?(serializer)
|
24
|
+
@condition.exclude?(serializer)
|
25
|
+
end
|
26
|
+
|
27
|
+
def evaluate(serializer)
|
28
|
+
@handler.evaluate(serializer)
|
29
|
+
end
|
30
|
+
|
31
|
+
def condition_type
|
32
|
+
@condition.type
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def build_condition(options)
|
38
|
+
Builders::Conditions.build_from_options(options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_handler(name, &block)
|
42
|
+
if block_given?
|
43
|
+
BBAttributeFilters::Handlers::Block.new(&block)
|
44
|
+
else
|
45
|
+
BBAttributeFilters::Handlers::Evaluation.new(name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative('../conditions/if')
|
4
|
+
require_relative('../conditions/unless')
|
5
|
+
require_relative('../conditions/none')
|
6
|
+
|
7
|
+
module BBAttributeFilters
|
8
|
+
module Builders
|
9
|
+
# Builder for conditions
|
10
|
+
class Conditions
|
11
|
+
class << self
|
12
|
+
def build_from_options(options)
|
13
|
+
if options.key?(:if)
|
14
|
+
BBAttributeFilters::Conditions::If.new(options[:if])
|
15
|
+
elsif options.key?(:unless)
|
16
|
+
BBAttributeFilters::Conditions::Unless.new(options[:unless])
|
17
|
+
else
|
18
|
+
BBAttributeFilters::Conditions::None.new
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative('../handlers/proc')
|
4
|
+
require_relative('../handlers/symbol')
|
5
|
+
|
6
|
+
module BBAttributeFilters
|
7
|
+
module Conditions
|
8
|
+
# The base class for all AttributeConditions. E.g. allows the :if :unless conditions to be
|
9
|
+
# used when creating attributes
|
10
|
+
class Condition
|
11
|
+
|
12
|
+
ALLOWED_CONDITIONS = %i[if unless none].freeze
|
13
|
+
|
14
|
+
attr_reader :type
|
15
|
+
|
16
|
+
def initialize(type, on)
|
17
|
+
unless invalid_type?(type)
|
18
|
+
raise ArgumentError, 'BBAttributeFilters::Conditions::Condition - Type should be :if, :unless or :none'
|
19
|
+
end
|
20
|
+
|
21
|
+
@type = type
|
22
|
+
@on = on
|
23
|
+
@handler = build_condition_handler(on)
|
24
|
+
end
|
25
|
+
|
26
|
+
def evaluate(evaluator)
|
27
|
+
@handler.evaluate(evaluator)
|
28
|
+
end
|
29
|
+
|
30
|
+
def include?(_serializer)
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def exclude?(_serializer)
|
35
|
+
false
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def invalid_type?(type)
|
41
|
+
Condition::ALLOWED_CONDITIONS.include?(type)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Builds the handler which will process the given condition.
|
45
|
+
# Current options are Symbol, Proc
|
46
|
+
def build_condition_handler(on)
|
47
|
+
if on.is_a?(::Symbol)
|
48
|
+
Handlers::Symbol.new(on)
|
49
|
+
elsif on.is_a?(::Proc)
|
50
|
+
Handlers::Proc.new(on)
|
51
|
+
else
|
52
|
+
raise ArgumentError, 'BBAttributeFilters::Conditions::Condition - Condition should be a symbol or Proc'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative('condition')
|
4
|
+
|
5
|
+
module BBAttributeFilters
|
6
|
+
module Conditions
|
7
|
+
##
|
8
|
+
# Allows the handling of the :if condition
|
9
|
+
class If < Condition
|
10
|
+
def initialize(on)
|
11
|
+
super(:if, on)
|
12
|
+
end
|
13
|
+
|
14
|
+
def include?(serializer)
|
15
|
+
evaluate(serializer)
|
16
|
+
end
|
17
|
+
|
18
|
+
def exclude?(serializer)
|
19
|
+
!evaluate(serializer)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative('condition')
|
4
|
+
|
5
|
+
module BBAttributeFilters
|
6
|
+
# The collections module
|
7
|
+
module Conditions
|
8
|
+
# Allows the handling of no condition
|
9
|
+
class None < Condition
|
10
|
+
def initialize
|
11
|
+
@type = :none
|
12
|
+
@on = nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def evaluate(evaluator); end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative('condition')
|
4
|
+
|
5
|
+
module BBAttributeFilters
|
6
|
+
module Conditions
|
7
|
+
##
|
8
|
+
# Allows the handling of the :unless condition
|
9
|
+
class Unless < Condition
|
10
|
+
def initialize(on)
|
11
|
+
super(:unless, on)
|
12
|
+
end
|
13
|
+
|
14
|
+
def include?(serializer)
|
15
|
+
!evaluate(serializer)
|
16
|
+
end
|
17
|
+
|
18
|
+
def exclude?(serializer)
|
19
|
+
evaluate(serializer)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BBAttributeFilters
|
4
|
+
module Handlers
|
5
|
+
# Handler which calls the block within the scope
|
6
|
+
# of the evaluator
|
7
|
+
class Block
|
8
|
+
def initialize(&block)
|
9
|
+
raise ArgumentError, 'BBAttributeFilters::Handlers::EvaluationHandler - Expected a Block' unless block_given?
|
10
|
+
|
11
|
+
@block = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def evaluate(evaluator)
|
15
|
+
evaluator.instance_eval(&@block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BBAttributeFilters
|
4
|
+
module Handlers
|
5
|
+
# Handler which passes the name of the attribute directly
|
6
|
+
# to the BBAttributeFilter via the evaluate_attribute
|
7
|
+
class Evaluation
|
8
|
+
def initialize(symbol)
|
9
|
+
unless symbol.is_a?(::Symbol)
|
10
|
+
raise ArgumentError, 'BBAttributeFilters::Handlers::EvaluationHandler - Expected a Symbol'
|
11
|
+
end
|
12
|
+
|
13
|
+
@symbol = symbol
|
14
|
+
end
|
15
|
+
|
16
|
+
def evaluate(evaluator)
|
17
|
+
evaluator.evaluate_attribute(@symbol)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BBAttributeFilters
|
4
|
+
module Handlers
|
5
|
+
# Handler which calls the proc within the scope
|
6
|
+
# of the evaluator
|
7
|
+
class Proc
|
8
|
+
def initialize(proc)
|
9
|
+
unless proc.is_a?(::Proc)
|
10
|
+
raise ArgumentError, 'BBAttributeFilters::Handlers::EvaluationHandler - Expected a Proc'
|
11
|
+
end
|
12
|
+
|
13
|
+
@proc = proc
|
14
|
+
end
|
15
|
+
|
16
|
+
def evaluate(evaluator)
|
17
|
+
evaluator.instance_exec(&@proc)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BBAttributeFilters
|
4
|
+
module Handlers
|
5
|
+
# Handler which passes the symbol provided to the
|
6
|
+
# evaluator
|
7
|
+
class Symbol
|
8
|
+
def initialize(symbol)
|
9
|
+
unless symbol.is_a?(::Symbol)
|
10
|
+
raise ArgumentError, 'BBAttributeFilters::Handlers::EvaluationHandler - Expected a Symbol'
|
11
|
+
end
|
12
|
+
|
13
|
+
@symbol = symbol
|
14
|
+
end
|
15
|
+
|
16
|
+
def evaluate(evaluator)
|
17
|
+
evaluator.public_send(@symbol)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bbattribute_filters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stuart Farnaby, Big Bear Studios
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-07-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: guard-rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.7.3
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.7.3
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.9.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.9.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.18.5
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.18.5
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.8.7.6
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.8.7.6
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- lib/bbattribute_filters.rb
|
90
|
+
- lib/bbattribute_filters/attribute.rb
|
91
|
+
- lib/bbattribute_filters/builders/conditions.rb
|
92
|
+
- lib/bbattribute_filters/conditions/condition.rb
|
93
|
+
- lib/bbattribute_filters/conditions/if.rb
|
94
|
+
- lib/bbattribute_filters/conditions/none.rb
|
95
|
+
- lib/bbattribute_filters/conditions/unless.rb
|
96
|
+
- lib/bbattribute_filters/handlers/block.rb
|
97
|
+
- lib/bbattribute_filters/handlers/evaluation.rb
|
98
|
+
- lib/bbattribute_filters/handlers/proc.rb
|
99
|
+
- lib/bbattribute_filters/handlers/symbol.rb
|
100
|
+
homepage: https://gitlab.com/big-bear-studios-open-source/bbattributefilters
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '2.5'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubygems_version: 3.0.1
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: ''
|
123
|
+
test_files: []
|