entasis 0.5.0.rc1 → 0.5.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,6 +2,7 @@ entasis
2
2
  =======
3
3
 
4
4
  [![Build Status](https://secure.travis-ci.org/ingemar/entasis.png)](http://travis-ci.org/ingemar/entasis)
5
+ [![Code Climate](https://codeclimate.com/github/ingemar/entasis.png)](https://codeclimate.com/github/ingemar/entasis)
5
6
 
6
7
  Entasis provides a few neat methods for building a basic class. Handy for models without a database.
7
8
 
@@ -11,6 +12,8 @@ Example:
11
12
  class Person
12
13
  include Entasis::Model
13
14
 
15
+ has_many :friends
16
+
14
17
  attributes :name, :age, :city
15
18
 
16
19
  def age=(years)
@@ -18,9 +21,22 @@ class Person
18
21
  end
19
22
  end
20
23
 
21
- hilda = Person.new(name: 'Hilda', age: '23', city: 'Stockholm')
22
- hilda.attribute_names # => ["name", "age", "city"]
23
- hilda.attributes # => {"name"=>"Hilda", "age"=>23, "city"=>"Stockholm"}
24
+ class Friend
25
+ include Entasis::Model
26
+
27
+ belongs_to :best_friend, class: 'Person'
28
+
29
+ attributes :name
30
+ end
31
+
32
+
33
+ hilda = Person.new(name: 'Hilda', age: '23', city: 'Stockholm', friends: [{ name: 'Emma' }, { name: 'Johan' }])
34
+
35
+ hilda.attribute_names # => ["name", "age", "city"]
36
+ hilda.attributes # => {"name"=>"Hilda", "age"=>23, "city"=>"Stockholm"}
37
+
38
+ hilda.friends # => [#<Friend:0x0 @name="Emma">, #<Friend:0x1 @name="Johan">]
39
+ hilda.friends[0].best_friend == hilda # => true
24
40
 
25
41
  anon = Person.new
26
42
  anon.valid? # => false
@@ -44,7 +60,8 @@ Contributors
44
60
  ------------
45
61
 
46
62
  - Ingemar Edsborn (ingemar)
47
- - Joshua Davey (jgdavey)
48
- - Johnny Winn (nurugger07)
63
+ - Gabriel Reis (greis)
49
64
  - Jack Christensen (jackc)
65
+ - Johnny Winn (nurugger07)
66
+ - Joshua Davey (jgdavey)
50
67
 
data/lib/entasis/model.rb CHANGED
@@ -7,6 +7,7 @@ module Entasis
7
7
  include ActiveModel::Validations
8
8
  class_attribute :attribute_names, :attributes_config, :belongings, instance_writer: false
9
9
 
10
+ self.belongings ||= {}
10
11
  self.attribute_names ||= []
11
12
  self.class_eval 'class UnknownAttributeError < StandardError; end'
12
13
  end
@@ -25,7 +25,7 @@ module Entasis
25
25
  options = relations.last.is_a?(Hash) ? relations.pop : {}
26
26
 
27
27
  relations.each do |relation|
28
- relation_model = (options[:class] || relation).to_s.singularize.camelize.constantize
28
+ relation_model = options[:class] || relation.to_s.singularize.camelize
29
29
 
30
30
  self.class_eval <<-RUBY
31
31
  def #{relation}=(resources)
@@ -36,8 +36,8 @@ module Entasis
36
36
  resource = #{relation_model}.new resource
37
37
  end
38
38
 
39
- if resource.belongs_to? self.class
40
- resource.send "\#{self.class.name.underscore}=", self
39
+ if inverse_relation = resource.belongings[self.class.to_s]
40
+ resource.send "\#{inverse_relation}=", self
41
41
  end
42
42
 
43
43
  @#{relation} << resource
@@ -55,18 +55,21 @@ module Entasis
55
55
  # To complete the inverse relationship and tie two models togeter
56
56
  # specify the +belongs_to+ relationship.
57
57
  #
58
+ # Last argument can be an options hash:
59
+ #
60
+ # class: provide a custom class name
61
+ #
58
62
  def belongs_to(*relations)
59
63
  options = relations.last.is_a?(Hash) ? relations.pop : {}
60
64
 
61
- self.belongings ||= []
62
- self.belongings += relations.map(&:to_s).sort
65
+ relations.each do |relation|
66
+ relation_model = options[:class] || relation.to_s.singularize.camelize
67
+
68
+ self.belongings[relation_model] = relation
69
+ end
63
70
 
64
71
  attr_accessor *relations
65
72
  end
66
73
  end
67
-
68
- def belongs_to?(model)
69
- self.belongings.include? model.to_s.underscore
70
- end
71
74
  end
72
75
  end
@@ -1,3 +1,3 @@
1
1
  module Entasis
2
- VERSION = "0.5.0.rc1"
2
+ VERSION = "0.5.0.rc2"
3
3
  end
@@ -5,12 +5,14 @@ describe Entasis::Relations do
5
5
  {
6
6
  name: 'Volvo',
7
7
  people: [{ name: 'Aslog' }],
8
- passengers: [{ name: 'Gunnar' }]
8
+ passengers: [{ name: 'Gunnar' }],
9
+ destinations: [{ name: 'Stockholm' }]
9
10
  }
10
11
  end
11
12
  let(:car) { Car.new }
12
13
  let(:person) { car.people.first }
13
14
  let(:passenger) { car.passengers.first }
15
+ let(:destination) { car.destinations.first }
14
16
 
15
17
  describe '#attributes=' do
16
18
  before { car.attributes = attributes }
@@ -31,5 +33,18 @@ describe Entasis::Relations do
31
33
  expect(passenger.car).to eq(car)
32
34
  end
33
35
  end
36
+
37
+ context 'subclasses' do
38
+ it 'creates a new instance of the relation' do
39
+ expect(destination).to be_a(Car::Destination)
40
+ expect(destination.name).to eq('Stockholm')
41
+ end
42
+ end
43
+
44
+ context 'when belongs_to was given a custom class name' do
45
+ it 'sets the correct relation' do
46
+ expect(destination.transportation).to eq(car)
47
+ end
48
+ end
34
49
  end
35
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: entasis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0.rc1
4
+ version: 0.5.0.rc2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors: