lego 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +13 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/README.md +3 -0
- data/Rakefile +7 -0
- data/lego.gemspec +21 -0
- data/lib/lego.rb +22 -0
- data/lib/lego/either.rb +91 -0
- data/lib/lego/model.rb +69 -0
- data/lib/lego/value.rb +15 -0
- data/lib/lego/value/base.rb +34 -0
- data/lib/lego/value/boolean.rb +26 -0
- data/lib/lego/value/date.rb +12 -0
- data/lib/lego/value/float.rb +22 -0
- data/lib/lego/value/integer.rb +22 -0
- data/lib/lego/value/set.rb +39 -0
- data/lib/lego/value/string.rb +20 -0
- data/spec/lego/either/fail_spec.rb +38 -0
- data/spec/lego/either/just_spec.rb +50 -0
- data/spec/lego/either/none_spec.rb +37 -0
- data/spec/lego/model_spec.rb +93 -0
- data/spec/lego/value/base_spec.rb +14 -0
- data/spec/lego/value/boolean_spec.rb +62 -0
- data/spec/lego/value/date_spec.rb +71 -0
- data/spec/lego/value/float_spec.rb +93 -0
- data/spec/lego/value/integer_spec.rb +86 -0
- data/spec/lego/value/set_spec.rb +77 -0
- data/spec/lego/value/string_spec.rb +106 -0
- data/spec/spec_helper.rb +38 -0
- metadata +142 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in lego.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development, :test do
|
7
|
+
gem 'guard-rspec'
|
8
|
+
gem 'fuubar'
|
9
|
+
|
10
|
+
gem 'growl', :require => RUBY_PLATFORM.include?('darwin') && 'growl'
|
11
|
+
gem 'rb-fsevent', :require => RUBY_PLATFORM.include?('darwin') && 'rb-fsevent'
|
12
|
+
gem 'rb-inotify', :require => RUBY_PLATFORM.include?('linux') && 'rb-inotify'
|
13
|
+
end
|
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Norbert Wojtowicz
|
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
data/Rakefile
ADDED
data/lego.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Norbert Wojtowicz"]
|
5
|
+
gem.email = ["wojtowicz.norbert@gmail.com"]
|
6
|
+
gem.description = "Data building blocks for you app."
|
7
|
+
gem.summary = gem.description
|
8
|
+
gem.homepage = "https://github.com/pithyless/lego"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "lego"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = '0.0.3'
|
16
|
+
|
17
|
+
gem.add_dependency 'activesupport'
|
18
|
+
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
end
|
data/lib/lego.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Lego
|
2
|
+
require 'active_support/json/encoding'
|
3
|
+
|
4
|
+
CoerceError = Class.new(StandardError)
|
5
|
+
|
6
|
+
require_relative 'lego/either'
|
7
|
+
require_relative 'lego/value'
|
8
|
+
require_relative 'lego/model'
|
9
|
+
|
10
|
+
def self.value_parser(item, *args)
|
11
|
+
if item.is_a?(Symbol)
|
12
|
+
Lego::Value.const_get(item.to_s.camelize, false).new(*args)
|
13
|
+
elsif item.respond_to?(:coerce)
|
14
|
+
item
|
15
|
+
else
|
16
|
+
fail NameError
|
17
|
+
end
|
18
|
+
rescue NameError
|
19
|
+
fail NameError, "Unknown Lego::Value parser: #{item.to_s.camelize}"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/lego/either.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
module Lego
|
2
|
+
|
3
|
+
def self.none
|
4
|
+
Either::None.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.just(value)
|
8
|
+
Either::Just.new(value)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.fail(error)
|
12
|
+
Either::Fail.new(error)
|
13
|
+
end
|
14
|
+
|
15
|
+
module Either
|
16
|
+
module Eitherish
|
17
|
+
def value?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
def none?
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
25
|
+
def error?
|
26
|
+
false
|
27
|
+
end
|
28
|
+
|
29
|
+
def next(callable)
|
30
|
+
self
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Just
|
35
|
+
include Eitherish
|
36
|
+
|
37
|
+
def initialize(value)
|
38
|
+
@value = value
|
39
|
+
end
|
40
|
+
|
41
|
+
attr_reader :value
|
42
|
+
|
43
|
+
def value?
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
def next(callable)
|
48
|
+
callable.call(value).tap do |res|
|
49
|
+
unless res.kind_of?(Eitherish)
|
50
|
+
fail TypeError, "Not Lego::Either: #{res.inspect}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def inspect
|
56
|
+
"<Lego::Either::Just '#{value}'>"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class None
|
61
|
+
include Eitherish
|
62
|
+
|
63
|
+
def none?
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
67
|
+
def inspect
|
68
|
+
"<Lego::Either::None>"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class Fail
|
73
|
+
include Eitherish
|
74
|
+
|
75
|
+
def initialize(error)
|
76
|
+
@error = error
|
77
|
+
end
|
78
|
+
|
79
|
+
attr_reader :error
|
80
|
+
|
81
|
+
def error?
|
82
|
+
true
|
83
|
+
end
|
84
|
+
|
85
|
+
def inspect
|
86
|
+
"<Lego::Either::Fail '#{error}'>"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
data/lib/lego/model.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module Lego
|
2
|
+
class Model
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def attribute(attr, type, *args)
|
6
|
+
parsers[attr.to_sym] = Lego.value_parser(type, *args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def parsers
|
10
|
+
@_parsers ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def attribute_names
|
14
|
+
parsers.keys
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(attrs={})
|
19
|
+
@attributes = {}.tap do |h|
|
20
|
+
self.class.parsers.each do |name, parser|
|
21
|
+
value = attrs.delete(name)
|
22
|
+
begin
|
23
|
+
h[name] = parser.coerce(value)
|
24
|
+
rescue Lego::CoerceError => e
|
25
|
+
fail ArgumentError, ":#{name} => #{e.message}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end.freeze
|
29
|
+
fail ArgumentError, "Unknown attributes: #{attrs}" unless attrs.empty?
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :attributes
|
33
|
+
|
34
|
+
def method_missing(name, *args, &block)
|
35
|
+
attributes.fetch(name.to_sym) { super }
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.coerce(hash)
|
39
|
+
hash.instance_of?(self) ? hash : self.new(hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.parse(hash)
|
43
|
+
Lego.just(self.coerce(hash))
|
44
|
+
rescue Lego::CoerceError => e
|
45
|
+
Lego.fail(e.message)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Equality
|
49
|
+
|
50
|
+
def ==(o)
|
51
|
+
o.class == self.class && o.attributes == attributes
|
52
|
+
end
|
53
|
+
alias_method :eql?, :==
|
54
|
+
|
55
|
+
def hash
|
56
|
+
attributes.hash
|
57
|
+
end
|
58
|
+
|
59
|
+
# Serialize
|
60
|
+
|
61
|
+
def as_json
|
62
|
+
{}.tap do |h|
|
63
|
+
attributes.each do |attr, val|
|
64
|
+
h[attr] = val.as_json
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/lego/value.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_support/core_ext/string'
|
2
|
+
require 'active_support/core_ext/time/conversions'
|
3
|
+
|
4
|
+
module Lego
|
5
|
+
module Value
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
require_relative 'value/base'
|
10
|
+
require_relative 'value/set'
|
11
|
+
require_relative 'value/string'
|
12
|
+
require_relative 'value/date'
|
13
|
+
require_relative 'value/integer'
|
14
|
+
require_relative 'value/float'
|
15
|
+
require_relative 'value/boolean'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Lego::Value
|
2
|
+
class Base
|
3
|
+
def initialize(opts={})
|
4
|
+
@opts = opts
|
5
|
+
end
|
6
|
+
|
7
|
+
def parse(val)
|
8
|
+
val = val.nil? ? Lego.none : Lego.just(val)
|
9
|
+
|
10
|
+
parsers.each do |callable|
|
11
|
+
val = val.next(callable)
|
12
|
+
end
|
13
|
+
val
|
14
|
+
end
|
15
|
+
|
16
|
+
def coerce(val)
|
17
|
+
resp = parse(val)
|
18
|
+
if resp.value?
|
19
|
+
resp.value
|
20
|
+
elsif resp.error?
|
21
|
+
raise Lego::CoerceError, resp.error
|
22
|
+
elsif @opts[:default]
|
23
|
+
@opts[:default].call
|
24
|
+
else
|
25
|
+
raise Lego::CoerceError, 'missing value'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def parsers
|
30
|
+
[]
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Lego::Value
|
2
|
+
class Boolean < Base
|
3
|
+
|
4
|
+
def parsers
|
5
|
+
[
|
6
|
+
->(v) { parse_boolean(v) }
|
7
|
+
]
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def parse_boolean(v)
|
13
|
+
case v
|
14
|
+
when true, 'true'
|
15
|
+
Lego.just(true)
|
16
|
+
when false, 'false'
|
17
|
+
Lego.just(false)
|
18
|
+
when ''
|
19
|
+
Lego.none
|
20
|
+
else
|
21
|
+
Lego.fail("invalid boolean: '#{v}'")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Lego::Value
|
2
|
+
class Float < Base
|
3
|
+
|
4
|
+
def parsers
|
5
|
+
[
|
6
|
+
->(v) { v.to_s.empty? ? Lego.none : Lego.just(v) },
|
7
|
+
->(v) { Lego.just(Float(v.to_s)) rescue Lego.fail("invalid float: '#{v}'") },
|
8
|
+
->(v) { v < minimum ? Lego.fail("less than minimum of #{minimum}: '#{v}'") : Lego.just(v) },
|
9
|
+
->(v) { v > maximum ? Lego.fail("more than maximum of #{maximum}: '#{v}'") : Lego.just(v) },
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
def minimum
|
14
|
+
@opts.fetch(:min, -1.0/0)
|
15
|
+
end
|
16
|
+
|
17
|
+
def maximum
|
18
|
+
@opts.fetch(:max, 1.0/0)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Lego::Value
|
2
|
+
class Integer < Base
|
3
|
+
|
4
|
+
def parsers
|
5
|
+
[
|
6
|
+
->(v) { v.to_s.empty? ? Lego.none : Lego.just(v) },
|
7
|
+
->(v) { Lego.just(Integer(v.to_s)) rescue Lego.fail("invalid integer: '#{v}'") },
|
8
|
+
->(v) { v < minimum ? Lego.fail("less than minimum of #{minimum}: '#{v}'") : Lego.just(v) },
|
9
|
+
->(v) { v > maximum ? Lego.fail("more than maximum of #{maximum}: '#{v}'") : Lego.just(v) },
|
10
|
+
]
|
11
|
+
end
|
12
|
+
|
13
|
+
def minimum
|
14
|
+
@opts.fetch(:min, -1.0/0)
|
15
|
+
end
|
16
|
+
|
17
|
+
def maximum
|
18
|
+
@opts.fetch(:max, 1.0/0)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Lego::Value
|
4
|
+
class Set < Base
|
5
|
+
|
6
|
+
def initialize(type, opts={})
|
7
|
+
@_item_parser = Lego.value_parser(type)
|
8
|
+
super(opts)
|
9
|
+
end
|
10
|
+
|
11
|
+
def parsers
|
12
|
+
[
|
13
|
+
->(v) { v.respond_to?(:to_set) ? Lego.just(v.to_set) : Lego.fail("invalid set: '#{v}'") },
|
14
|
+
->(v) { parse_items(v) },
|
15
|
+
->(v) { (not allow_empty? and v.empty?) ? Lego.none : Lego.just(v) },
|
16
|
+
]
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def parse_items(set)
|
22
|
+
new_set = ::Set.new
|
23
|
+
set.each do |item|
|
24
|
+
new_item = @_item_parser.parse(item)
|
25
|
+
if new_item.value?
|
26
|
+
new_set << new_item.value
|
27
|
+
else
|
28
|
+
return new_item
|
29
|
+
end
|
30
|
+
end
|
31
|
+
Lego.just(new_set)
|
32
|
+
end
|
33
|
+
|
34
|
+
def allow_empty?
|
35
|
+
@opts.fetch(:allow_empty, false)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|