Module: Pincushion
- Defined in:
- lib/pincushion.rb,
lib/pincushion/plugins.rb,
lib/pincushion/version.rb,
lib/pincushion/plugins/csv_serializer.rb,
lib/pincushion/plugins/json_serializer.rb
Defined Under Namespace
Modules: InstanceMethods, ModuleMethods, Plugins, RootModuleMethods
Constant Summary
collapse
- DuplicateIdentifierError =
Class.new(StandardError)
- MissingIdentifierError =
Class.new(StandardError)
- MissingPredicateError =
Class.new(StandardError)
- VERSION =
"0.1.3"
Class Method Summary
collapse
Class Method Details
.from_csv(csv, *args) ⇒ Object
4
5
6
7
8
|
# File 'lib/pincushion/plugins/csv_serializer.rb', line 4
def self.from_csv(csv, *args)
args << {} unless args.last.is_a?(Hash)
args.last.merge!(Plugins::CsvSerializer::OPTS)
from_rows(CSV.parse(csv, *args).map(&:to_hash))
end
|
.from_rows(rows) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/pincushion.rb', line 5
def self.from_rows(rows)
Module.new.module_eval do
include Pincushion
predicates = rows
.reduce(Set.new) { |a, e| a.merge(e.keys) }
.delete(:identifier)
predicates(*predicates)
rows.each do |row|
row = row.dup
identifier = row.delete(:identifier)
true_predicates = row.each_pair.select { |_, v| v }.map { |k, _| k }
that_is(*true_predicates).named(identifier)
end
self
end
end
|
.included(mod) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/pincushion.rb', line 182
def self.included(mod)
mod.instance_eval do
@identifiers = {}
@predicate_pincushion_root = self
extend ModuleMethods
extend RootModuleMethods
include InstanceMethods
is_not(*predicates)
end
end
|
.plugin(name) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/pincushion/plugins.rb', line 6
def self.plugin(name)
require "pincushion/plugins/#{name}"
mod = Plugins.const_get(Plugins.camelize(name))
extend mod::PincushionMethods if defined?(mod::PincushionMethods)
@plugins ||= []
@plugins << name
rescue LoadError
raise LoadError, "Plugin not found: #{name}"
rescue NameError => e
raise NameError, "#{e} for plugin: #{name}"
end
|
.plugins ⇒ Object
2
3
4
|
# File 'lib/pincushion/plugins.rb', line 2
def self.plugins
@plugins || []
end
|
.validate(mod, preds) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/pincushion.rb', line 25
def self.validate(mod, preds)
violations = Set.new(preds) - mod.predicates
return if violations.empty?
fail MissingPredicateError,
"Tried to set a value for unregistered predicate(s): "\
"#{violations.inspect}"
end
|