forged_model 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +1 -0
- data/forged_model.gemspec +26 -0
- data/lib/forged_model/model.rb +51 -0
- data/lib/forged_model/version.rb +3 -0
- data/lib/forged_model.rb +7 -0
- data/spec/forged_model/model_spec.rb +74 -0
- data/spec/spec_helper.rb +20 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fb204a9f577830f252c3dc65d22af04b712679dc
|
4
|
+
data.tar.gz: 95b06993ad876c18bd8c08dcea522bbfdd48b007
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e57117ccbeebcc46452d0f3ec021ee680281d940ab0930bd9170c56d6041fb3f0e01aa8a18b39d21f7f6bf9661ac76bb59012013f72270d64f38d1f8387eb8df
|
7
|
+
data.tar.gz: 078ad3686359c81f64dcf2aef29d92991f48f612da192cd742a0faf25cac068b4bc0b4836ca600b4ce2aa6e352e2308bb292a8eebd15c0cefa4ac9d0fc7e8e92
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
forged_model
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Konstantin Munteanu
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# ForgedModel
|
2
|
+
|
3
|
+
Ruby model enriched with some activemodel features.
|
4
|
+
|
5
|
+
Includes `ActiveModel::Model`, `ActiveModel::Dirty` and `ActiveModel::Serialization`.
|
6
|
+
Adds a generator for instance attributes.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'fake_model'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install fake_model
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Just subclass `ForgedModel::Model` and use the attribute generator, like so:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
Dummy < ForgedModel::Model
|
28
|
+
define_attributes :foo
|
29
|
+
end
|
30
|
+
|
31
|
+
dummy = Dummy.new(foo: "bar")
|
32
|
+
dummy.foo = "other"
|
33
|
+
dummy.foo_was # => "bar"
|
34
|
+
# ...
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'forged_model/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "forged_model"
|
8
|
+
spec.version = ForgedModel::VERSION
|
9
|
+
spec.authors = ["Konstantin Munteanu"]
|
10
|
+
spec.email = ["konstantin@munteanu.de"]
|
11
|
+
spec.description = %q{Ruby model enriched with some activemodel features}
|
12
|
+
spec.summary = %q{Includes ActiveModel::Model, Dirty and Serialization and adds an generator for attributes}
|
13
|
+
spec.homepage = "https://github.com/mkon/forged_model"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
26
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "active_model"
|
2
|
+
|
3
|
+
module ForgedModel
|
4
|
+
class Model
|
5
|
+
include ActiveModel::Model
|
6
|
+
include ActiveModel::Dirty
|
7
|
+
include ActiveModel::Serialization
|
8
|
+
|
9
|
+
class_attribute :attribute_list
|
10
|
+
self.attribute_list = []
|
11
|
+
|
12
|
+
class << self
|
13
|
+
|
14
|
+
def define_attributes(*methods)
|
15
|
+
self.attribute_list += methods
|
16
|
+
attr_reader *methods
|
17
|
+
methods.each do |method|
|
18
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
19
|
+
def #{method}=(value)
|
20
|
+
#{method}_will_change!
|
21
|
+
@#{method} = value
|
22
|
+
end
|
23
|
+
EOS
|
24
|
+
end
|
25
|
+
define_attribute_methods *methods
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(params={})
|
31
|
+
load(params)
|
32
|
+
changed_attributes.clear
|
33
|
+
super()
|
34
|
+
end
|
35
|
+
|
36
|
+
def attributes
|
37
|
+
Hash[self.class.attribute_list \
|
38
|
+
.select{ |name| instance_variable_defined? "@#{name}" } \
|
39
|
+
.map { |name, _| [name, self.send(name)] }]
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
def load(params)
|
45
|
+
params.each do |attr, value|
|
46
|
+
self.public_send("#{attr}=", value)
|
47
|
+
end if params
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/lib/forged_model.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ForgedModel::Model do
|
4
|
+
|
5
|
+
before { @Dummy = Class.new(ForgedModel::Model) }
|
6
|
+
|
7
|
+
describe "self.define_attributes" do
|
8
|
+
|
9
|
+
before { @Dummy.define_attributes :foo }
|
10
|
+
|
11
|
+
it "creates the public methods" do
|
12
|
+
dummy = @Dummy.new
|
13
|
+
expect(dummy).to respond_to(:foo)
|
14
|
+
expect(dummy).to respond_to(:foo=)
|
15
|
+
dummy.foo = "value"
|
16
|
+
expect(dummy.foo).to eq("value")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "allows subclasses to inherit attributes" do
|
20
|
+
SubDummy = Class.new(@Dummy)
|
21
|
+
SubDummy.define_attributes :bar
|
22
|
+
dummy = SubDummy.new
|
23
|
+
expect(dummy).to respond_to(:foo)
|
24
|
+
expect(dummy).to respond_to(:bar)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "initialize" do
|
30
|
+
|
31
|
+
it "correctly assigns the attributes" do
|
32
|
+
@Dummy.define_attributes :foo
|
33
|
+
dummy = @Dummy.new(foo: "bar")
|
34
|
+
expect(dummy.foo).to eq("bar")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "raises an error if an attribute does not exist" do
|
38
|
+
expect {
|
39
|
+
dummy = @Dummy.new(notexisting: "something")
|
40
|
+
}.to raise_error(NoMethodError)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "attributes" do
|
46
|
+
|
47
|
+
before { @Dummy.define_attributes :foo, :bar }
|
48
|
+
|
49
|
+
it "returns a hash of attribute key/value pairs" do
|
50
|
+
dummy = @Dummy.new(foo: "value1", bar: "value2")
|
51
|
+
expect(dummy.attributes).to eq(foo: "value1", bar: "value2")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "only includes set attributes" do
|
55
|
+
dummy = @Dummy.new(foo: "value1")
|
56
|
+
expect(dummy.attributes).to_not include(:bar)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "attribute tracking" do
|
62
|
+
|
63
|
+
it "correctly keeps track of changes" do
|
64
|
+
@Dummy.define_attributes :foo
|
65
|
+
dummy = @Dummy.new(foo: "value1")
|
66
|
+
expect(dummy.changed_attributes).to be_empty
|
67
|
+
dummy.foo = "value2"
|
68
|
+
expect(dummy.changed_attributes.key? "foo").to be_true
|
69
|
+
expect(dummy.foo_was).to eq("value1")
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require "forged_model"
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: forged_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Konstantin Munteanu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-29 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.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
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: '2.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.14'
|
69
|
+
description: Ruby model enriched with some activemodel features
|
70
|
+
email:
|
71
|
+
- konstantin@munteanu.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".ruby-gemset"
|
79
|
+
- ".ruby-version"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- forged_model.gemspec
|
85
|
+
- lib/forged_model.rb
|
86
|
+
- lib/forged_model/model.rb
|
87
|
+
- lib/forged_model/version.rb
|
88
|
+
- spec/forged_model/model_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: https://github.com/mkon/forged_model
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.1
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Includes ActiveModel::Model, Dirty and Serialization and adds an generator
|
114
|
+
for attributes
|
115
|
+
test_files:
|
116
|
+
- spec/forged_model/model_spec.rb
|
117
|
+
- spec/spec_helper.rb
|