ethos 0.2.0.beta1

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: ab618cfe1935d258913fb2986ea607baabc690bd
4
+ data.tar.gz: bc01d20bfb599df12c7b5e6884406add79926258
5
+ SHA512:
6
+ metadata.gz: 27018e1f0949a2ba50e479769d5ccf10be153a2f5c4df3969b77db92f1737b3eb3c91e0953167b6e911d301d67702cc4621f206944f5e0a57a2a175672a0d288
7
+ data.tar.gz: 959d25e2aa998cc671b3801b7661e8ce2efba9cc2ed34583db75dbb84b1fca85491a9e42b6f8fa00532f0536dcd4b81861ed8a2122c2d9ccf4b297d8b8eb35d9
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.gem/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ethos.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Erol Fornoles
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,14 @@
1
+ [![Code Climate Score](http://img.shields.io/codeclimate/github/Erol/ethos.svg?style=flat)](https://codeclimate.com/github/Erol/ethos)
2
+
3
+ # Ethos
4
+
5
+ Entity library for Ruby
6
+
7
+ ## Contributing
8
+
9
+ 1. Fork it
10
+ 2. Create your feature branch ( `git checkout -b my-new-feature` )
11
+ 3. Create tests and make them pass ( `rake test` )
12
+ 4. Commit your changes ( `git commit -am 'Added some feature'` )
13
+ 5. Push to the branch ( `git push origin my-new-feature` )
14
+ 6. Create a new Pull Request
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Run gem specs'
4
+ task :spec do
5
+ require 'microspec'
6
+
7
+ runner = Microspec::Runner.new
8
+ runner.includes << 'spec/**/*.rb'
9
+ runner.excludes << 'spec/examples/**/*.rb'
10
+
11
+ success = runner.perform
12
+
13
+ exit 1 unless success
14
+ end
15
+
16
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ethos/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ethos'
8
+ spec.version = Ethos::VERSION
9
+ spec.authors = ['Erol Fornoles']
10
+ spec.email = ['erol.fornoles@gmail.com']
11
+ spec.summary = %q{Lightweight entity library for Ruby}
12
+ spec.description = %q{Lightweight entity library for Ruby}
13
+ spec.homepage = 'https://github.com/Erol/ethos'
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_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'microspec'
24
+ end
@@ -0,0 +1,4 @@
1
+ require 'ethos/version'
2
+
3
+ module Ethos
4
+ end
@@ -0,0 +1,54 @@
1
+ require 'ethos/type'
2
+
3
+ module Ethos
4
+ class Attributes
5
+ def schema
6
+ @_schema
7
+ end
8
+
9
+ def initial
10
+ @_initial ||= {}
11
+ end
12
+
13
+ def current
14
+ @_current ||= {}
15
+ end
16
+
17
+ def initialize(schema:, values: {})
18
+ @_schema = schema
19
+
20
+ values.each do |key, value|
21
+ initial[key] = value
22
+ current[key] = value
23
+ end
24
+ end
25
+
26
+ def [](key)
27
+ memoize key do
28
+ current[key] and Ethos::Type.cast current[key], schema.attributes[key][:type]
29
+ end
30
+ end
31
+
32
+ def []=(key, value)
33
+ unmemoize key
34
+
35
+ current[key] = value
36
+ end
37
+
38
+ private
39
+
40
+ def memoized
41
+ @_memoized ||= {}
42
+ end
43
+
44
+ def memoize(key)
45
+ memoized.fetch key do
46
+ memoized[key] = yield
47
+ end
48
+ end
49
+
50
+ def unmemoize(key)
51
+ memoized.delete key
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,44 @@
1
+ require 'ethos/schema'
2
+ require 'ethos/attributes'
3
+
4
+ module Ethos
5
+ module Entity
6
+ module ClassMethods
7
+ def schema
8
+ @_schema ||= Ethos::Schema.new
9
+ end
10
+
11
+ def attribute(key, type:, default: nil)
12
+ schema.define key, type: type, default: default
13
+
14
+ reader = :"#{key}"
15
+ writer = :"#{key}="
16
+
17
+ define_method reader do
18
+ attributes[key]
19
+ end
20
+
21
+ define_method writer do |value|
22
+ attributes[key] = value
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.prepended(base)
28
+ base.extend ClassMethods
29
+ end
30
+
31
+ def initialize(values = {})
32
+ schema = self.class.schema
33
+ values = values.merge schema.defaults
34
+
35
+ @_attributes = Ethos::Attributes.new schema: schema, values: values
36
+
37
+ super()
38
+ end
39
+
40
+ def attributes
41
+ @_attributes
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ module Ethos
2
+ class Schema
3
+ def attributes
4
+ @_attributes ||= {}
5
+ end
6
+
7
+ def defaults
8
+ @_defaults ||= {}
9
+ end
10
+
11
+ def define(key, type:, default: nil)
12
+ attributes[key] = {
13
+ type: type,
14
+ default: default
15
+ }
16
+
17
+ defaults[key] = default if default
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,63 @@
1
+ require 'date'
2
+ require 'time'
3
+ require 'bigdecimal'
4
+
5
+ module Ethos
6
+ module Type
7
+ CASTS = {}
8
+
9
+ def self.define(type, &block)
10
+ CASTS[type] = block
11
+ end
12
+
13
+ def self.cast(value, type)
14
+ return value if value.nil?
15
+
16
+ return value if value.is_a? type
17
+
18
+ return CASTS[type][value] if CASTS.key? type
19
+
20
+ type.new value
21
+ end
22
+ end
23
+ end
24
+
25
+ Ethos::Type.define Symbol do |value|
26
+ value.to_sym
27
+ end
28
+
29
+ Ethos::Type.define String do |value|
30
+ String value
31
+ end
32
+
33
+ Ethos::Type.define Integer do |value|
34
+ Integer value
35
+ end
36
+
37
+ Ethos::Type.define Float do |value|
38
+ Float value
39
+ end
40
+
41
+ Ethos::Type.define Date do |value|
42
+ Date.parse value
43
+ end
44
+
45
+ Ethos::Type.define Time do |value|
46
+ Time.parse value
47
+ end
48
+
49
+ Ethos::Type.define DateTime do |value|
50
+ DateTime.parse value
51
+ end
52
+
53
+ Ethos::Type.define Hash do |values|
54
+ Hash[*values]
55
+ end
56
+
57
+ Ethos::Type.define Array do |values|
58
+ Array[*values]
59
+ end
60
+
61
+ Ethos::Type.define BigDecimal do |value|
62
+ BigDecimal.new String value
63
+ end
@@ -0,0 +1,3 @@
1
+ module Ethos
2
+ VERSION = '0.2.0.beta1'
3
+ end
File without changes
@@ -0,0 +1,48 @@
1
+ require 'ethos/schema'
2
+ require 'ethos/attributes'
3
+
4
+ define schema: -> do
5
+ schema = Ethos::Schema.new
6
+ schema.define :value, type: Integer
7
+ schema
8
+ end
9
+
10
+ spec do
11
+ raises ArgumentError, 'missing keyword: schema' do
12
+ Ethos::Attributes.new
13
+ end
14
+ end
15
+
16
+ scope do
17
+ define attributes: -> { Ethos::Attributes.new schema: schema }
18
+
19
+ spec do
20
+ asserts(attributes[:value]) == nil
21
+ end
22
+
23
+ spec do
24
+ attributes[:value] = 1
25
+
26
+ asserts(attributes[:value]) == 1
27
+ end
28
+
29
+ spec do
30
+ attributes[:value] = '1'
31
+
32
+ asserts(attributes[:value]) == 1
33
+ end
34
+ end
35
+
36
+ scope do
37
+ spec do
38
+ attributes = Ethos::Attributes.new schema: schema, values: {value: 1}
39
+
40
+ asserts(attributes[:value]) == 1
41
+ end
42
+
43
+ spec do
44
+ attributes = Ethos::Attributes.new schema: schema, values: {value: '1'}
45
+
46
+ asserts(attributes[:value]) == 1
47
+ end
48
+ end
@@ -0,0 +1,92 @@
1
+ require 'ethos/entity'
2
+
3
+ scope do
4
+ setup do
5
+ class Entity
6
+ prepend Ethos::Entity
7
+
8
+ attribute :value, type: Integer
9
+ end
10
+ end
11
+
12
+ spec do
13
+ entity = Entity.new
14
+
15
+ asserts(entity.value) == nil
16
+ end
17
+
18
+ spec do
19
+ entity = Entity.new value: 1
20
+
21
+ asserts(entity.value) == 1
22
+ end
23
+
24
+ spec do
25
+ entity = Entity.new value: '1'
26
+
27
+ asserts(entity.value) == 1
28
+ end
29
+ end
30
+
31
+ scope do
32
+ setup do
33
+ class Entity
34
+ prepend Ethos::Entity
35
+
36
+ attribute :value, type: Integer, default: 1
37
+ end
38
+ end
39
+
40
+ spec do
41
+ entity = Entity.new
42
+
43
+ asserts(entity.value) == 1
44
+ end
45
+ end
46
+
47
+ scope do
48
+ setup do
49
+ class Entity
50
+ prepend Ethos::Entity
51
+
52
+ attribute :value, type: Integer, default: '1'
53
+ end
54
+ end
55
+
56
+ spec do
57
+ entity = Entity.new
58
+
59
+ asserts(entity.value) == 1
60
+ end
61
+ end
62
+
63
+ scope do
64
+ setup do
65
+ class Entity
66
+ prepend Ethos::Entity
67
+
68
+ attribute :name, type: String
69
+ attribute :parent, type: Entity
70
+ end
71
+ end
72
+
73
+ scope do
74
+ define entity: -> { Entity.new }
75
+
76
+ spec do
77
+ asserts(entity.parent) == nil
78
+ end
79
+ end
80
+
81
+ scope do
82
+ define entity: -> { Entity.new name: 'Child', parent: {name: 'Parent'} }
83
+
84
+ spec do
85
+ asserts(entity.parent).is_a? Entity
86
+ end
87
+
88
+ spec do
89
+ asserts(entity.parent.name) == 'Parent'
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,37 @@
1
+ require 'ethos/schema'
2
+
3
+ define schema: -> { Ethos::Schema.new }
4
+
5
+ spec do
6
+ raises ArgumentError, 'missing keyword: type' do
7
+ schema.define :value
8
+ end
9
+ end
10
+
11
+ scope do
12
+ setup do
13
+ schema.define :value, type: Integer
14
+ end
15
+
16
+ spec do
17
+ asserts(schema.attributes[:value]) == {type: Integer, default: nil}
18
+ end
19
+
20
+ spec do
21
+ refutes(schema.defaults).key? :value
22
+ end
23
+ end
24
+
25
+ scope do
26
+ setup do
27
+ schema.define :value, type: Integer, default: 1
28
+ end
29
+
30
+ spec do
31
+ asserts(schema.attributes[:value]) == {type: Integer, default: 1}
32
+ end
33
+
34
+ spec do
35
+ asserts(schema.defaults).key? :value
36
+ end
37
+ end
@@ -0,0 +1,103 @@
1
+ require 'ethos/type'
2
+ require 'ethos/entity'
3
+
4
+ scope '.cast' do
5
+ spec 'casts a value to a Symbol' do
6
+ expected = :Symbol
7
+ result = Ethos::Type.cast 'Symbol', Symbol
8
+
9
+ asserts(result) == expected
10
+ end
11
+
12
+ spec 'casts a value to a String' do
13
+ expected = String 1
14
+ result = Ethos::Type.cast 1, String
15
+
16
+ asserts(result) == expected
17
+ end
18
+
19
+ spec 'casts a value to an Integer' do
20
+ expected = Integer 1
21
+ result = Ethos::Type.cast 1, Integer
22
+
23
+ asserts(result) == expected
24
+ end
25
+
26
+ spec 'casts a value to a Float' do
27
+ expected = Float 1
28
+ result = Ethos::Type.cast 1, Float
29
+
30
+ asserts(result) == expected
31
+ end
32
+
33
+ spec 'casts a value to a Date' do
34
+ expected = Date.parse '2014-01-01'
35
+ result = Ethos::Type.cast '2014-01-01', Date
36
+
37
+ asserts(result) == expected
38
+ end
39
+
40
+ spec 'casts a value to a Time' do
41
+ expected = Time.parse '2014-01-01 12:00AM'
42
+ result = Ethos::Type.cast '2014-01-01 12:00AM', Time
43
+
44
+ asserts(result) == expected
45
+ end
46
+
47
+ spec 'casts a value to a DateTime' do
48
+ expected = DateTime.parse '2014-01-01 12:00AM'
49
+ result = Ethos::Type.cast '2014-01-01 12:00AM', DateTime
50
+
51
+ asserts(result) == expected
52
+ end
53
+
54
+ spec 'casts a value to a Hash' do
55
+ expected = Hash[:a, 1]
56
+ result = Ethos::Type.cast [:a, 1], Hash
57
+
58
+ asserts(result) == expected
59
+ end
60
+
61
+ spec 'casts a value to a Array' do
62
+ expected = Array[1, 2]
63
+ result = Ethos::Type.cast [1, 2], Array
64
+
65
+ asserts(result) == expected
66
+ end
67
+
68
+ spec 'casts a value to a BigDecimal' do
69
+ expected = BigDecimal.new '10.10'
70
+ result = Ethos::Type.cast 10.10, BigDecimal
71
+
72
+ asserts(result) == expected
73
+ end
74
+
75
+ spec 'casts a hash of values to an Entity' do
76
+ class Entity
77
+ prepend Ethos::Entity
78
+
79
+ attribute :value, type: Float
80
+ end
81
+
82
+ values = {value: 1}
83
+
84
+ expected = Entity.new values
85
+ result = Ethos::Type.cast values, Entity
86
+
87
+ asserts(result).is_a? Entity
88
+ asserts(result.value) == expected.value
89
+ end
90
+
91
+ spec 'casts a nil' do
92
+ class Entity
93
+ prepend Ethos::Entity
94
+
95
+ attribute :value, type: Float
96
+ end
97
+
98
+ expected = nil
99
+ result = Ethos::Type.cast nil, Entity
100
+
101
+ asserts(result) == expected
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ethos
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0.beta1
5
+ platform: ruby
6
+ authors:
7
+ - Erol Fornoles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-17 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: microspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Lightweight entity library for Ruby
56
+ email:
57
+ - erol.fornoles@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - ethos.gemspec
68
+ - lib/ethos.rb
69
+ - lib/ethos/attributes.rb
70
+ - lib/ethos/entity.rb
71
+ - lib/ethos/schema.rb
72
+ - lib/ethos/type.rb
73
+ - lib/ethos/version.rb
74
+ - spec/.gitkeep
75
+ - spec/ethos/attributes.rb
76
+ - spec/ethos/entity.rb
77
+ - spec/ethos/schema.rb
78
+ - spec/ethos/type.rb
79
+ homepage: https://github.com/Erol/ethos
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.1
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Lightweight entity library for Ruby
103
+ test_files:
104
+ - spec/.gitkeep
105
+ - spec/ethos/attributes.rb
106
+ - spec/ethos/entity.rb
107
+ - spec/ethos/schema.rb
108
+ - spec/ethos/type.rb