intrinsic 1.0.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/example/default_values.rb +18 -0
- data/example/sample.rb +38 -0
- data/intrinsic.gemspec +23 -0
- data/lib/intrinsic.rb +29 -0
- data/lib/intrinsic/extrinsicism.rb +4 -0
- data/lib/intrinsic/intrinsicism.rb +42 -0
- data/lib/intrinsic/intrinsicism/coercion.rb +8 -0
- data/lib/intrinsic/intrinsicism/coercion/integer.rb +7 -0
- data/lib/intrinsic/intrinsicism/coercion/proc.rb +7 -0
- data/lib/intrinsic/intrinsicism/coercion/string.rb +7 -0
- data/lib/intrinsic/intrinsicism/validation.rb +43 -0
- data/lib/intrinsic/version.rb +3 -0
- data/test/helper.rb +35 -0
- data/test/unit/test_intrinsicism.rb +7 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kurtis Rainbolt-Greene
|
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,29 @@
|
|
1
|
+
# Intrinsic
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'intrinsic'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install intrinsic
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'intrinsic'
|
2
|
+
|
3
|
+
class Page
|
4
|
+
include Intrinsic
|
5
|
+
|
6
|
+
property :title, String
|
7
|
+
property :slug, Proc, default: ->(page) { page.title.downcase.gsub ' ', '-' }
|
8
|
+
property :views, Integer, default: 0
|
9
|
+
end
|
10
|
+
|
11
|
+
p page = Page.new.title("Little Red Ridding Hood")
|
12
|
+
# => #<Page:2157011700 title="Little Red Ridding Hood", slug=#<Proc:0x0000010122c818@example/default_values.rb:7 (lambda)>, views=0>
|
13
|
+
p page.slug
|
14
|
+
# => "little-red-ridding-hood"
|
15
|
+
p page.slug ->(page) { page.title.upcase.gsub ' ', '-' }
|
16
|
+
# => #<Page:2156971000 title="Little Red Ridding Hood", slug=#<Proc:0x00000101217e68@example/default_values.rb:15 (lambda)>, views=0>
|
17
|
+
p page.slug
|
18
|
+
# => "LITTLE-RED-RIDDING-HOOD"
|
data/example/sample.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'intrinsic'
|
2
|
+
|
3
|
+
class Person
|
4
|
+
include Intrinsic
|
5
|
+
include Intrinsic::Intrinsicism::Validation
|
6
|
+
|
7
|
+
property :name, String
|
8
|
+
property :email, String
|
9
|
+
property :age, Integer, default: 13
|
10
|
+
|
11
|
+
validation_for :name do
|
12
|
+
name.match /\w/
|
13
|
+
end
|
14
|
+
|
15
|
+
validation_for :age do
|
16
|
+
(13..100).include? age
|
17
|
+
end
|
18
|
+
|
19
|
+
validation_for :email do
|
20
|
+
email.count('@') == 1 and email.length < 256 and email.length > 5
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
person = Person.new name: "Kurtis"
|
26
|
+
# => #<Person:2152879000 name="Kurtis", email=nil, age=13>
|
27
|
+
|
28
|
+
p person.name("Hurly Burly").age("34")
|
29
|
+
# => #<Person:2153464580 name="Hurly Burly", age=34>
|
30
|
+
|
31
|
+
p person.name
|
32
|
+
# => "Hurly Burly"
|
33
|
+
|
34
|
+
p person.is_valid?
|
35
|
+
# => false
|
36
|
+
|
37
|
+
p person.errors
|
38
|
+
# => ["email is not valid"]
|
data/intrinsic.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../lib/intrinsic/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Kurtis Rainbolt-Greene"]
|
6
|
+
gem.email = ["kurtisrainboltgreene@gmail.com"]
|
7
|
+
gem.description = "Intrinsic adds properties to your objects"
|
8
|
+
gem.summary = %q{
|
9
|
+
Intrinsic adds properties to your objects
|
10
|
+
}
|
11
|
+
gem.homepage = "http://krainboltgreene.github.com/intrinsic"
|
12
|
+
|
13
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
gem.name = "intrinsic"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = Intrinsic::VERSION
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rake', '0.9.2.2'
|
21
|
+
gem.add_development_dependency 'yard', '0.7.4'
|
22
|
+
end
|
23
|
+
|
data/lib/intrinsic.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'intrinsic/version'
|
2
|
+
require 'intrinsic/intrinsicism'
|
3
|
+
require 'intrinsic/extrinsicism'
|
4
|
+
|
5
|
+
module Intrinsic
|
6
|
+
def self.included(subject)
|
7
|
+
super
|
8
|
+
subject.extend Intrinsicism
|
9
|
+
subject.send :include, Intrinsicism
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(values = {})
|
13
|
+
@properties = {}.merge self.class.defaults
|
14
|
+
check_properties_of values
|
15
|
+
values.each { |property, value| send property.to_sym, value }
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
"#<#{self.class}:#{self.object_id} #{properties.map{|k,v| k.to_s + '=' + v.inspect}.join(', ')}>"
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def check_properties_of(values)
|
25
|
+
raise ArgumentError unless values.each_key.all? do |property|
|
26
|
+
self.class.properties.include? property
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'intrinsicism/coercion'
|
2
|
+
require_relative 'intrinsicism/validation'
|
3
|
+
|
4
|
+
module Intrinsic
|
5
|
+
module Intrinsicism
|
6
|
+
|
7
|
+
attr_reader :properties, :defaults
|
8
|
+
|
9
|
+
def property(name, type = String, options = {})
|
10
|
+
@properties ||= []
|
11
|
+
@defaults ||= {}
|
12
|
+
check_types_for name, type, options
|
13
|
+
define_method name, property_block(name, type)
|
14
|
+
@properties << name
|
15
|
+
@defaults[name.to_sym] = options[:default]
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def check_types_for(name, type, options)
|
20
|
+
raise TypeError unless name.is_a? Symbol
|
21
|
+
raise TypeError unless Intrinsic.const_get(type.name.to_sym).is_a? Class
|
22
|
+
raise TypeError unless options.is_a? Hash
|
23
|
+
end
|
24
|
+
|
25
|
+
def property_block(name, type)
|
26
|
+
->(value = nil) do
|
27
|
+
unless value.nil?
|
28
|
+
@properties[name.to_sym] = Coercion.const_get(type.name.to_sym).convert value
|
29
|
+
self.instance_variable_set :"@#{name.to_sym}", value
|
30
|
+
self
|
31
|
+
else
|
32
|
+
value = @properties[name.to_sym]
|
33
|
+
unless value.is_a? Proc
|
34
|
+
value
|
35
|
+
else
|
36
|
+
value.call(self)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Intrinsic::Intrinsicism
|
2
|
+
module Validation
|
3
|
+
|
4
|
+
attr_reader :validators, :errors
|
5
|
+
|
6
|
+
def self.included(subject)
|
7
|
+
super
|
8
|
+
subject.extend Validation
|
9
|
+
end
|
10
|
+
|
11
|
+
def validation_for(property, &block)
|
12
|
+
check_for_property property
|
13
|
+
add_validators property, block
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_valid?(property = nil)
|
17
|
+
self.class.validators.all? { |property, validator| validate property, validator }
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_validators(property = nil, block = nil)
|
21
|
+
@validators ||= {}
|
22
|
+
@validators[property] = block
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def validate(property, validator)
|
27
|
+
begin
|
28
|
+
validation_result = instance_eval &validator
|
29
|
+
rescue
|
30
|
+
validation_result = false
|
31
|
+
end
|
32
|
+
|
33
|
+
unless validation_result
|
34
|
+
@errors ||= []
|
35
|
+
@errors << "#{property} is not valid"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def check_for_property(property)
|
40
|
+
raise TypeError unless properties.include? property
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'intrinsic'
|
3
|
+
|
4
|
+
|
5
|
+
class BasicPerson
|
6
|
+
include Intrinsic
|
7
|
+
|
8
|
+
property :name, String
|
9
|
+
property :email, String
|
10
|
+
property :age, Integer, default: 13
|
11
|
+
end
|
12
|
+
|
13
|
+
class EmbeddedPerson
|
14
|
+
include Intrinsic
|
15
|
+
|
16
|
+
property :name, String
|
17
|
+
property :email, String
|
18
|
+
property :age, Integer, default: 13
|
19
|
+
|
20
|
+
embeds :skills { |skill|
|
21
|
+
skill.property :name
|
22
|
+
skill.property :rank, Integer, default: 0 }
|
23
|
+
end
|
24
|
+
|
25
|
+
class ValidPerson
|
26
|
+
include Intrinsic
|
27
|
+
|
28
|
+
property :name, String
|
29
|
+
property :email, String
|
30
|
+
property :age, Integer, default: 13
|
31
|
+
|
32
|
+
validation_for :name { |name| !name.blank? && name.match =~ /\w+|\,+|\.+|\-+|\s+/ }
|
33
|
+
validation_for :email { |email| !email.blank? }
|
34
|
+
validation_for :age { |age| (13..90).include? age }
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: intrinsic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kurtis Rainbolt-Greene
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &2157841500 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - =
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.2.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2157841500
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yard
|
27
|
+
requirement: &2157840980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - =
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.7.4
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2157840980
|
36
|
+
description: Intrinsic adds properties to your objects
|
37
|
+
email:
|
38
|
+
- kurtisrainboltgreene@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- LICENSE
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- example/default_values.rb
|
49
|
+
- example/sample.rb
|
50
|
+
- intrinsic.gemspec
|
51
|
+
- lib/intrinsic.rb
|
52
|
+
- lib/intrinsic/extrinsicism.rb
|
53
|
+
- lib/intrinsic/intrinsicism.rb
|
54
|
+
- lib/intrinsic/intrinsicism/coercion.rb
|
55
|
+
- lib/intrinsic/intrinsicism/coercion/integer.rb
|
56
|
+
- lib/intrinsic/intrinsicism/coercion/proc.rb
|
57
|
+
- lib/intrinsic/intrinsicism/coercion/string.rb
|
58
|
+
- lib/intrinsic/intrinsicism/validation.rb
|
59
|
+
- lib/intrinsic/version.rb
|
60
|
+
- test/helper.rb
|
61
|
+
- test/unit/test_intrinsicism.rb
|
62
|
+
homepage: http://krainboltgreene.github.com/intrinsic
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: 3284528492314320514
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: 3284528492314320514
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.10
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Intrinsic adds properties to your objects
|
92
|
+
test_files:
|
93
|
+
- test/helper.rb
|
94
|
+
- test/unit/test_intrinsicism.rb
|
95
|
+
has_rdoc:
|