transient-model 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.irbrc +1 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/transient_model/base.rb +40 -0
- data/lib/transient_model/dsl/fields.rb +123 -0
- data/lib/transient_model/dsl.rb +9 -0
- data/lib/transient_model/fields/base_field.rb +61 -0
- data/lib/transient_model/fields/standard_field.rb +9 -0
- data/lib/transient_model/fields.rb +10 -0
- data/lib/transient_model/version.rb +5 -0
- data/lib/transient_model.rb +9 -0
- data/transient-model.gemspec +30 -0
- metadata +187 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4834b578d0f364eb5db665ec3801d3d1601cca30
|
4
|
+
data.tar.gz: 2909d3c25af1fb60f5ddeaa00f3c9c3986edd9c7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b49fbbc0fc4d60330b49477f2a78fe42f0f927292c69ea8c16041c0d4616c17d84b9dab007539c2725f5c6d12891d3960ca5d740f2de34d0811b14890aaece8
|
7
|
+
data.tar.gz: 0206f69d925d1ecf5baa99532300a6ab1f9c712f213144cd8f8b03d26df35a14498bbb89d71739d825c15344bddaa2f616425575b7a425325db8285fd9726df1
|
data/.gitignore
ADDED
data/.irbrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'irbtools'
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
transient-model
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Transient::Model
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/transient/model`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'transient-model'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install transient-model
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/transient-model.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "transient/model"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# lib/transient_model/base.rb
|
2
|
+
|
3
|
+
require "transient_model/version"
|
4
|
+
require "transient_model/dsl"
|
5
|
+
require "transient_model/fields"
|
6
|
+
|
7
|
+
module TransientModel
|
8
|
+
class MissingField < StandardError; end
|
9
|
+
|
10
|
+
class Base
|
11
|
+
include ActiveModel::Model
|
12
|
+
include TransientModel::Dsl::Fields
|
13
|
+
# include ActiveModel::Dirty
|
14
|
+
# extend ActiveSupport::Callbacks
|
15
|
+
|
16
|
+
def initialize(attributes = {})
|
17
|
+
hydrate_attributes attributes
|
18
|
+
end
|
19
|
+
|
20
|
+
private # ---------------------------------------------
|
21
|
+
|
22
|
+
def hydrate_attributes(attributes = {})
|
23
|
+
hydrate_attributes! attributes
|
24
|
+
rescue TransientModel::MissingField
|
25
|
+
false
|
26
|
+
end
|
27
|
+
|
28
|
+
def hydrate_attributes!(attributes = {})
|
29
|
+
attributes.each do |key, value|
|
30
|
+
setter_method_name = "#{ key }=".to_sym
|
31
|
+
|
32
|
+
if self.respond_to?(setter_method_name)
|
33
|
+
self.send(setter_method_name, value)
|
34
|
+
else
|
35
|
+
raise MissingField, "the field #{ key } was not found."
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
# lib/transient/model/dsl/fields.rb
|
2
|
+
|
3
|
+
module TransientModel
|
4
|
+
module Dsl
|
5
|
+
module Fields
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
class_attribute :fields
|
10
|
+
self.fields = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
# Add a field to your model.
|
15
|
+
#
|
16
|
+
# @example field :first_name
|
17
|
+
#
|
18
|
+
# @param [ String ] name
|
19
|
+
# @param [ Hash ] options
|
20
|
+
#
|
21
|
+
# @return [ Field ] the field added to the model.
|
22
|
+
# @since 0.1.0
|
23
|
+
##
|
24
|
+
def field(name, options = {})
|
25
|
+
field_name = name.to_s
|
26
|
+
define_field(field_name, options)
|
27
|
+
end
|
28
|
+
|
29
|
+
protected # -------------------------------------
|
30
|
+
|
31
|
+
# Add the new field to the class definition.
|
32
|
+
#
|
33
|
+
# @example
|
34
|
+
#
|
35
|
+
# @param [ String ] name - The name of the field you are adding.
|
36
|
+
# @param [ Hash ] options - The options to be used by this field.
|
37
|
+
#
|
38
|
+
# @return [ TransientModel::Fields::Base ] the field generated
|
39
|
+
# @since 0.1.0
|
40
|
+
##
|
41
|
+
def define_field(name, options = {})
|
42
|
+
field = field_for(name, options)
|
43
|
+
fields[name.to_sym] = field
|
44
|
+
create_accessors name, options
|
45
|
+
field
|
46
|
+
end
|
47
|
+
|
48
|
+
# Create the getter & setter accessors for the field defined.
|
49
|
+
#
|
50
|
+
# @param [ Field ] field - The field we are created accessors for.
|
51
|
+
#
|
52
|
+
# @return [ Boolean ]
|
53
|
+
# @since 0.1.0
|
54
|
+
##
|
55
|
+
def create_accessors(name, options)
|
56
|
+
field = fields[name.to_sym]
|
57
|
+
create_field_getter(field)
|
58
|
+
create_field_setter(field)
|
59
|
+
end
|
60
|
+
|
61
|
+
# Generate a setter method for setting the field value.
|
62
|
+
#
|
63
|
+
# @since 0.1.0
|
64
|
+
##
|
65
|
+
def create_field_setter(field)
|
66
|
+
generated_methods.module_eval do
|
67
|
+
define_method(field.setter_method_name) do |value|
|
68
|
+
current_value = instance_variable_get(field.instance_variable_name)
|
69
|
+
|
70
|
+
# Commenting out until I have time to add dirty tracking.
|
71
|
+
#
|
72
|
+
# self.send "#{ field.name }_will_change!" unless current_value == value
|
73
|
+
|
74
|
+
instance_variable_set(field.instance_variable_name, value)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Generate a getter method for the data object.
|
80
|
+
#
|
81
|
+
# @return []
|
82
|
+
# @since 0.1.0
|
83
|
+
##
|
84
|
+
def create_field_getter(field)
|
85
|
+
generated_methods.module_eval do
|
86
|
+
define_method(field.getter_method_name) do
|
87
|
+
_value = instance_variable_get field.instance_variable_name
|
88
|
+
_value.blank? ? field.default_value : _value
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# We are going to dynamically build all of our setters/getter in a
|
94
|
+
# module that will then be included into the implementing class.
|
95
|
+
#
|
96
|
+
# @return [ Module ] returns the module that was generated.
|
97
|
+
# @since 0.1.0
|
98
|
+
##
|
99
|
+
def generated_methods
|
100
|
+
@generated_methods ||= begin
|
101
|
+
_module = Module.new
|
102
|
+
include(_module)
|
103
|
+
_module
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Dynamically load up the proper field type
|
108
|
+
#
|
109
|
+
# @param [ String ] name - The name of the field you are creating.
|
110
|
+
# @param [ Hash ] options - The options to be used.
|
111
|
+
#
|
112
|
+
# @return [ Field ] The field definition
|
113
|
+
# @since 0.1.0
|
114
|
+
##
|
115
|
+
def field_for(name, options)
|
116
|
+
options.merge!(klass: self)
|
117
|
+
TransientModel::Fields::StandardField.new name, options
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# lib/transient_model/fields/base_field.rb
|
2
|
+
|
3
|
+
module TransientModel
|
4
|
+
module Fields
|
5
|
+
class BaseField
|
6
|
+
attr_accessor :name, :options
|
7
|
+
|
8
|
+
# Initialize a new field object
|
9
|
+
#
|
10
|
+
# @param [ String ] name
|
11
|
+
# @param [ Hash ] options
|
12
|
+
#
|
13
|
+
# @since 0.1.0
|
14
|
+
##
|
15
|
+
def initialize(name, options = {})
|
16
|
+
defaults = {
|
17
|
+
type: String
|
18
|
+
}
|
19
|
+
|
20
|
+
@name = name
|
21
|
+
@options = defaults.merge options
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return the default value that should be used for the field.
|
25
|
+
#
|
26
|
+
# @return [ any ] returns the default value set for the field.
|
27
|
+
# @since 0.1.0
|
28
|
+
##
|
29
|
+
def default_value
|
30
|
+
@options.fetch(:default, nil)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return the data type for this field.
|
34
|
+
#
|
35
|
+
# @return [ any ]
|
36
|
+
# @since 0.1.0
|
37
|
+
##
|
38
|
+
def type
|
39
|
+
@type ||= options.fetch(:type, String)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Return the symbol representation of the instance variable
|
43
|
+
# used by the defining model object.
|
44
|
+
#
|
45
|
+
# @return [ Symbol ] the instance variable name of the field
|
46
|
+
# @since 0.1.0
|
47
|
+
##
|
48
|
+
def instance_variable_name
|
49
|
+
"@#{ @name }".to_sym
|
50
|
+
end
|
51
|
+
|
52
|
+
def getter_method_name
|
53
|
+
"#{ @name }".to_sym
|
54
|
+
end
|
55
|
+
|
56
|
+
def setter_method_name
|
57
|
+
"#{ @name }=".to_sym
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'transient_model/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "transient-model"
|
8
|
+
spec.version = TransientModel::VERSION
|
9
|
+
spec.authors = ["JD Hendrickson"]
|
10
|
+
spec.email = ["jd@digitalopera.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Provides a simple DSL for defining non-persistent models}
|
13
|
+
spec.description = %q{Provides a simple DSL for defining non-persistent models}
|
14
|
+
# spec.homepage = "TODO: Put your gem's website or public repo URL here."
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
+
spec.add_development_dependency "simplecov", "~> 0.11.1"
|
25
|
+
spec.add_development_dependency "irbtools"
|
26
|
+
spec.add_development_dependency "byebug"
|
27
|
+
|
28
|
+
spec.add_dependency 'activemodel', '>= 4.0.0', '< 6.0.0'
|
29
|
+
spec.add_dependency 'activesupport', '>= 4.0.0', '< 6.0.0'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: transient-model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- JD Hendrickson
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.11.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.11.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: irbtools
|
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: byebug
|
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
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activemodel
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 4.0.0
|
104
|
+
- - "<"
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: 6.0.0
|
107
|
+
type: :runtime
|
108
|
+
prerelease: false
|
109
|
+
version_requirements: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: 4.0.0
|
114
|
+
- - "<"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 6.0.0
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: activesupport
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 4.0.0
|
124
|
+
- - "<"
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 6.0.0
|
127
|
+
type: :runtime
|
128
|
+
prerelease: false
|
129
|
+
version_requirements: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 4.0.0
|
134
|
+
- - "<"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: 6.0.0
|
137
|
+
description: Provides a simple DSL for defining non-persistent models
|
138
|
+
email:
|
139
|
+
- jd@digitalopera.com
|
140
|
+
executables: []
|
141
|
+
extensions: []
|
142
|
+
extra_rdoc_files: []
|
143
|
+
files:
|
144
|
+
- ".gitignore"
|
145
|
+
- ".irbrc"
|
146
|
+
- ".rspec"
|
147
|
+
- ".ruby-gemset"
|
148
|
+
- ".ruby-version"
|
149
|
+
- ".travis.yml"
|
150
|
+
- Gemfile
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- bin/console
|
154
|
+
- bin/setup
|
155
|
+
- lib/transient_model.rb
|
156
|
+
- lib/transient_model/base.rb
|
157
|
+
- lib/transient_model/dsl.rb
|
158
|
+
- lib/transient_model/dsl/fields.rb
|
159
|
+
- lib/transient_model/fields.rb
|
160
|
+
- lib/transient_model/fields/base_field.rb
|
161
|
+
- lib/transient_model/fields/standard_field.rb
|
162
|
+
- lib/transient_model/version.rb
|
163
|
+
- transient-model.gemspec
|
164
|
+
homepage:
|
165
|
+
licenses: []
|
166
|
+
metadata: {}
|
167
|
+
post_install_message:
|
168
|
+
rdoc_options: []
|
169
|
+
require_paths:
|
170
|
+
- lib
|
171
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
requirements: []
|
182
|
+
rubyforge_project:
|
183
|
+
rubygems_version: 2.4.5.1
|
184
|
+
signing_key:
|
185
|
+
specification_version: 4
|
186
|
+
summary: Provides a simple DSL for defining non-persistent models
|
187
|
+
test_files: []
|