rom_factory 0.1.2 → 0.1.3

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: 0a018bff15466b0128f574e6a14cf178bf0372c3
4
- data.tar.gz: c5f5dc658060edb6826078e2e13ac2b0c08cb040
3
+ metadata.gz: 9b7808b0bff76822b94029d45568d1f007d20a89
4
+ data.tar.gz: be8c3075c20d56fcc8e8d216cf670e1a2c30423d
5
5
  SHA512:
6
- metadata.gz: 04762eb5e4c07a49df42f8157659f316ada9af6807c7385da4d07fb09eb05c926940b405793371c1ec62e9b4a62eb34ae9c6ae48705cb51678a368d7dd4cf732
7
- data.tar.gz: 38125c48764ed26d8b94e0fb4e3f5c0bdae8dcfaa0da7d3ee5c852a40ddaf26fce358c05bc4406d607dde0cfb7c2eb40095100dc0bc50b6a398196d111307273
6
+ metadata.gz: 0baa10ffc3ae0723f61c7955dbd315defd5f0305606ec3e9baa6ab735407811b4e1697c9d4e2b9e4eb34ef6c96693802feaf9912dd9f8ecd71971375d50c5218
7
+ data.tar.gz: 141140206d40ad4fe465ab6a7835322a0ba477cc6d0f4c5cf5299cec2842f328938726aa0420c93dbb2c3957cc85c0a0061e903c4d5bda8707e709bb71a0d814
data/README.md CHANGED
@@ -38,18 +38,12 @@ RomFactory::Config.configure do |config|
38
38
  config.container = container
39
39
  end
40
40
  ```
41
- This library works in conjunction with "rom-repository" so you will need to define repository with create command:
42
- ```ruby
43
- UsersRepo < ROM::Repository[:users] do
44
- commands :create
45
- end
46
- ```
47
41
  ## Simple use case
48
42
 
49
43
  After configuration is done, you can define the factory as follows:
50
44
  ```ruby
51
45
  RomFactory::Builder.define do |b|
52
- b.factory(repo: UsersRepo, name: :user) do |f|
46
+ b.factory(relation: :users, name: :user) do |f|
53
47
  f.first_name "Janis"
54
48
  f.last_name "Miezitis"
55
49
  f.email "janjiss@gmail.com"
@@ -62,39 +56,11 @@ user = RomFactory::Builder.create(:user)
62
56
  user.email #=> "janjiss@gmail.com"
63
57
  ```
64
58
 
65
- ### Mapping of records
66
- To follow ROM conventions, you can pass any object that responds to `.call` method with `as:` key in following way:
67
- ```ruby
68
- class User
69
- def self.call(attrs)
70
- self.new(attrs)
71
- end
72
-
73
- attr_reader :first_name, :last_name, :email
74
-
75
- def initialize(first_name:, last_name:, email:)
76
- @first_name, @last_name, @email = first_name, last_name, email
77
- end
78
- end
79
-
80
- RomFactory::Builder.define do |b|
81
- b.factory(repo: UsersRepo, name: :user, as: User) do |f|
82
- f.first_name "Janis"
83
- f.last_name "Miezitis"
84
- f.email "janjiss@gmail.com"
85
- end
86
- end
87
-
88
- user = RomFactory::Builder.create(:user)
89
- user.class #=> User
90
- ```
91
- I would recommend using `dry-types` library or similar for creating your domain objects.
92
-
93
59
  ### Callable properties
94
60
  You can easily define dynamic (callbale) properties if value needs to change every time it needs to be called. Anything that responds to `.call` can be dynamic property.
95
61
  ```ruby
96
62
  RomFactory::Builder.define do |b|
97
- b.factory(repo: UsersRepo, name: :user) do |f|
63
+ b.factory(relation: :users, name: :user) do |f|
98
64
  f.first_name "Janis"
99
65
  f.last_name "Miezitis"
100
66
  f.email "janjiss@gmail.com"
@@ -105,8 +71,6 @@ user = RomFactory::Builder.create(:user)
105
71
  user.created_at #=> 2016-08-27 18:17:08 -0500
106
72
  ```
107
73
 
108
-
109
-
110
74
  ## License
111
75
 
112
76
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -22,12 +22,11 @@ module RomFactory
22
22
  yield(self)
23
23
  end
24
24
 
25
- def factory(name:, repo:, as: nil, &block)
26
- @_repo = repo.new(RomFactory::Config.config.container)
25
+ def factory(name:, relation:, &block)
26
+ @_relation = RomFactory::Config.config.container.relations.fetch(relation)
27
27
  @name = name
28
- @_as = as
29
28
  @_schema = {}
30
- define_methods_from_repos_schema
29
+ define_methods_from_relation
31
30
  yield(self)
32
31
  end
33
32
 
@@ -39,18 +38,18 @@ module RomFactory
39
38
  [k, v]
40
39
  end
41
40
  end
42
- record = _repo.create(values.to_h)
43
- _as ? _as.call(record.to_h) : record
41
+ record_id = _relation.insert(values.to_h)
42
+ OpenStruct.new(_relation.where(id: record_id).one)
44
43
  end
45
44
 
46
45
  attr_reader :name
47
46
 
48
47
  private
49
48
 
50
- attr_reader :_repo, :_as, :_schema
49
+ attr_reader :_relation, :_schema
51
50
 
52
- def define_methods_from_repos_schema
53
- _repo.root.relation.attributes.each do |a|
51
+ def define_methods_from_relation
52
+ _relation.attributes.each do |a|
54
53
  define_singleton_method a, Proc.new {|v = nil, &block|
55
54
  if block
56
55
  _schema[a] = block
@@ -1,3 +1,3 @@
1
1
  module RomFactory
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janis Miezitis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-28 00:00:00.000000000 Z
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -171,6 +171,7 @@ files:
171
171
  - lib/rom_factory/builder.rb
172
172
  - lib/rom_factory/config.rb
173
173
  - lib/rom_factory/version.rb
174
+ - rom_factory-0.1.2.gem
174
175
  - rom_factory.gemspec
175
176
  homepage: ''
176
177
  licenses: