lego 0.0.3

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format Fuubar
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
@@ -0,0 +1,5 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
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.
@@ -0,0 +1,3 @@
1
+ # Lego
2
+
3
+ Data building blocks for your app.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => 'spec'
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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,12 @@
1
+ module Lego::Value
2
+ class Date < Base
3
+
4
+ def parsers
5
+ [
6
+ ->(v) { Lego.just(v.to_date) rescue Lego.fail("invalid date: '#{v}'") },
7
+ ->(v) { v.nil? ? Lego.none : Lego.just(v) },
8
+ ]
9
+ end
10
+
11
+ end
12
+ 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