liner 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/README.md +2 -2
- data/lib/liner.rb +48 -35
- data/lib/liner/base.rb +20 -0
- data/lib/liner/equalizable.rb +7 -0
- data/lib/liner/hashable.rb +23 -0
- data/lib/liner/inspectable.rb +3 -0
- data/lib/liner/serializable.rb +10 -4
- data/lib/liner/version.rb +1 -1
- data/test/liner_test.rb +3 -3
- 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: 7bbfb3aba7d5aa9e37d5b7f9bf3192ba358ed87d
|
4
|
+
data.tar.gz: 9b69f55a62fb0e5d0ef4b4de8ccd3427bf64b190
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b07b23e891919b936ed3a1ad2f42db641490e558237547b2d8a85c234489d0815d089cedd738aa13d34fedeef9ba7a79fdc4b09312f963d4ab9193fbc54c9d0c
|
7
|
+
data.tar.gz: 7a1445ce77473f7f1fad7c63ad876780126b52dcd0b4ade0f0b869590e95cb4ce79322d9bff14cc9e77ff3c08088b2383fbbc6c1f6713d9c587f7011bda89151
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -100,7 +100,7 @@ e.to_json
|
|
100
100
|
#### Getters/Readers
|
101
101
|
If you want to customize the way an attribute is read, just override the method
|
102
102
|
like you would any accessor. You can access the raw value through either the instance variable,
|
103
|
-
`
|
103
|
+
`liner_get`, or `super`.
|
104
104
|
|
105
105
|
```ruby
|
106
106
|
class Taco < Liner.new(:filling)
|
@@ -130,7 +130,7 @@ taco.liner
|
|
130
130
|
|
131
131
|
#### Setters/Writers
|
132
132
|
It's the same scenario for customizing the writer. Set the real value
|
133
|
-
through the instance variable, `
|
133
|
+
through the instance variable, `liner_set`, or `super`.
|
134
134
|
|
135
135
|
```ruby
|
136
136
|
class Bacon < Liner.new(:is_good)
|
data/lib/liner.rb
CHANGED
@@ -7,49 +7,62 @@ require 'liner/serializable'
|
|
7
7
|
require 'ext/class'
|
8
8
|
|
9
9
|
module Liner
|
10
|
-
|
10
|
+
|
11
|
+
# List of liner attributes as an array of symbols
|
12
|
+
# @return [Array]
|
13
|
+
def self.liner_keys
|
14
|
+
[]
|
15
|
+
end
|
16
|
+
|
17
|
+
# Apply a liner to a given class
|
18
|
+
# @param base [Class] A class to add a liner to
|
19
|
+
# @param keys [Array] An array of symbols or strings which will serve as attributes
|
20
|
+
# @return [Class] The class with a liner installed
|
21
|
+
# @example
|
22
|
+
# Person = Class.new
|
23
|
+
# Liner.apply! Person, :name, :occupation # => Person
|
11
24
|
# @api public
|
12
|
-
def
|
13
|
-
|
14
|
-
|
25
|
+
def Liner.apply!(base, *keys)
|
26
|
+
keys = keys.map(&:to_sym).uniq
|
27
|
+
|
28
|
+
base.send(:define_singleton_method, :liner_keys) do
|
29
|
+
@liner_keys ||= begin
|
30
|
+
super() + keys
|
31
|
+
rescue NoMethodError
|
32
|
+
keys
|
33
|
+
end.uniq.freeze
|
15
34
|
end
|
16
|
-
end
|
17
|
-
end
|
18
35
|
|
19
|
-
|
20
|
-
# @param keys [Array] An array of symbols or strings which will serve as attributes
|
21
|
-
# @return [Class]
|
22
|
-
# @example Liner.new(:foo) # => #<Class:0x007fd0993bab98>
|
23
|
-
# @api public
|
24
|
-
def Liner.new(*keys)
|
25
|
-
apply! Class.new, *keys
|
26
|
-
end
|
36
|
+
base.send :include, Liner unless base < Liner
|
27
37
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
base.send(:define_method, :liner_keys) do
|
37
|
-
begin
|
38
|
-
(super() + keys).uniq
|
39
|
-
rescue NoMethodError
|
40
|
-
keys
|
38
|
+
keys.each do |key|
|
39
|
+
unless base.method_defined? key
|
40
|
+
base.send(:define_method, key){ liner_get key }
|
41
|
+
end
|
42
|
+
unless base.method_defined? "#{key}="
|
43
|
+
base.send(:define_method, "#{key}="){ |value| liner_set key, value }
|
44
|
+
end
|
41
45
|
end
|
46
|
+
base
|
42
47
|
end
|
43
48
|
|
44
|
-
|
49
|
+
# Setup an anonymous class with liner keys
|
50
|
+
# @param keys [Array] An array of symbols or strings which will serve as attributes
|
51
|
+
# @return [Class]
|
52
|
+
# @example
|
53
|
+
# Liner.new(:foo) # => #<Class:0x007fd0993bab98>
|
54
|
+
# @api public
|
55
|
+
def Liner.new(*keys)
|
56
|
+
apply! Class.new, *keys
|
57
|
+
end
|
45
58
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
base.send(:define_method, "#{key}="){ |value| liner_set key, value }
|
59
|
+
# Includes liner submodules when included
|
60
|
+
# @api private
|
61
|
+
def self.included(base)
|
62
|
+
[Base, Hashable, Equalizable, Inspectable, Serializable].each do |mod|
|
63
|
+
base.send :include, mod
|
52
64
|
end
|
53
65
|
end
|
54
|
-
base
|
55
66
|
end
|
67
|
+
|
68
|
+
|
data/lib/liner/base.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
module Liner
|
2
2
|
module Base
|
3
|
+
# Create a new instance of a Liner class
|
4
|
+
# @param args [Hash, Array, nil] A hash of attribute-value pairs, an array of values or nil
|
5
|
+
# @return [Class] A new instance of the Liner class
|
3
6
|
def initialize(*args)
|
4
7
|
if args.count == 1 && args.first.respond_to?(:keys)
|
5
8
|
self.liner = args.first
|
@@ -11,6 +14,10 @@ module Liner
|
|
11
14
|
|
12
15
|
private
|
13
16
|
|
17
|
+
# Get a liner attribute.
|
18
|
+
# @param key [Symbol, String] The attribute to be read
|
19
|
+
# @return value [Object] The value of the attribute
|
20
|
+
# @api private
|
14
21
|
def liner_get(key)
|
15
22
|
key = key.to_sym
|
16
23
|
with_valid_attribute(key) do
|
@@ -18,6 +25,11 @@ module Liner
|
|
18
25
|
end
|
19
26
|
end
|
20
27
|
|
28
|
+
# Set a liner attribute
|
29
|
+
# @param key [Symbol, String] The attribute to be set
|
30
|
+
# @param value [Object] The value to set the attribute to
|
31
|
+
# @return value [Object]
|
32
|
+
# @api private
|
21
33
|
def liner_set(key, value)
|
22
34
|
key = key.to_sym
|
23
35
|
with_valid_attribute(key) do
|
@@ -25,6 +37,8 @@ module Liner
|
|
25
37
|
end
|
26
38
|
end
|
27
39
|
|
40
|
+
# Raise an error unless the key exists
|
41
|
+
# @api private
|
28
42
|
def with_valid_attribute(key, &block)
|
29
43
|
if liner_keys.include?(key)
|
30
44
|
yield
|
@@ -32,4 +46,10 @@ module Liner
|
|
32
46
|
raise ArgumentError, "Invalid liner attribute: '#{key}'"
|
33
47
|
end
|
34
48
|
end
|
49
|
+
|
50
|
+
# Delegate liner keys to the singleton class method
|
51
|
+
# @api private
|
52
|
+
def liner_keys
|
53
|
+
self.class.liner_keys
|
54
|
+
end
|
35
55
|
end
|
data/lib/liner/equalizable.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
1
|
module Liner
|
2
2
|
module Equalizable
|
3
|
+
|
4
|
+
# Test equality between a liner class instance and another object
|
5
|
+
# @param other [Object]
|
6
|
+
# @return [true, false]
|
3
7
|
def ==(other)
|
4
8
|
return false unless other.class == self.class
|
5
9
|
liner == other.liner
|
6
10
|
end
|
7
11
|
|
12
|
+
# Test equality between a liner class instance and another object
|
13
|
+
# @param other [Object]
|
14
|
+
# @return [true, false]
|
8
15
|
def eql?(other)
|
9
16
|
return false unless other.class == self.class
|
10
17
|
liner.eql?(other.liner)
|
data/lib/liner/hashable.rb
CHANGED
@@ -1,32 +1,55 @@
|
|
1
1
|
module Liner
|
2
2
|
module Hashable
|
3
3
|
|
4
|
+
# Read a liner attribute
|
5
|
+
# @param key [Symbol, String] The attribute to fetch the value for
|
6
|
+
# @return [Object] The attribute value
|
7
|
+
# @api public
|
4
8
|
def [](key)
|
5
9
|
send key.to_sym
|
6
10
|
rescue NoMethodError
|
7
11
|
liner_get key
|
8
12
|
end
|
9
13
|
|
14
|
+
# Set a liner attribute
|
15
|
+
# @param key [Symbol, String] The attribute to set the value for
|
16
|
+
# @param value [Object] The attribute value to set
|
17
|
+
# @return [Object] The attribute value
|
18
|
+
# @api public
|
10
19
|
def []=(key,value)
|
11
20
|
send :"#{key}=", value
|
12
21
|
rescue NoMethodError
|
13
22
|
liner_set key, value
|
14
23
|
end
|
15
24
|
|
25
|
+
# Build a hash of liner attributes
|
26
|
+
# @return [Hash] A hash of liner attributes
|
27
|
+
# @api public
|
16
28
|
def liner
|
17
29
|
liner_keys.inject({}) { |h,k| h[k] = self[k]; h }.freeze
|
18
30
|
end
|
19
31
|
|
32
|
+
# Set multiple attribute values
|
33
|
+
# @param hash [Hash] A hash of attribute-value pairs to set
|
34
|
+
# @return [Hash]
|
35
|
+
# @api public
|
20
36
|
def liner=(hash)
|
21
37
|
hash.each { |k,v| self[k] = hash[k] }
|
22
38
|
end
|
23
39
|
|
40
|
+
# Set mulitple attribute values
|
41
|
+
# @param values [Array] Ordered array of attribute values
|
42
|
+
# @return [Array]
|
43
|
+
# @api public
|
24
44
|
def liner_values=(values)
|
25
45
|
values.each_with_index do |value, i|
|
26
46
|
self[liner_keys[i]] = value
|
27
47
|
end
|
28
48
|
end
|
29
49
|
|
50
|
+
# Get a hash of attributes
|
51
|
+
# @return [Hash] An attribute-value hash
|
52
|
+
# @api public
|
30
53
|
def to_h
|
31
54
|
liner.dup
|
32
55
|
end
|
data/lib/liner/inspectable.rb
CHANGED
data/lib/liner/serializable.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
module Liner
|
2
2
|
module Serializable
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
# Convert a liner instances attributes to json
|
4
|
+
# @return [String] A JSON string of attributes
|
5
|
+
# @api public
|
7
6
|
def to_json(*args)
|
8
7
|
as_json.to_json(*args)
|
9
8
|
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
# @api private
|
13
|
+
def as_json(*)
|
14
|
+
to_h
|
15
|
+
end
|
10
16
|
end
|
11
17
|
end
|
data/lib/liner/version.rb
CHANGED
data/test/liner_test.rb
CHANGED
@@ -9,9 +9,9 @@ describe Liner do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should define liner_keys" do
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
Beer.liner_keys.must_equal [:hops, :yeast]
|
13
|
+
Pizza.liner_keys.must_equal [:crust, :sauce]
|
14
|
+
Cheeseburger.liner_keys.must_equal [:bun, :meat, :cheese]
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should define attribute getters" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: liner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lewis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|