horza 0.4.0 → 0.5.0

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: 85bbbb02cee6bd4f2fe40c56bd9c37d16307b22f
4
- data.tar.gz: 0398edae9ed16b2bb8d13650b1869741e8213a2c
3
+ metadata.gz: 17772e0cbf9e727f3f7faa1cb164fde24a25d962
4
+ data.tar.gz: 54b00c21a99e3b706c4d3154a35bea05f4b8e8f7
5
5
  SHA512:
6
- metadata.gz: f890a99acceefb96521d07a8b0ef41a290438f4672f917bfefd68cc0085379de73dd050de8eff44b442bb0f91daa7e4a39f409893200bc2b2dd4d82dd4560347
7
- data.tar.gz: 98f821f258db1e72ce67439eb7aff920098a5c941954cb5fc8192ff491b68a7b12bd8247ce3aee336db9c14ed405a22ccb17b8e96d25195f70f58e767820aa59
6
+ metadata.gz: 85558cd6b04bb398969a19b635df243401246ea8ce5a05b72f08bd4fd9995636a5eed3e33bb8b7c2b249803e0e9863cb8b95e15601c5f472acb85b432f0cc3f4
7
+ data.tar.gz: d734305118e77162d4122696df034cf9cf29394d11a49eea3ed251c44e4d9af85bc9fe1e7adf64258e563463018f7f00651efe1a544515a58503483984b93847
data/README.md CHANGED
@@ -147,3 +147,9 @@ module CustomEntities
147
147
  end
148
148
  end
149
149
  ```
150
+
151
+ ## Active Model Semantics
152
+
153
+ The returned entity for ActiveRecord adapter now include ActiveModel semantics. This type of
154
+ entity SingleWithActiveModel can work with rails form helper and the submitted params will be
155
+ grouped under params[:horza_entities_single_with_active_model].
@@ -7,6 +7,7 @@ require 'horza/adapters/active_record/arel_join'
7
7
  require 'horza/core_extensions/string'
8
8
  require 'horza/entities/single'
9
9
  require 'horza/entities/collection'
10
+ require 'horza/entities/single_with_active_model'
10
11
  require 'horza/entities'
11
12
  require 'horza/configuration'
12
13
  require 'horza/errors'
@@ -5,6 +5,10 @@ module Horza
5
5
  CONTEXT_NAMESPACE = ::ActiveRecord::Base
6
6
 
7
7
  class << self
8
+ def single_entity_klass
9
+ ::Horza::Entities::SingleWithActiveModel
10
+ end
11
+
8
12
  def entity_context_map
9
13
  # Rails doesn't preload classes in development mode, caching doesn't make sense
10
14
  return ::Horza.descendants_map(CONTEXT_NAMESPACE) if ::Horza.configuration.development_mode
@@ -21,21 +25,21 @@ module Horza
21
25
  end
22
26
 
23
27
  def get!(id)
24
- run_and_convert_exceptions { entity_class(@context.find(id).attributes) }
28
+ run_and_convert_exceptions { entity(@context.find(id).attributes) }
25
29
  end
26
30
 
27
31
  def find_first!(options = {})
28
- run_and_convert_exceptions { entity_class(query(options).first!.attributes) }
32
+ run_and_convert_exceptions { entity(query(options).first!.attributes) }
29
33
  end
30
34
 
31
35
  def find_all(options = {})
32
- run_and_convert_exceptions { entity_class(query(options)) }
36
+ run_and_convert_exceptions { entity(query(options)) }
33
37
  end
34
38
 
35
39
  def join(options = {})
36
40
  run_and_convert_exceptions do
37
41
  sql = ArelJoin.sql(self.context, options)
38
- entity_class(::ActiveRecord::Base.connection.exec_query(sql).to_a)
42
+ entity(::ActiveRecord::Base.connection.exec_query(sql).to_a)
39
43
  end
40
44
  end
41
45
 
@@ -43,7 +47,7 @@ module Horza
43
47
  run_and_convert_exceptions do
44
48
  record = @context.new(options)
45
49
  record.save!
46
- entity_class(record.attributes)
50
+ entity(record.attributes)
47
51
  end
48
52
  end
49
53
 
@@ -59,7 +63,7 @@ module Horza
59
63
  record = @context.find(id)
60
64
  record.assign_attributes(options)
61
65
  record.save!
62
- entity_class(record.attributes)
66
+ entity(record.attributes)
63
67
  end
64
68
  end
65
69
 
@@ -82,7 +86,7 @@ module Horza
82
86
  result = walk_family_tree(base, options)
83
87
  return nil unless result
84
88
 
85
- options.target.to_s.plural? ? entity_class(query(options, result)) : entity_class(result.attributes)
89
+ options.target.to_s.plural? ? entity(query(options, result)) : entity(result.attributes)
86
90
  end
87
91
  end
88
92
 
@@ -38,6 +38,14 @@ module Horza
38
38
  def not_implemented_error
39
39
  raise ::Horza::Errors::MethodNotImplemented, 'You must implement this method in your adapter.'
40
40
  end
41
+
42
+ def single_entity_klass
43
+ ::Horza::Entities::Single
44
+ end
45
+
46
+ def collection_entity_klass
47
+ ::Horza::Entities::Collection
48
+ end
41
49
  end
42
50
  end
43
51
  end
@@ -44,8 +44,9 @@ module Horza
44
44
  raise self.class.horza_error_from_orm_error(e.class).new(e.message)
45
45
  end
46
46
 
47
- def entity_class(res = @context)
48
- collection?(res) ? ::Horza::Entities.collection_entity_for(entity_symbol).new(res) : ::Horza::Entities.single_entity_for(entity_symbol).new(res)
47
+ def entity(res = @context)
48
+ collection?(res) ? ::Horza::Entities.collection_entity_for(entity_symbol, res) :
49
+ ::Horza::Entities.single_entity_for(entity_symbol, res)
49
50
  end
50
51
 
51
52
  def entity_symbol
@@ -1,16 +1,18 @@
1
1
  module Horza
2
2
  module Entities
3
3
  class << self
4
- def single_entity_for(entity_symbol)
5
- single_entities[entity_symbol] || ::Horza::Entities::Single
4
+ def single_entity_for(entity_symbol, attributes)
5
+ klass = single_entities[entity_symbol] || Horza.adapter.single_entity_klass
6
+ klass.new(attributes)
6
7
  end
7
8
 
8
9
  def single_entities
9
10
  @singles ||= ::Horza.descendants_map(::Horza::Entities::Single)
10
11
  end
11
12
 
12
- def collection_entity_for(entity_symbol)
13
- collection_entities[entity_symbol] || ::Horza::Entities::Collection
13
+ def collection_entity_for(entity_symbol, attributes)
14
+ klass = collection_entities[entity_symbol] || Horza.adapter.collection_entity_klass
15
+ klass.new(attributes)
14
16
  end
15
17
 
16
18
  def collection_entities
@@ -1,8 +1,8 @@
1
1
  module Horza
2
2
  module Entities
3
3
  class Collection
4
- def initialize(collection, singular_entity = nil)
5
- @collection, @singular_entity = collection, singular_entity
4
+ def initialize(collection)
5
+ @collection = collection
6
6
  end
7
7
 
8
8
  def [](index)
@@ -27,15 +27,11 @@ module Horza
27
27
  end
28
28
  end
29
29
 
30
- def singular_entity(record)
31
- attributes = record.respond_to?(:to_hash) ? record.to_hash : Horza.adapter.new(record).to_hash
32
- singular_entity_class(record).new(attributes)
33
- end
34
-
35
30
  # Collection classes have the form Horza::Entities::Users
36
31
  # Single output requires the form Horza::Entities::User
37
- def singular_entity_class(record)
38
- @singular_entity ||= ::Horza::Entities::single_entity_for(record.class.name.split('::').last.symbolize)
32
+ def singular_entity(record)
33
+ attributes = record.respond_to?(:to_hash) ? record.to_hash : Horza.adapter.new(record).to_hash
34
+ ::Horza::Entities::single_entity_for(record.class.name.split('::').last.symbolize, attributes)
39
35
  end
40
36
  end
41
37
  end
@@ -0,0 +1,17 @@
1
+ require 'active_model/naming'
2
+ require 'active_model/validations'
3
+ require 'active_model/conversion'
4
+
5
+ module Horza
6
+ module Entities
7
+ class SingleWithActiveModel < Single
8
+ include ActiveModel::Validations
9
+ include ActiveModel::Conversion
10
+ extend ActiveModel::Naming
11
+
12
+ def persisted?
13
+ false
14
+ end
15
+ end
16
+ end
17
+ end
@@ -72,7 +72,7 @@ describe Horza do
72
72
  context '#context_for_entity' do
73
73
  context 'when model exists' do
74
74
  it 'Returns correct class' do
75
- expect(Horza.adapter.context_for_entity(:simple_record)).to eq SimpleRecord
75
+ expect(Horza.adapter.context_for_entity(:simple_record).to_s).to eq 'SimpleRecord'
76
76
  end
77
77
  end
78
78
 
@@ -102,7 +102,7 @@ describe Horza do
102
102
 
103
103
  context 'when model exists' do
104
104
  it 'Returns correct class' do
105
- expect(Horza.adapter.context_for_entity(:simple_record)).to eq SimpleRecord
105
+ expect(Horza.adapter.context_for_entity(:simple_record).to_s).to eq 'SimpleRecord'
106
106
  end
107
107
  end
108
108
 
@@ -149,6 +149,7 @@ describe Horza do
149
149
 
150
150
  describe 'Queries' do
151
151
  let(:user) { HorzaSpec::User.create }
152
+ let(:single_entity_klass) { Horza::Entities::Single }
152
153
 
153
154
  describe '#get!' do
154
155
  context 'when user exists' do
@@ -675,13 +676,15 @@ describe Horza do
675
676
  3.times.map { HorzaSpec::User.create }
676
677
  end
677
678
 
679
+ let(:single_entity_klass) { Horza::Entities::SingleWithActiveModel }
680
+
678
681
  subject { Horza::Entities::Collection.new(members) }
679
682
 
680
683
  context '#each' do
681
684
  context 'when name is of ancestry type' do
682
685
  it 'yields a Horza::Entities::Single with each iteration' do
683
686
  subject.each do |member|
684
- expect(member.is_a? Horza::Entities::Single).to be true
687
+ expect(member.is_a? single_entity_klass).to be true
685
688
  end
686
689
  end
687
690
  end
@@ -711,7 +714,7 @@ describe Horza do
711
714
  context 'when name is of ancestry type' do
712
715
  it 'returns the last collection item as a Horza::Entities::Single' do
713
716
  entity = subject.pop
714
- expect(entity.is_a? Horza::Entities::Single).to be true
717
+ expect(entity.is_a? single_entity_klass).to be true
715
718
  end
716
719
  end
717
720
  end
@@ -726,7 +729,7 @@ describe Horza do
726
729
  end
727
730
 
728
731
  it 'returns Horza::Collection::Single' do
729
- expect(TestNamespace::DummyModels.new([]).send(:singular_entity_class, dummy_model)).to eq Horza::Entities::Single
732
+ expect(TestNamespace::DummyModels.new([]).send(:singular_entity, dummy_model).is_a? single_entity_klass).to be true
730
733
  end
731
734
  end
732
735
 
@@ -742,7 +745,7 @@ describe Horza do
742
745
  end
743
746
 
744
747
  it 'returns the existing singular class' do
745
- expect(TestNamespace::OtherDummyModels.new([]).send(:singular_entity_class, other_dummy_model)).to eq TestNamespace::OtherDummyModel
748
+ expect(TestNamespace::OtherDummyModels.new([]).send(:singular_entity, other_dummy_model).is_a? TestNamespace::OtherDummyModel).to be true
746
749
  end
747
750
  end
748
751
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: horza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Turner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-21 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -115,6 +115,7 @@ files:
115
115
  - lib/horza/entities.rb
116
116
  - lib/horza/entities/collection.rb
117
117
  - lib/horza/entities/single.rb
118
+ - lib/horza/entities/single_with_active_model.rb
118
119
  - lib/horza/errors.rb
119
120
  - spec/active_record_spec.rb
120
121
  - spec/horza_spec.rb