arrayfu 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/arrayfu.gemspec +0 -0
- data/lib/arrayfu/array_definition.rb +84 -0
- data/lib/arrayfu/arrayfu.rb +31 -0
- data/lib/arrayfu/generate_mutators.rb +28 -0
- data/lib/arrayfu/{readable_step.rb → generate_readers.rb} +6 -4
- data/lib/arrayfu/generate_visitors.rb +18 -0
- data/lib/arrayfu/{writeable_step.rb → generate_writers.rb} +6 -4
- data/lib/arrayfu/initializer.rb +21 -0
- data/lib/arrayfu/item_constraint.rb +17 -0
- data/lib/arrayfu/module_registry.rb +9 -7
- data/lib/arrayfu/mutator_definition.rb +15 -0
- data/lib/arrayfu/version.rb +1 -1
- data/lib/arrayfu/visitor_definition.rb +19 -0
- data/lib/arrayfu.rb +10 -9
- data/spec/examples/{instance_usage.rb → usage.rb} +169 -72
- data/spec/specs/{dsl_spec.rb → array_definition_spec.rb} +20 -22
- data/spec/specs/dsl_usage_spec.rb +86 -52
- data/spec/specs/{mutator_step_spec.rb → generate_mutators_spec.rb} +5 -5
- data/spec/specs/{readable_step_spec.rb → generate_readers_spec.rb} +3 -3
- data/spec/specs/{visitor_detail_step_spec.rb → generate_visitors_spec.rb} +5 -5
- data/spec/specs/{writeable_step_spec.rb → generate_writers_step_spec.rb} +3 -3
- data/spec/specs/{object_extensions_spec.rb → initializer_spec.rb} +4 -1
- data/spec/specs/{add_criterion_spec.rb → item_constraint_spec.rb} +11 -11
- data/spec/specs/sample.rb +2 -0
- metadata +28 -29
- data/lib/arrayfu/add_criterion.rb +0 -16
- data/lib/arrayfu/dsl.rb +0 -43
- data/lib/arrayfu/mutator_detail.rb +0 -10
- data/lib/arrayfu/mutator_step.rb +0 -18
- data/lib/arrayfu/object_extensions.rb +0 -27
- data/lib/arrayfu/visitor_detail.rb +0 -10
- data/lib/arrayfu/visitor_detail_step.rb +0 -14
- data/spec/examples/class_usage.rb +0 -210
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a90d155b62053d740976ddc34cfbbcf0f5b30d04
|
4
|
+
data.tar.gz: bfd56440c6bfc8e35b43b9614f13135c3c75effc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5eb88f16b8067459c963d21389ff8904fa2b7a3d139ae0cdfabfd1278d0da9d52479df80ad37d4ca12e91a99d9b7c14b73fc7336d02184efb995b7aeb79d611
|
7
|
+
data.tar.gz: 2ef91b2bca6aaaa484d7074db470c74462dc49d59a69dfad750218778e61d246908517858020b7d8468ddd347eda992f28881d03b215dc059f641eaa5c27e119
|
data/arrayfu.gemspec
CHANGED
File without changes
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
class ArrayDefinition
|
3
|
+
include Initializer
|
4
|
+
|
5
|
+
attr_accessor :name
|
6
|
+
attr_accessor :writable
|
7
|
+
attr_accessor :readable
|
8
|
+
attr_accessor :mutators
|
9
|
+
attr_accessor :visitors
|
10
|
+
attr_accessor :constraints
|
11
|
+
|
12
|
+
def initialize(name)
|
13
|
+
@name = name
|
14
|
+
initialize_arrays :mutators, :visitors, :constraints
|
15
|
+
initialize_false :writable, :readable
|
16
|
+
end
|
17
|
+
|
18
|
+
def read_and_write
|
19
|
+
writable
|
20
|
+
readable
|
21
|
+
end
|
22
|
+
|
23
|
+
def writable
|
24
|
+
@writable = true
|
25
|
+
end
|
26
|
+
|
27
|
+
def readable
|
28
|
+
@readable = true
|
29
|
+
end
|
30
|
+
|
31
|
+
def writable?
|
32
|
+
@writable ||= false
|
33
|
+
end
|
34
|
+
|
35
|
+
def readable?
|
36
|
+
@readable ||= false
|
37
|
+
end
|
38
|
+
|
39
|
+
def configure_using(*configurators)
|
40
|
+
configurators.each do|configurator|
|
41
|
+
method = configurator.respond_to?(:configure) ? :configure : 'call'.to_sym
|
42
|
+
configurator.send(method, self)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def mutator(*names, &block)
|
47
|
+
names.each do |mutator_name|
|
48
|
+
self.mutators.push(MutatorDefinition.new(mutator_name, block))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module NoFailure
|
53
|
+
extend self
|
54
|
+
|
55
|
+
def run(description, value)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def each_mutator(&block)
|
60
|
+
mutators.each &block
|
61
|
+
end
|
62
|
+
|
63
|
+
def each_constraint(&block)
|
64
|
+
constraints.each &block
|
65
|
+
end
|
66
|
+
|
67
|
+
def each_visitor(&block)
|
68
|
+
visitors.each &block
|
69
|
+
end
|
70
|
+
|
71
|
+
def addition_constraint(constraint, fail_option = NoFailure)
|
72
|
+
self.constraints.push(ItemConstraint.new(constraint, fail_option))
|
73
|
+
end
|
74
|
+
alias :new_item_must :addition_constraint
|
75
|
+
|
76
|
+
def process_using(name,visitor)
|
77
|
+
self.visitors.push(VisitorDefinition.new(name, visitor))
|
78
|
+
end
|
79
|
+
|
80
|
+
def variable_name
|
81
|
+
"@#{@name}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
include Initializer
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize_arrayfu
|
9
|
+
self.class.each_array_definition do |array_definition|
|
10
|
+
initialize_arrays(array_definition.name)
|
11
|
+
ArrayFu::ModuleRegistry.configure(self, array_definition)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def array_definitions
|
17
|
+
@array_definitions ||= []
|
18
|
+
end
|
19
|
+
|
20
|
+
def each_array_definition(&block)
|
21
|
+
array_definitions.each &block
|
22
|
+
end
|
23
|
+
|
24
|
+
def array(name, &block)
|
25
|
+
array_definition = ArrayDefinition.new(name)
|
26
|
+
yield array_definition if block_given?
|
27
|
+
array_definitions << array_definition
|
28
|
+
array_definition
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
module GenerateMutators
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def create_using(builder)
|
6
|
+
Module.new do
|
7
|
+
builder.each_mutator do |mutator|
|
8
|
+
define_method(mutator.name) do|value|
|
9
|
+
array_var = instance_variable_get(builder.variable_name)
|
10
|
+
continue_add = true
|
11
|
+
|
12
|
+
builder.each_constraint do |constraint|
|
13
|
+
continue_add &= constraint.apply_to(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
return unless continue_add
|
17
|
+
|
18
|
+
if mutator.block
|
19
|
+
mutator.run(self, value)
|
20
|
+
else
|
21
|
+
array_var.push(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,13 +1,15 @@
|
|
1
1
|
module ArrayFu
|
2
|
-
|
2
|
+
module GenerateReaders
|
3
|
+
extend self
|
4
|
+
|
3
5
|
def create_using(builder)
|
4
6
|
Module.new do
|
5
|
-
if
|
7
|
+
if builder.readable?
|
6
8
|
define_method(builder.name) do
|
7
|
-
return instance_variable_get(
|
9
|
+
return instance_variable_get(builder.variable_name)
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
11
|
-
end
|
12
13
|
end
|
13
14
|
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
module GenerateVisitors
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def create_using(builder)
|
6
|
+
Module.new do
|
7
|
+
builder.each_visitor do |visitor|
|
8
|
+
define_method(visitor.name) do
|
9
|
+
array_var = instance_variable_get(builder.variable_name)
|
10
|
+
array_var.each do |item|
|
11
|
+
visitor.process(item)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,13 +1,15 @@
|
|
1
1
|
module ArrayFu
|
2
|
-
|
2
|
+
module GenerateWriters
|
3
|
+
extend self
|
4
|
+
|
3
5
|
def create_using(builder)
|
4
6
|
Module.new do
|
5
|
-
if (builder.writable)
|
7
|
+
if (builder.writable?)
|
6
8
|
define_method("#{builder.name}=") do|value|
|
7
|
-
instance_variable_set(
|
9
|
+
instance_variable_set(builder.variable_name, value)
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
11
|
-
end
|
12
13
|
end
|
13
14
|
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
module Initializer
|
3
|
+
def initialize_defaults(builder, *names)
|
4
|
+
names.each do|item|
|
5
|
+
instance_variable_set("@#{item}", builder.call)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize_arrays(*names)
|
10
|
+
initialize_defaults(lambda { [] }, *names)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize_hashes(*names)
|
14
|
+
initialize_defaults(lambda { {} }, *names)
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize_false(*names)
|
18
|
+
initialize_defaults(lambda { false }, *names)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
class ItemConstraint
|
3
|
+
attr_accessor :constraint
|
4
|
+
attr_accessor :failure_action
|
5
|
+
|
6
|
+
def initialize(constraint, failure_action)
|
7
|
+
@constraint = constraint
|
8
|
+
@failure_action = failure_action
|
9
|
+
end
|
10
|
+
|
11
|
+
def apply_to(value)
|
12
|
+
result = constraint.matches?(value)
|
13
|
+
failure_action.run(constraint.name, value) unless result
|
14
|
+
return result
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,16 +1,18 @@
|
|
1
1
|
module ArrayFu
|
2
|
-
|
2
|
+
module ModuleRegistry
|
3
3
|
def self.all_modules
|
4
4
|
return [
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
GenerateMutators,
|
6
|
+
GenerateVisitors,
|
7
|
+
GenerateWriters,
|
8
|
+
GenerateReaders
|
9
9
|
]
|
10
10
|
end
|
11
11
|
|
12
|
-
def self.configure(target,dsl)
|
13
|
-
all_modules.each
|
12
|
+
def self.configure(target, dsl)
|
13
|
+
all_modules.each do |the_module|
|
14
|
+
target.extend(the_module.create_using(dsl))
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
data/lib/arrayfu/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
module ArrayFu
|
2
|
+
class VisitorDefinition
|
3
|
+
attr_accessor :name
|
4
|
+
attr_accessor :visitor
|
5
|
+
|
6
|
+
def initialize(name, visitor)
|
7
|
+
@name = name
|
8
|
+
@visitor = visitor
|
9
|
+
end
|
10
|
+
|
11
|
+
def process(item)
|
12
|
+
if visitor.respond_to?(:run_using)
|
13
|
+
visitor.run_using(item)
|
14
|
+
else
|
15
|
+
item.send(visitor)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/arrayfu.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
require 'arrayfu/
|
2
|
-
require 'arrayfu/
|
1
|
+
require 'arrayfu/initializer'
|
2
|
+
require 'arrayfu/item_constraint'
|
3
|
+
require 'arrayfu/array_definition'
|
3
4
|
require 'arrayfu/module_registry'
|
4
|
-
require 'arrayfu/
|
5
|
-
require 'arrayfu/
|
6
|
-
require 'arrayfu/
|
7
|
-
require 'arrayfu/
|
8
|
-
require 'arrayfu/
|
9
|
-
require 'arrayfu/
|
10
|
-
require 'arrayfu/
|
5
|
+
require 'arrayfu/mutator_definition'
|
6
|
+
require 'arrayfu/generate_mutators'
|
7
|
+
require 'arrayfu/generate_readers'
|
8
|
+
require 'arrayfu/visitor_definition'
|
9
|
+
require 'arrayfu/generate_visitors'
|
10
|
+
require 'arrayfu/generate_writers'
|
11
|
+
require 'arrayfu/arrayfu'
|