materialize 0.1.0 → 0.2.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 +4 -4
- data/.DS_Store +0 -0
- data/lib/materialize/base_builder.rb +35 -0
- data/lib/materialize/entity.rb +25 -0
- data/lib/materialize/repo.rb +81 -0
- data/lib/materialize/version.rb +1 -1
- data/lib/materialize.rb +3 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55807b52a48599cec295368ba920296c197aeaf5
|
4
|
+
data.tar.gz: ae3d14f8536666bbc8d0465e2c26c40870aea6e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d5517a157f484dace8f31136b7505b9adbcb49b45d04ad3b2a37679c670648aaffe6df9a6cb022dc44d4a0a99138a7a70ea33441a687660ceab2528af4ef5ba
|
7
|
+
data.tar.gz: e68e581d0988f59b0a7f1971ba636342019888283d5bb5bca280ca993cec858972032da43e20734d1990d8ddaca663acd1c6d31680b246707ed803160fff3083
|
data/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Materialize
|
2
|
+
class BaseBuilder
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def build(data, repo, options)
|
6
|
+
entity_class.new(data)
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_all(data, repo, options)
|
10
|
+
entity_class.wrap(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
def concurrent(*lambdas)
|
14
|
+
threads = []
|
15
|
+
lambdas.each do |l|
|
16
|
+
threads << Thread.new do
|
17
|
+
l.()
|
18
|
+
end
|
19
|
+
end
|
20
|
+
threads.each(&:join)
|
21
|
+
end
|
22
|
+
|
23
|
+
def entity_class
|
24
|
+
"Entities::#{entity_base_class_name}".split('::').reduce(Module, :const_get)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def entity_base_class_name
|
30
|
+
"#{self.name[0..-8]}".split('::').last
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# To test:
|
2
|
+
# 1. Should be able to add extra arguments after attributes in a subclass
|
3
|
+
# 2. Should throw an error if hash is not provided as FIRST argument
|
4
|
+
|
5
|
+
module Materialize
|
6
|
+
class Entity
|
7
|
+
|
8
|
+
def self.wrap(entities_data)
|
9
|
+
entities_data.map { |entity_data| new(entity_data) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(attributes)
|
13
|
+
raise "Attributes must be a hash" unless attributes.is_a?(Hash)
|
14
|
+
|
15
|
+
attributes.each_pair do |key, value|
|
16
|
+
instance_variable_set("@#{key}", value)
|
17
|
+
|
18
|
+
(class << self; self; end).class_eval do
|
19
|
+
attr_reader key.to_sym
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Materialize
|
2
|
+
class Repo
|
3
|
+
|
4
|
+
attr_reader :token
|
5
|
+
|
6
|
+
def initialize(token = nil)
|
7
|
+
@token = token
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(query, *args, &block)
|
11
|
+
data_source_class = args[0]
|
12
|
+
options = args[1] || {}
|
13
|
+
args_to_pass = options[:args]
|
14
|
+
|
15
|
+
data, builder_class = process(data_source_class, query, args_to_pass)
|
16
|
+
options.delete(:args)
|
17
|
+
|
18
|
+
if data.is_a?(Array)
|
19
|
+
builder_class.build_all(data, self, options)
|
20
|
+
else
|
21
|
+
builder_class.build(data, self, options)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def process(data_source_class, query, args_to_pass)
|
28
|
+
data = get_data(data_source_class, query, args_to_pass)
|
29
|
+
builder_class = builder_class_for builder_class_name_for base_class_name_for data_source_class
|
30
|
+
return data, builder_class
|
31
|
+
end
|
32
|
+
|
33
|
+
def builder_class_for(builder_class_name)
|
34
|
+
if class_exists?(builder_class_name)
|
35
|
+
Module.const_get(builder_class_name)
|
36
|
+
else
|
37
|
+
Object.const_set(builder_class_name, Class.new(Materialize::BaseBuilder))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def builder_class_name_for(base_class_name)
|
42
|
+
"#{base_class_name}Builder"
|
43
|
+
end
|
44
|
+
|
45
|
+
def base_class_name_for(data_source_class)
|
46
|
+
data_source_class.name.split('::').last
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_data(data_source_class, query, args_to_pass)
|
50
|
+
if token.nil?
|
51
|
+
|
52
|
+
if args_to_pass.nil?
|
53
|
+
data_source_class.send(query)
|
54
|
+
elsif args_to_pass.is_a?(Array)
|
55
|
+
data_source_class.send(query, *args_to_pass)
|
56
|
+
else
|
57
|
+
data_source_class.send(query, args_to_pass)
|
58
|
+
end
|
59
|
+
|
60
|
+
else
|
61
|
+
|
62
|
+
if args_to_pass.nil?
|
63
|
+
data_source_class.send(query, token)
|
64
|
+
elsif args_to_pass.is_a?(Array)
|
65
|
+
data_source_class.send(query, token, *args_to_pass)
|
66
|
+
else
|
67
|
+
data_source_class.send(query, token, args_to_pass)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def class_exists?(class_name)
|
74
|
+
klass = Module.const_get(class_name)
|
75
|
+
return klass.is_a?(Class)
|
76
|
+
rescue NameError
|
77
|
+
return false
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
data/lib/materialize/version.rb
CHANGED
data/lib/materialize.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: materialize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stephen Fiser
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -59,6 +59,7 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- ".DS_Store"
|
62
63
|
- ".gitignore"
|
63
64
|
- ".rspec"
|
64
65
|
- ".travis.yml"
|
@@ -69,6 +70,9 @@ files:
|
|
69
70
|
- bin/console
|
70
71
|
- bin/setup
|
71
72
|
- lib/materialize.rb
|
73
|
+
- lib/materialize/base_builder.rb
|
74
|
+
- lib/materialize/entity.rb
|
75
|
+
- lib/materialize/repo.rb
|
72
76
|
- lib/materialize/version.rb
|
73
77
|
- materialize.gemspec
|
74
78
|
homepage: http://bluebear.io
|