looker 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +107 -0
- data/lib/looker.rb +21 -7
- data/lib/looker/table.rb +12 -5
- data/lib/looker/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b33ca4cb16746cbe51209792863e034860525fe7
|
4
|
+
data.tar.gz: b91099734a0144e57d78e6f6bce5ad00d7bdaf1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e10c6ec67d4bfbb3d6364303ac365bd76d23bf3a9838d18e7cbd244d106bc6cbb90703cbbd61b64e229d4caa8ed83a775663049272ab11a83d9fdc46d048382c
|
7
|
+
data.tar.gz: 59e9dcc96fd43d0a3d8faf69ca7c823729278a91998f58147ea7a93abdb73b82c595d3d324637c50362e5012bedb72a4b1a32e24ce276b4f6f0bb34b9d352c7c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,110 @@
|
|
1
|
+
[](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
|
2
|
+
[](https://codeclimate.com/github/hopsoft/looker)
|
3
|
+
[](https://gemnasium.com/hopsoft/looker)
|
4
|
+
[](https://travis-ci.org/hopsoft/looker)
|
5
|
+
[](https://coveralls.io/r/hopsoft/looker?branch=master)
|
6
|
+
[](http://rubygems.org/gems/looker)
|
7
|
+
|
1
8
|
# Looker
|
2
9
|
|
3
10
|
## Hash based enumerated types (ENUMS)
|
11
|
+
|
12
|
+
## Quick Start
|
13
|
+
|
14
|
+
```sh
|
15
|
+
gem install looker
|
16
|
+
```
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
Looker.add roles: {
|
20
|
+
:admin => 1,
|
21
|
+
:reader => 2,
|
22
|
+
:writer => 3
|
23
|
+
}
|
24
|
+
|
25
|
+
# The name (first Hash key) & value is converted
|
26
|
+
# to an upcase named frozen constant on the Looker module
|
27
|
+
|
28
|
+
Looker::ROLES[:admin] # => 1
|
29
|
+
Looker::ROLES[1] # => :admin
|
30
|
+
|
31
|
+
Looker::ROLES[:reader] # => 2
|
32
|
+
Looker::ROLES[2] # => :reader
|
33
|
+
|
34
|
+
Looker::ROLES["writer"] # => 3
|
35
|
+
Looker::ROLES[3] # => :writer
|
36
|
+
```
|
37
|
+
|
38
|
+
__NOTE:__ You can perform lookups using both the key & value.
|
39
|
+
|
40
|
+
## Multiple Enumerated Types
|
41
|
+
|
42
|
+
It's possible to add multiple enumerated types with a single call to `Looker#add`.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Looker.add(
|
46
|
+
roles: {
|
47
|
+
:admin => 1,
|
48
|
+
:reader => 2,
|
49
|
+
:writer => 3
|
50
|
+
},
|
51
|
+
|
52
|
+
colors: {
|
53
|
+
:red => "ff0000",
|
54
|
+
:yellow => "ffff00",
|
55
|
+
:blue => "0000ff"
|
56
|
+
}
|
57
|
+
)
|
58
|
+
|
59
|
+
Looker::ROLES[:admin] # => 1
|
60
|
+
Looker::ROLES[3] # => :writer
|
61
|
+
|
62
|
+
Looker::COLORS[:red] # => "ff0000"
|
63
|
+
Looker::COLORS["ffff00"] # => :yellow
|
64
|
+
```
|
65
|
+
|
66
|
+
## Adding Enumerated Types from YAML
|
67
|
+
|
68
|
+
It can be useful to manage enumerated type definitions with a YAML file.
|
69
|
+
|
70
|
+
```yaml
|
71
|
+
# /path/to/enums.yml
|
72
|
+
roles:
|
73
|
+
admin: 1
|
74
|
+
reader: 2
|
75
|
+
writer: 3
|
76
|
+
|
77
|
+
colors:
|
78
|
+
red: ff0000
|
79
|
+
yellow: ffff00
|
80
|
+
blue: 0000ff
|
81
|
+
```
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
Looker.add YAML.load(File.read("/path/to/enums.yml")
|
85
|
+
|
86
|
+
Looker::ROLES["reader"] # => 2
|
87
|
+
Looker::ROLES[1] # => "admin"
|
88
|
+
|
89
|
+
Looker::COLORS["yellow"] # => "ffff00"
|
90
|
+
Looker::COLORS["0000ff"] # => "blue"
|
91
|
+
```
|
92
|
+
|
93
|
+
## ActiveRecord enums
|
94
|
+
|
95
|
+
You may find it useful to reuse Looker defined enumerated types as [ActiveRecord enums](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html).
|
96
|
+
|
97
|
+
Fortunately this is pretty simple— just use the `to_h` method on the Looker constant.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
Looker.add roles: {
|
101
|
+
:admin => 1,
|
102
|
+
:reader => 2,
|
103
|
+
:writer => 3
|
104
|
+
}
|
105
|
+
|
106
|
+
class User < ActiveRecord::Base
|
107
|
+
enum roles: Looker::ROLES.to_h
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
data/lib/looker.rb
CHANGED
@@ -1,15 +1,29 @@
|
|
1
|
+
require "forwardable"
|
1
2
|
require "looker/version"
|
2
3
|
require "looker/table"
|
3
4
|
|
4
5
|
module Looker
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
extend Forwardable
|
7
|
+
include Enumerable
|
8
|
+
def_delegators :tables, :each
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
def tables
|
13
|
+
@tables ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def add(data={})
|
17
|
+
data.each do |name, rows|
|
18
|
+
table = Looker::Table.new(name, rows)
|
19
|
+
if Looker.const_defined?(table.constant_name)
|
20
|
+
message = "Looker::#{table.constant_name} is already defined!"
|
21
|
+
raise ArgumentError.new(message)
|
22
|
+
end
|
23
|
+
Looker.const_set table.constant_name, table
|
24
|
+
tables << table
|
11
25
|
end
|
12
|
-
Looker.const_set table.const_name, table
|
13
26
|
end
|
27
|
+
|
14
28
|
end
|
15
29
|
end
|
data/lib/looker/table.rb
CHANGED
@@ -5,21 +5,28 @@ module Looker
|
|
5
5
|
extend Forwardable
|
6
6
|
include Enumerable
|
7
7
|
def_delegators :rows, :each
|
8
|
-
|
9
|
-
attr_reader :name, :const_name, :rows
|
8
|
+
attr_reader :name, :constant_name, :rows
|
10
9
|
|
11
10
|
def self.constant_name(name)
|
12
|
-
name.
|
11
|
+
name.gsub(/\s/, "_").gsub(/\W/, "").upcase
|
13
12
|
end
|
14
13
|
|
15
14
|
def initialize(name, rows={})
|
16
|
-
@name = name.to_s.
|
17
|
-
@
|
15
|
+
@name = name.to_s.strip.freeze
|
16
|
+
@constant_name = self.class.constant_name(@name).freeze
|
18
17
|
@rows = rows.dup.freeze
|
19
18
|
@dict = rows.invert.merge(rows).freeze
|
20
19
|
freeze
|
21
20
|
end
|
22
21
|
|
22
|
+
def [](key)
|
23
|
+
dict[key] || dict[key.to_s] || dict[key.to_s.to_sym]
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_h
|
27
|
+
rows
|
28
|
+
end
|
29
|
+
|
23
30
|
private
|
24
31
|
|
25
32
|
attr_reader :dict
|
data/lib/looker/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: looker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Hopkins
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|