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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88e622d4b8a2fbd5684a873a9b3e2c30beefd9fd
4
- data.tar.gz: ab73433d9e9513e20b5853940b016e8afa0c4c16
3
+ metadata.gz: b33ca4cb16746cbe51209792863e034860525fe7
4
+ data.tar.gz: b91099734a0144e57d78e6f6bce5ad00d7bdaf1c
5
5
  SHA512:
6
- metadata.gz: 809eb722dfeb22a1ab4785af98d9a77cef28177aadb082b4c1fadfdbc35999e5b436a19262e4480541468d587cc45948dbd074e739ef5610a0b3d9284f961985
7
- data.tar.gz: 9069a79cdc8b699d0b5691105686c1ad82e7a2c513ba49cbbaf1368191e0699d917f5a2c30891cde517c8abfd4ae244d8d1891f919f6052b50f135947faff99f
6
+ metadata.gz: e10c6ec67d4bfbb3d6364303ac365bd76d23bf3a9838d18e7cbd244d106bc6cbb90703cbbd61b64e229d4caa8ed83a775663049272ab11a83d9fdc46d048382c
7
+ data.tar.gz: 59e9dcc96fd43d0a3d8faf69ca7c823729278a91998f58147ea7a93abdb73b82c595d3d324637c50362e5012bedb72a4b1a32e24ce276b4f6f0bb34b9d352c7c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- looker (0.1.0)
4
+ looker (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,3 +1,110 @@
1
+ [![Lines of Code](http://img.shields.io/badge/lines_of_code-52-brightgreen.svg?style=flat)](http://blog.codinghorror.com/the-best-code-is-no-code-at-all/)
2
+ [![Code Status](http://img.shields.io/codeclimate/github/hopsoft/looker.svg?style=flat)](https://codeclimate.com/github/hopsoft/looker)
3
+ [![Dependency Status](http://img.shields.io/gemnasium/hopsoft/looker.svg?style=flat)](https://gemnasium.com/hopsoft/looker)
4
+ [![Build Status](http://img.shields.io/travis/hopsoft/looker.svg?style=flat)](https://travis-ci.org/hopsoft/looker)
5
+ [![Coverage Status](https://img.shields.io/coveralls/hopsoft/looker.svg?style=flat)](https://coveralls.io/r/hopsoft/looker?branch=master)
6
+ [![Downloads](http://img.shields.io/gem/dt/looker.svg?style=flat)](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
+
@@ -1,15 +1,29 @@
1
+ require "forwardable"
1
2
  require "looker/version"
2
3
  require "looker/table"
3
4
 
4
5
  module Looker
5
- def self.create_constants(data={})
6
- data.each do |name, rows|
7
- table = Looker::Table.new(name, rows)
8
- if Looker.const_defined?(table.const_name)
9
- message = "Looker::#{table.const_name} is already defined!"
10
- raise ArgumentError.new(message)
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
@@ -5,21 +5,28 @@ module Looker
5
5
  extend Forwardable
6
6
  include Enumerable
7
7
  def_delegators :rows, :each
8
- def_delegator :dict, :[]
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.to_s.gsub(/\s/, "_").gsub(/\W/, "").upcase
11
+ name.gsub(/\s/, "_").gsub(/\W/, "").upcase
13
12
  end
14
13
 
15
14
  def initialize(name, rows={})
16
- @name = name.to_s.dup.freeze
17
- @const_name = self.class.constant_name(name).freeze
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
@@ -1,3 +1,3 @@
1
1
  module Looker
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.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-03-31 00:00:00.000000000 Z
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler