AR2H 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ddcf2999c0a5f1d385fa137606b3a12823e6fea
4
- data.tar.gz: 0dad5c3f0262e3d7e383436f9335fda42f8e1668
3
+ metadata.gz: 78cd604ee19ed84f6cc042874bb82930dc92c408
4
+ data.tar.gz: 7d3a2974b396088dac1a9af9e969f231013c29e4
5
5
  SHA512:
6
- metadata.gz: 225b070df89f6c25865c136550f0fe25c1cfd0a6ea7ba270894dd373c76f5bde1af47013a2153db3c71cc7a707f1d57c85fd0e1341f57d2c2444e1b4a0ec6a42
7
- data.tar.gz: d3cc358a946eb4ca388afb67eeaf3be46b7b422dcfeea1a3d5ea4c4b9a2e449a5ca05d00fcc9425dec643748e6e46c43a2be57cae712a9b570f25f2fadc17f67
6
+ metadata.gz: 595e245e38640802f4156a3b5af38bc6ef73a5ec1c695acc4e381972eaa28ecc8d9433834532448468e3f29b8c4ddbc992d7c19b454502d8b2afbc7f95f55c06
7
+ data.tar.gz: b84c618822e9105e0992d75e6baa3834356d018b0e8a047cdc7eb15ff04746efd2f01e78f06aa65dc90ac926fbded3ae5fe66fc2f4c79e56fdf0f20c6d2c396d
data/AR2H.gemspec CHANGED
@@ -1,28 +1,30 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'AR2H'
7
- spec.version = '0.0.0'
8
- spec.authors = ['Eugene Yak']
9
- spec.email = ['GeneAYak@gmail.com']
10
-
11
- spec.summary = ''
12
- spec.description = ''
13
- spec.homepage = 'https://gitlab.com/yak/AR2H'
14
-
15
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
- f.match(%r{^(test|spec|features)/})
17
- end
18
- spec.bindir = 'exe'
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ['lib']
21
-
22
- spec.add_development_dependency 'bundler', '~> 1.13'
23
- spec.add_development_dependency 'rake', '~> 10.0'
24
- spec.add_development_dependency 'rspec', '~> 3.5'
25
-
26
- spec.add_runtime_dependency 'activerecord'
27
- spec.add_runtime_dependency 'activesupport'
28
- end
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'AR2H'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['Eugene Yak']
9
+ spec.email = ['GeneAYak@gmail.com']
10
+
11
+ spec.summary = ''
12
+ spec.description = ''
13
+ spec.homepage = 'https://gitlab.com/yak/AR2H'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.13'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.5'
25
+ spec.add_development_dependency 'sqlite3', '~> 1.3', '>= 1.3.12'
26
+ spec.add_development_dependency 'simplecov', '~> 0.12.0'
27
+
28
+ spec.add_runtime_dependency 'activerecord'
29
+ spec.add_runtime_dependency 'activesupport'
30
+ end
data/lib/AR2H.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'AR2H/executor'
2
2
 
3
- require 'AR2H/extensions/base'
4
- require 'AR2H/extensions/relation'
3
+ require 'AR2H/concerns/active_record/base'
4
+ require 'AR2H/concerns/active_record/relation'
5
5
 
6
- require 'AR2H/hashers/collection'
7
- require 'AR2H/hashers/hasher'
6
+ require 'AR2H/presenters/collection'
7
+ require 'AR2H/presenters/presenter'
@@ -0,0 +1,31 @@
1
+ require 'active_record'
2
+
3
+ module AR2H
4
+ module Concerns
5
+ module ActiveRecordConcern
6
+ module Base
7
+ extend ActiveSupport::Concern
8
+
9
+ def present(presenter = :default)
10
+ Executor.run(self, presenter)
11
+ end
12
+
13
+ def presenters
14
+ self.class.presenters
15
+ end
16
+
17
+ module ClassMethods
18
+ def presenter(name = :default, &block)
19
+ presenters.add Presenters::Presenter.new(name, block)
20
+ end
21
+
22
+ def presenters
23
+ @presenters ||= Presenters::Collection.new(self.name)
24
+ end
25
+ end
26
+ end
27
+
28
+ ActiveRecord::Base.send :include, ActiveRecordConcern::Base
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_record'
2
+
3
+ module AR2H
4
+ module Concerns
5
+ module ActiveRecordConcern
6
+ module Relation
7
+ extend ActiveSupport::Concern
8
+
9
+ def present(presenter = :default)
10
+ self.map { |e| Executor.run(e, presenter) }
11
+ end
12
+ end
13
+
14
+ ActiveRecord::Relation.send :include, ActiveRecordConcern::Relation
15
+ end
16
+ end
17
+ end
18
+
data/lib/AR2H/executor.rb CHANGED
@@ -1,43 +1,43 @@
1
1
  module AR2H
2
2
  class Executor
3
- def self.run(object, hasher)
4
- self.new(object, hasher).execute
3
+ def self.run(object, presenter)
4
+ self.new(object, presenter).execute
5
5
  end
6
6
 
7
- def initialize(object, hasher)
8
- @object = object
9
- @hasher = object.hashers.find hasher
10
- @result = {}
7
+ def initialize(object, presenter)
8
+ @object = object
9
+ @presenter = object.presenters.find presenter
10
+ @result = Hash.new
11
11
  end
12
12
 
13
13
  attr_reader :object
14
14
 
15
15
  def execute
16
- instance_eval &@hasher.block
16
+ instance_eval(&@presenter.block)
17
17
  @result
18
18
  end
19
19
 
20
20
  private
21
21
 
22
- def attr(attr)
23
- @result[attr] = block_given? ? yield : object[attr]
22
+ def attr(attr, &block)
23
+ @result[attr] = block ? block.call : object[attr]
24
24
  end
25
25
 
26
26
  def attrs(*attrs)
27
27
  attrs.each { |attr| attr(attr) }
28
28
  end
29
29
 
30
- def has_one(node)
31
- node(node)
30
+ def has_one(model, &block)
31
+ _has_branch(model, &block)
32
32
  end
33
33
 
34
- def has_many(node)
35
- node(node)
34
+ def has_many(model, &block)
35
+ _has_branch(model, &block)
36
36
  end
37
37
 
38
- def branch(node)
39
- hasher = block_given? ? yield : @hasher.name
40
- @result[node] = object.send(node).hasherize(hasher)
38
+ def _has_branch(child, &block) # :nodoc:
39
+ child_presenter = block ? block.call : @presenter.name
40
+ @result[child] = object.send(child).present(child_presenter)
41
41
  end
42
42
  end
43
43
  end
@@ -0,0 +1,36 @@
1
+ module AR2H
2
+ module Presenters
3
+ class Collection
4
+ def initialize(model_name)
5
+ @model_name = model_name
6
+ @presenters = Array.new
7
+ end
8
+
9
+ def add(presenter)
10
+ if self.include? presenter.name
11
+ raise Exception.new("#{@model_name}: presenter #{presenter.name} is already exists")
12
+ else
13
+ @presenters << presenter
14
+ end
15
+ end
16
+
17
+ def all
18
+ @presenters
19
+ end
20
+
21
+ def list
22
+ @presenters.map{ |presenter| presenter.name }
23
+ end
24
+
25
+ def include?(name)
26
+ list.include? name
27
+ end
28
+
29
+ def find(name)
30
+ presenter = @presenters.find{ |hasher| hasher.name == name }
31
+ raise Exception.new("#{@model_name}: presenter #{name} not found") unless presenter
32
+ presenter
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,6 +1,6 @@
1
1
  module AR2H
2
- module Hashers
3
- class Hasher
2
+ module Presenters
3
+ class Presenter
4
4
  def initialize(name, block)
5
5
  @name = name
6
6
  @block = block
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: AR2H
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Yak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-23 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,40 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.12
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '1.3'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 1.3.12
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.12.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.12.0
55
89
  - !ruby/object:Gem::Dependency
56
90
  name: activerecord
57
91
  requirement: !ruby/object:Gem::Requirement
@@ -95,11 +129,11 @@ files:
95
129
  - bin/console
96
130
  - bin/setup
97
131
  - lib/AR2H.rb
132
+ - lib/AR2H/concerns/active_record/base.rb
133
+ - lib/AR2H/concerns/active_record/relation.rb
98
134
  - lib/AR2H/executor.rb
99
- - lib/AR2H/extensions/base.rb
100
- - lib/AR2H/extensions/relation.rb
101
- - lib/AR2H/hashers/collection.rb
102
- - lib/AR2H/hashers/hasher.rb
135
+ - lib/AR2H/presenters/collection.rb
136
+ - lib/AR2H/presenters/presenter.rb
103
137
  homepage: https://gitlab.com/yak/AR2H
104
138
  licenses: []
105
139
  metadata: {}
@@ -1,31 +0,0 @@
1
- module AR2H
2
- module Extensions
3
- module Base
4
- extend ActiveSupport::Concern
5
-
6
- def hasherize(hasher = :default)
7
- # require 'AR2H/executor'
8
- logger.info { " Hashing #{self.class}" }
9
- Executor.run(self, hasher)
10
- end
11
-
12
- def hashers
13
- self.class.hashers
14
- end
15
-
16
- module ClassMethods
17
- # require 'AR2H/hashers'
18
-
19
- def hasher(name = :default, &block)
20
- hashers.add Hashers::Hasher.new(name, block)
21
- end
22
-
23
- def hashers
24
- @hashers ||= Hashers::Collection.new(self.name)
25
- end
26
- end
27
- end
28
-
29
- ActiveRecord::Base.send :include, Base
30
- end
31
- end
@@ -1,15 +0,0 @@
1
- module AR2H
2
- module Extensions
3
- module Relation
4
- extend ActiveSupport::Concern
5
-
6
- def hasherize(hasher = :default)
7
- logger.info { " Hashing #{self.model.to_s.pluralize}" }
8
- self.map { |e| Executor.run(e, hasher) }
9
- end
10
- end
11
-
12
- ActiveRecord::Relation.send :include, Relation
13
- end
14
- end
15
-
@@ -1,37 +0,0 @@
1
- module AR2H
2
- module Hashers
3
- class Collection
4
- def initialize(label)
5
- @label = label
6
- @hashers = Array.new
7
- end
8
-
9
- def add(hasher)
10
- raise Exception.new('NOT hasher') if hasher.class != Hasher
11
- if self.include? hasher.name
12
- raise Exception.new('DOUBLING hasher')
13
- else
14
- @hashers << hasher
15
- end
16
- end
17
-
18
- def include?(name)
19
- list.include? name
20
- end
21
-
22
- def all
23
- @hashers
24
- end
25
-
26
- def list
27
- @hashers.map{ |hasher| hasher.name }
28
- end
29
-
30
- def find(name)
31
- hasher = @hashers.find{ |hasher| hasher.name == name }
32
- raise Exception.new("#{@label}: hasher :#{name} NOT FOUND") unless hasher
33
- hasher
34
- end
35
- end
36
- end
37
- end