liner 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Liner
2
2
 
3
- Lay a liner for your basic Ruby classes. Basically, it's a solid foundation for a PORO (Plain Old Ruby Object). A Liner is something like a more flexible Struct or a less magical OpenStruct, with enhanced inheritability.
3
+ Lay a liner for your Ruby classes. Liner is designed to enhance simple classes
4
+ with some common idioms.
4
5
 
5
6
  ## Usage
6
7
 
@@ -19,27 +20,34 @@ class Engine
19
20
  end
20
21
  ```
21
22
 
22
- It comes with a hash based initializer and a nice inspector.
23
+ Your new class comes with an initializer that takes values in the order you
24
+ defined them.
23
25
  ```ruby
24
- e = Engine.new(layout: 'V8', fuel: "gasoline") # => #<Engine layout="V8", fuel="gasoline">
26
+ e = Engine.new('V6', 'gasoline')
27
+ # => #<Engine layout="V6", fuel="gasoline">
28
+ ```
29
+ Or, you can initialize with a hash if you prefer.
30
+ ```ruby
31
+ e = Engine.new(layout: 'V8', fuel: "gasoline")
32
+ # => #<Engine layout="V8", fuel="gasoline">
25
33
  ```
26
34
 
27
35
  Attribute getters and setters are built in.
28
36
  ```ruby
29
- e.fuel # => "gasoline"
37
+ e.fuel # => "gasoline"
30
38
  e.fuel = "diesel" # => "diesel"
31
39
  ```
32
40
 
33
- Attributes are accessible via hash-style lookup too.
41
+ Attributes are accessible via hash style lookup too.
34
42
  ```ruby
35
- e[:layout] # => "V8"
43
+ e[:layout] # => "V8"
36
44
  e[:layout] = "V6" # => "V6"
37
- e[:foo] = "Bar" # => ArgumentError: Invalid liner attribute: 'foo'
45
+ e[:foo] = "Bar" # => ArgumentError: Invalid liner attribute: 'foo'
38
46
  ```
39
47
 
40
48
  Equality methods are also availble.
41
49
  ```ruby
42
- e.eql? Engine.new(layout: 'I4') # => false
50
+ e.eql? Engine.new(layout: 'I4') # => false
43
51
  e == Engine.new(layout: 'V6', fuel: 'diesel') # => true
44
52
  ```
45
53
 
data/lib/liner.rb CHANGED
@@ -19,7 +19,7 @@ def Liner.new(*keys)
19
19
  end
20
20
 
21
21
  def Liner.apply(base, *keys)
22
- keys = keys.map(&:to_sym).uniq
22
+ keys = keys.map(&:to_sym).uniq.freeze
23
23
  base.class_eval do
24
24
  define_method(:liner_keys){ keys }
25
25
  include Liner
data/lib/liner/base.rb CHANGED
@@ -1,7 +1,13 @@
1
1
  module Liner
2
2
  module Base
3
- def initialize(hash)
4
- self.liner = hash
3
+ def initialize(*args)
4
+ if args.count == 1 && args.first.respond_to?(:keys)
5
+ self.liner = args.first
6
+ elsif args.count >= 1 && args.count >= liner_keys.count
7
+ self.liner_values = args
8
+ else
9
+ raise ArgumentError, "Liner doesn't know how to initialize with `#{args}`."
10
+ end
5
11
  end
6
12
  end
7
13
  end
@@ -22,6 +22,12 @@ module Liner
22
22
  end
23
23
  end
24
24
 
25
+ def liner_values=(values)
26
+ values.each_with_index do |v,i|
27
+ self[liner_keys[i]] = v
28
+ end
29
+ end
30
+
25
31
  def to_h
26
32
  liner.dup
27
33
  end
data/lib/liner/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Liner
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -0,0 +1,14 @@
1
+ describe Liner::Base do
2
+ describe :initialize do
3
+ it "should accept key/value pairs" do
4
+ beer = Beer.new(hops: 'Cascade', yeast: 'Top Fermenting')
5
+ beer.hops.must_equal 'Cascade'
6
+ beer.yeast.must_equal 'Top Fermenting'
7
+ end
8
+ it "should accept a list of values" do
9
+ burger = Burger.new('Sesame Seed', 'Soy', 'Cheddar')
10
+ burger.bun.must_equal 'Sesame Seed'
11
+ burger.cheese.must_equal 'Cheddar'
12
+ end
13
+ end
14
+ end
data/test/liner_test.rb CHANGED
@@ -3,23 +3,28 @@ describe Liner do
3
3
  let(:beer) { Beer.new(hops: 'columbus') }
4
4
  let(:pizza) { Pizza.new(crust: 'thin') }
5
5
  let(:burger){ Burger.new(bun: 'sesame') }
6
+
6
7
  it "should create a new class" do
7
8
  [Beer, Pizza, Burger].all?{|klass| klass.is_a? Class }
8
9
  end
10
+
9
11
  it "should define liner_keys" do
10
12
  beer.liner_keys.must_equal [:hops, :yeast]
11
13
  pizza.liner_keys.must_equal [:crust, :sauce]
12
14
  burger.liner_keys.must_equal [:bun, :meat, :cheese]
13
15
  end
16
+
14
17
  it "should define attribute getters" do
15
18
  beer.must_respond_to :hops
16
19
  pizza.must_respond_to :sauce
17
20
  burger.must_respond_to :meat
18
21
  end
22
+
19
23
  it "should define attribute setters" do
20
24
  beer.must_respond_to :yeast=
21
25
  pizza.must_respond_to :crust=
22
26
  burger.must_respond_to :bun=
23
27
  end
28
+
24
29
  end
25
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-02 00:00:00.000000000 Z
12
+ date: 2014-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -80,6 +80,7 @@ files:
80
80
  - lib/liner/serializable.rb
81
81
  - lib/liner/version.rb
82
82
  - liner.gemspec
83
+ - test/liner/base_test.rb
83
84
  - test/liner/equalizable_test.rb
84
85
  - test/liner/hashable_test.rb
85
86
  - test/liner/inspectable_test.rb
@@ -111,6 +112,7 @@ signing_key:
111
112
  specification_version: 3
112
113
  summary: A liner for Ruby objects. Add attribute, inspection, and equality methods.
113
114
  test_files:
115
+ - test/liner/base_test.rb
114
116
  - test/liner/equalizable_test.rb
115
117
  - test/liner/hashable_test.rb
116
118
  - test/liner/inspectable_test.rb