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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53f068a96a64299ca295a0c1821a5232a4ec2618
4
- data.tar.gz: 459fd2b8e24e184a5437744fa1ae3701c384d609
3
+ metadata.gz: 7bbfb3aba7d5aa9e37d5b7f9bf3192ba358ed87d
4
+ data.tar.gz: 9b69f55a62fb0e5d0ef4b4de8ccd3427bf64b190
5
5
  SHA512:
6
- metadata.gz: 98f7de6a0c3580a6dd7c8774aa7b427b7819c30a28ad1dce9fdfd362fd3c6a9742df59f2c9a0b64508e943a802a3fa422c3f442c6529827d7acf5dab9e943bd1
7
- data.tar.gz: 7d8e902c6662b26f751bee45f120855c21e3a300a66532fe536d4be439f5063745a352691f97788cd0461086a88bba3b6ff269327354d75c0761b381cf29abf3
6
+ metadata.gz: b07b23e891919b936ed3a1ad2f42db641490e558237547b2d8a85c234489d0815d089cedd738aa13d34fedeef9ba7a79fdc4b09312f963d4ab9193fbc54c9d0c
7
+ data.tar.gz: 7a1445ce77473f7f1fad7c63ad876780126b52dcd0b4ade0f0b869590e95cb4ce79322d9bff14cc9e77ff3c08088b2383fbbc6c1f6713d9c587f7011bda89151
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'pry'
4
3
  gemspec
4
+
5
+ gem 'pry'
6
+ gem 'yard'
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
- `read_attribute`, or `super`.
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, `write_attribute`, or `super`.
133
+ through the instance variable, `liner_set`, or `super`.
134
134
 
135
135
  ```ruby
136
136
  class Bacon < Liner.new(:is_good)
@@ -7,49 +7,62 @@ require 'liner/serializable'
7
7
  require 'ext/class'
8
8
 
9
9
  module Liner
10
- # include all submodules when included
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 self.included(base)
13
- [Base, Hashable, Equalizable, Inspectable, Serializable].each do |mod|
14
- base.send :include, mod
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
- # Setup an anonymous class with liner keys
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
- # Apply a liner to a given class
29
- # @param base [Class] A class to add a liner to
30
- # @param keys [Array] An array of symbols or strings which will serve as attributes
31
- # @return [Class] The class with a liner installed
32
- # @api public
33
- def Liner.apply!(base, *keys)
34
- keys = keys.map(&:to_sym).uniq.freeze
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
- base.send :include, Liner unless base < Liner
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
- keys.each do |key|
47
- unless base.method_defined? key
48
- base.send(:define_method, key){ liner_get key }
49
- end
50
- unless base.method_defined? "#{key}="
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
+
@@ -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
@@ -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)
@@ -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
@@ -1,5 +1,8 @@
1
1
  module Liner
2
2
  module Inspectable
3
+ # A handy inspection string
4
+ # @return [String]
5
+ # @api public
3
6
  def inspect
4
7
  attribute_string = liner.map{|k,v| "#{k}=#{v.inspect}"}.join(', ')
5
8
  "#<#{self.class} #{attribute_string}>"
@@ -1,11 +1,17 @@
1
1
  module Liner
2
2
  module Serializable
3
- def as_json(*)
4
- to_h
5
- end
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
@@ -1,3 +1,3 @@
1
1
  module Liner
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -9,9 +9,9 @@ describe Liner do
9
9
  end
10
10
 
11
11
  it "should define liner_keys" do
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]
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.0
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-06 00:00:00.000000000 Z
11
+ date: 2014-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler