datacraft 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcf078a0a72cdec3ec5a91b960b7fda170b7a5d5
4
- data.tar.gz: 0a56de5697fc086c38b9570091400599590056e9
3
+ metadata.gz: 0d5b93cedf58b0affe9e22abc1f1597a3de90c4e
4
+ data.tar.gz: d7592ee90f256ad37cd2c6b8b90a5b71cc397581
5
5
  SHA512:
6
- metadata.gz: fabbff6bb6ab5e68ba24694496229094c163d11a2230113bb52e58a87d1e22511cf328d8eef8125795727a234ab50fde510b89e55dc7e48162e369314b32fa83
7
- data.tar.gz: e7400f95ee9b21b6f4b25809ab5abc47ffedc47583260ec53329bc6c55f5ec90edc7a2e45bf7dbe7ccffd97c9048bdff8ba91bff1457f5c4ae260ec40c9de9a4
6
+ metadata.gz: e0eb59fa1bb9d2cb546425393bd07f97c754c343a4d685eeb2e11b3c55061617ff1ec22626a16814bd5a0c0ce29e188205834af64f8a570dd85abf566d2a7116
7
+ data.tar.gz: 2cb72b1a6852382c61985f620c8542744af97dab13df5091d443e810cac397af54a46525e86a44169381b00748533a2eaa3c2b168a00f1fe90dd09a60d6596b0
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # Datacraft
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/datacraft`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Play with data like a Pro, and have fun like Minecraft.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ [![Gem Version](https://badge.fury.io/rb/datacraft.svg)](http://badge.fury.io/rb/datacraft)
6
+ [![Build Status](https://travis-ci.org/xiaoxinghu/datacraft.svg?branch=master)](https://travis-ci.org/xiaoxinghu/datacraft)
7
+ [![Dependency Status](https://gemnasium.com/xiaoxinghu/datacraft.svg)](https://gemnasium.com/xiaoxinghu/datacraft)
8
+ [![Code Climate](https://codeclimate.com/github/xiaoxinghu/datacraft/badges/gpa.svg)](https://codeclimate.com/github/xiaoxinghu/datacraft)
6
9
 
7
10
  ## Installation
8
11
 
@@ -22,7 +25,7 @@ Or install it yourself as:
22
25
 
23
26
  ## Usage
24
27
 
25
- TODO: Write usage instructions here
28
+ $ dcraft build inst.rb
26
29
 
27
30
  ## Development
28
31
 
@@ -32,10 +35,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
35
 
33
36
  ## Contributing
34
37
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/datacraft. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xiaoxinghu/datacraft. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
39
 
37
40
 
38
41
  ## License
39
42
 
40
43
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
data/exe/dcraft CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w{ .. lib })
2
3
 
3
4
  require 'thor'
4
5
  require_relative '../lib/datacraft/cli'
@@ -19,5 +19,9 @@ module Datacraft
19
19
  def post_hooks
20
20
  @post_hooks ||= HookRegistry.new
21
21
  end
22
+
23
+ def options
24
+ @options ||= {}
25
+ end
22
26
  end
23
27
  end
@@ -27,6 +27,10 @@ module Datacraft
27
27
  @context.post_hooks << { block: block }
28
28
  end
29
29
 
30
+ def set(key, value)
31
+ @context.options[key.to_sym] = value
32
+ end
33
+
30
34
  def self.from_file(filename)
31
35
  script_content = IO.read(filename)
32
36
  instruction = Instruction.new
@@ -1,3 +1,5 @@
1
+ require 'forwardable'
2
+
1
3
  module Datacraft
2
4
  # for normalizing blocks
3
5
  class CompatiableProc < Proc
@@ -8,12 +10,24 @@ module Datacraft
8
10
 
9
11
  # common registry
10
12
  class Registry
13
+ extend Forwardable
14
+ def_delegators :instances, :each, :map
11
15
  def initialize
12
16
  @items = []
13
17
  end
14
18
 
15
19
  attr_accessor :mandatory_methods
16
20
 
21
+ def instances
22
+ @instances ||= @items.map do |i|
23
+ if i[:klass]
24
+ i[:klass].new(*i[:args])
25
+ elsif i[:block]
26
+ CompatiableProc.new(&i[:block])
27
+ end
28
+ end
29
+ end
30
+
17
31
  def <<(val)
18
32
  klass = val[:klass]
19
33
  block = val[:block]
@@ -32,19 +46,6 @@ module Datacraft
32
46
  klass.method_defined? m
33
47
  end
34
48
  end
35
-
36
- def each
37
- @instances ||= @items.map do |i|
38
- if i[:klass]
39
- i[:klass].new(*i[:args])
40
- elsif i[:block]
41
- CompatiableProc.new(&i[:block])
42
- end
43
- end
44
- @instances.each do |instance|
45
- yield instance
46
- end
47
- end
48
49
  end
49
50
 
50
51
  class ProviderRegistry < Registry
@@ -1,17 +1,49 @@
1
+ require 'benchmark'
2
+
1
3
  module Datacraft
2
4
  # The runner for the whole process
3
5
  module Runner
6
+ # run the instruction
4
7
  def run(instruction)
5
8
  context = instruction.context
6
- context.pre_hooks.each(&:call)
7
- process_rows(
8
- context.providers,
9
- context.tweakers,
10
- context.consumers
11
- )
12
- context.post_hooks.each(&:call)
9
+ measurements = []
10
+ measurements << Benchmark.measure('pre build:') do
11
+ context.pre_hooks.each(&:call)
12
+ end
13
+ measurements << Benchmark.measure('process rows:') do
14
+ if context.options[:parallel]
15
+ pprocess_rows(
16
+ context.providers,
17
+ context.tweakers,
18
+ context.consumers)
19
+ else
20
+ process_rows(
21
+ context.providers,
22
+ context.tweakers,
23
+ context.consumers)
24
+ end
25
+ end
26
+
27
+ measurements << Benchmark.measure('build:') do
28
+ build context.consumers
29
+ end
30
+
31
+ measurements << Benchmark.measure('post build:') do
32
+ context.post_hooks.each(&:call)
33
+ end
34
+ report measurements if context.options[:benchmark]
13
35
  end
14
36
 
37
+ # output benchmark results
38
+ def report(measurements)
39
+ width = measurements.max_by { |m| m.label.size }.label.size + 1
40
+ puts "#{' ' * width}#{Benchmark::CAPTION}"
41
+ measurements.each do |m|
42
+ puts "#{m.label.to_s.ljust width} #{m}"
43
+ end
44
+ end
45
+
46
+ # process rows sequentially
15
47
  def process_rows(providers, tweakers, consumers)
16
48
  providers.each do |provider|
17
49
  provider.each do |row|
@@ -26,11 +58,33 @@ module Datacraft
26
58
  end
27
59
  end
28
60
  end
61
+ end
62
+
63
+ # process rows in parallel
64
+ def pprocess_rows(providers, tweakers, consumers)
65
+ threads = providers.map do |provider|
66
+ Thread.new(provider) do |p|
67
+ p.each do |row|
68
+ tweakers.each do |tweaker|
69
+ row = tweaker.tweak row
70
+ break unless row
71
+ end
72
+ next unless row
73
+ consumers.each do |consumer|
74
+ consumer << row
75
+ end
76
+ end
77
+ end
78
+ end
79
+ threads.each(&:join)
80
+ end
81
+
82
+ # build and close consumers
83
+ def build(consumers)
29
84
  consumers.each do |consumer|
30
85
  consumer.build if consumer.respond_to? :build
31
86
  consumer.close if consumer.respond_to? :close
32
87
  end
33
88
  end
34
-
35
89
  end
36
90
  end
@@ -1,3 +1,3 @@
1
1
  module Datacraft
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datacraft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiaoxing Hu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-11 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor