active_entity 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47be0a7e04d9be58924ae62eb75767f415e87e89
4
+ data.tar.gz: c00c8b1651220fc744e68f0fc9097ab71505df2f
5
+ SHA512:
6
+ metadata.gz: 8014240c2bac204b34fbecef8cbc3f31ad4f2c353bd7d3573d89397902d1db56bf0e5c3ecdfe58eaa52a7e006b1f64608ea7a545eaf1ee92eaff0b1de7c0dd12
7
+ data.tar.gz: fdd4e6a0b67f756dece737e2076cf496dd02021412ff2156f8a6eda88cf5d39f3c967500b1f11b2282271ac1b9bd505bef75bdc276da00f98d594a082c37f3eb
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_entity.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Taiki ONO
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # ActiveEntity
2
+
3
+ To make an entity with ease according to ActiveModel way.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_entity'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_entity
20
+
21
+ ## Synopsis
22
+
23
+ ```ruby
24
+ class Message
25
+ include ActiveEntity::Entity
26
+
27
+ attribute :title
28
+ attribute :body
29
+
30
+ identity_attribute :title
31
+
32
+ validates :title, presence: true, length: { maximum: 255 }
33
+ validates :body, presence: true
34
+ end
35
+
36
+ message = Message.new(title: 'A README of ActiveEntity')
37
+ message.valid? #=> false
38
+ message.errors #=> returns a ActiveModel::Errors
39
+
40
+ message = Message.new(title: 'A README of ActiveEntity', body: 'No contents!')
41
+ message.valid? #=> true
42
+ message.attributes #=> { title: "A README of ActiveEntity", body: "No contents!" }
43
+
44
+ another_messsage = Message.new(title: 'A README of ActiveEntity', body: '')
45
+ message == another_messsage #=> true
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/taiki45/active_entity/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_entity/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_entity"
8
+ spec.version = ActiveEntity::VERSION
9
+ spec.authors = ["Taiki ONO"]
10
+ spec.email = ["taiki-ono@cookpad.com"]
11
+ spec.summary = %q{To make an entity with ease according to ActiveModel way.}
12
+ spec.description = spec.summary
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activemodel", ">= 4.0"
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.1"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "pry-doc"
27
+ end
@@ -0,0 +1,9 @@
1
+ require 'active_model'
2
+ require 'active_support/concern'
3
+
4
+ require 'active_entity/version'
5
+ require 'active_entity/attribute'
6
+ require 'active_entity/identity'
7
+ require 'active_entity/strict_assignment'
8
+ require 'active_entity/entity'
9
+ require 'active_entity/mutability'
@@ -0,0 +1,32 @@
1
+ module ActiveEntity
2
+ module Attribute
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :defined_attributes,
7
+ instance_writer: false, instance_predicate: false
8
+ self.defined_attributes = {}
9
+ end
10
+
11
+ class_methods do
12
+ def attribute(name, options = {})
13
+ defined_attributes[name] = options
14
+ attr_reader(name)
15
+ end
16
+ end
17
+
18
+ def initialize(attrs = {})
19
+ attrs.each do |attr, value|
20
+ instance_variable_set(:"@#{attr}", value)
21
+ end if attrs.respond_to?(:each)
22
+
23
+ super()
24
+ end
25
+
26
+ def attributes
27
+ @_attributes ||= begin
28
+ Hash[defined_attributes.keys.map {|name| [name, public_send(name)] }]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ module ActiveEntity
2
+ module Entity
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include Attribute
7
+ include StrictAssignment
8
+ include Identity
9
+ include ActiveModel::Validations
10
+ include ActiveModel::Serialization
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module ActiveEntity
2
+ module Identity
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :identity_attributes,
7
+ instance_writer: false, instance_predicate: false
8
+ self.identity_attributes = []
9
+ end
10
+
11
+ class_methods do
12
+ def identity_attribute(*names)
13
+ identity_attributes.concat(names.flatten)
14
+
15
+ define_method(:==) do |other|
16
+ return false unless self.class === other
17
+
18
+ names.all? do |name|
19
+ public_send(name) == other.public_send(name)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ # For ActiveModel::Conversion
26
+ def to_key
27
+ indentity_attributes.empty? ? nil : identity_attributes
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module ActiveEntity
2
+ module Mutability
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ attr_writer(*defined_attributes.keys)
7
+ end
8
+
9
+ class_methods do
10
+ def attribute(name, option = {})
11
+ super
12
+ attr_writer(name)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveEntity
2
+ module StrictAssignment
3
+ extend ActiveSupport::Concern
4
+
5
+ def initialize(attrs = {})
6
+ keys = attrs.keys.map(&:to_sym) - defined_attributes.keys
7
+
8
+ unless keys.empty?
9
+ raise ArgumentError.new("Invalid assignments on: #{keys}")
10
+ end
11
+
12
+ super
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveEntity
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveEntity::Attribute do
4
+ let(:test_class) do
5
+ Class.new do
6
+ include ActiveEntity::Attribute
7
+
8
+ attribute :name
9
+ attribute :age
10
+ end
11
+ end
12
+
13
+ let(:test_attributes) { { name: 'Alice', age: 1 } }
14
+
15
+ describe '.defined_attributes' do
16
+ subject { test_class.defined_attributes }
17
+ it { is_expected.to be_kind_of(Hash) }
18
+
19
+ it 'returns all defined attributes' do
20
+ is_expected.to eq(name: {}, age: {})
21
+ end
22
+ end
23
+
24
+ describe '#initialize' do
25
+ subject { test_class.new(test_attributes) }
26
+
27
+ it 'assigns attributes' do
28
+ expect(subject.name).to eq('Alice')
29
+ expect(subject.age).to eq(1)
30
+ end
31
+ end
32
+
33
+ describe '#attributes' do
34
+ subject { test_class.new(test_attributes).attributes }
35
+
36
+ it { is_expected.to be_kind_of(Hash) }
37
+
38
+ it "returns all attribute's name-value pairs" do
39
+ is_expected.to eq(name: 'Alice', age: 1)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveEntity::Identity do
4
+ let(:test_class) do
5
+ Class.new do
6
+ include ActiveEntity::Identity
7
+
8
+ attr_reader :name, :age
9
+ identity_attribute :name, :age
10
+
11
+ def initialize(name, age)
12
+ @name = name
13
+ @age = age
14
+ end
15
+ end
16
+ end
17
+
18
+ describe '.identity_attributes' do
19
+ subject { test_class.identity_attributes }
20
+ it { is_expected.to eq([:name, :age]) }
21
+ end
22
+
23
+ describe '#==' do
24
+ subject { a == b }
25
+ let(:a) { test_class.new('Alice', 1) }
26
+
27
+ context 'with same entity' do
28
+ let(:b) { test_class.new('Alice', 1) }
29
+ it { is_expected.to be_truthy }
30
+ end
31
+
32
+ context 'with different entity' do
33
+ context 'and it has all different attributes' do
34
+ let(:b) { test_class.new('Bob', 2) }
35
+ it { is_expected.to be_falsey }
36
+ end
37
+
38
+ context 'and it has some of different attributes' do
39
+ let(:b) { test_class.new('Alice', 2) }
40
+ it { is_expected.to be_falsey }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveEntity::Mutability do
4
+ subject { test_class.new(x: 1) }
5
+
6
+ context 'when included after attribute definitions' do
7
+ before { test_class.send(:include, ActiveEntity::Mutability) }
8
+
9
+ let(:test_class) do
10
+ Class.new do
11
+ include ActiveEntity::Attribute
12
+ attribute :x
13
+ end
14
+ end
15
+
16
+ it 'accepts attribute assign' do
17
+ expect { subject.x = 0 }.not_to raise_error
18
+ expect(subject.x).to eq(0)
19
+ end
20
+ end
21
+
22
+ context 'when included before attribute definitions' do
23
+ let(:test_class) do
24
+ Class.new do
25
+ include ActiveEntity::Attribute
26
+ include ActiveEntity::Mutability
27
+ attribute :x
28
+ end
29
+ end
30
+
31
+ it 'accepts attribute assign' do
32
+ expect { subject.x = 0 }.not_to raise_error
33
+ expect(subject.x).to eq(0)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveEntity::StrictAssignment do
4
+ let(:test_class) do
5
+ Class.new do
6
+ include ActiveEntity::Attribute
7
+ include ActiveEntity::StrictAssignment
8
+
9
+ attribute :name
10
+ end
11
+ end
12
+
13
+ describe '#initialize' do
14
+ context 'with valid mass assignment' do
15
+ it 'does not raise anything' do
16
+ expect { test_class.new(name: 'Alice') }.not_to raise_error
17
+ end
18
+ end
19
+
20
+ context 'with forbidden mass assignment' do
21
+ it 'raises ArgumentError' do
22
+ expect { test_class.new(x: 'x') }.to raise_error(ArgumentError)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ require 'pry'
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'active_entity'
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with :rspec do |expectations|
9
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
10
+ end
11
+
12
+ config.mock_with :rspec do |mocks|
13
+ mocks.verify_partial_doubles = true
14
+ end
15
+
16
+ config.filter_run :focus
17
+ config.run_all_when_everything_filtered = true
18
+
19
+ config.disable_monkey_patching!
20
+
21
+ if config.files_to_run.one?
22
+ config.default_formatter = 'doc'
23
+ end
24
+
25
+ config.order = :random
26
+ Kernel.srand config.seed
27
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_entity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Taiki ONO
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-doc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: To make an entity with ease according to ActiveModel way.
98
+ email:
99
+ - taiki-ono@cookpad.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - active_entity.gemspec
111
+ - lib/active_entity.rb
112
+ - lib/active_entity/attribute.rb
113
+ - lib/active_entity/entity.rb
114
+ - lib/active_entity/identity.rb
115
+ - lib/active_entity/mutability.rb
116
+ - lib/active_entity/strict_assignment.rb
117
+ - lib/active_entity/version.rb
118
+ - spec/active_entity/attribute_spec.rb
119
+ - spec/active_entity/identity_spec.rb
120
+ - spec/active_entity/mutability_spec.rb
121
+ - spec/active_entity/strict_assignment_spec.rb
122
+ - spec/spec_helper.rb
123
+ homepage: ''
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.2.2
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: To make an entity with ease according to ActiveModel way.
147
+ test_files:
148
+ - spec/active_entity/attribute_spec.rb
149
+ - spec/active_entity/identity_spec.rb
150
+ - spec/active_entity/mutability_spec.rb
151
+ - spec/active_entity/strict_assignment_spec.rb
152
+ - spec/spec_helper.rb
153
+ has_rdoc: