attire 2.0.1 → 3.0.0

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: c4631d29300b437d384ef7c0873628c7e133eaff
4
- data.tar.gz: 2ce7528a9f0b6ce2a8d919c68637f5f644e2530c
3
+ metadata.gz: 9e182d55a0029623e5a05e47f92701bbe3fd55e2
4
+ data.tar.gz: 2d209d7daf659e2bef9e5f7da389641ab0d7c99f
5
5
  SHA512:
6
- metadata.gz: 21c05ea5b7dd1af7817e4cd41f95c4b6f0740b21b5e20000ce96c129ff77ecb7e84b1442549f604b002d9e5ab180dd7baedcf0b0d5e8ca15da50c9bc28ca686d
7
- data.tar.gz: 2f0ff51cda93ad0d1a06c29f304bd869588c0910483ecb21aa1a8709209f3a887e024d0a9a1a476a0bf02f169f20e1832565a1bae95c708134311af4ba801844
6
+ metadata.gz: 5fa120c1f70fedb9a5a3f93173444bd054269ba6eaa9d9881b5e89b24b39aad1d33abdc25c6c1940138e8d4562a003c87fd4531c5313a9cbbe50bfbded357cf0
7
+ data.tar.gz: 8b0e10d88064358730c4ff416e5f9f90b56b6fd01a951389651ccb6e205e47528e78b6bf227226d34fec0e0dfe286138384901c4df3e799c0e8ce03a2134a5f2
data/README.md CHANGED
@@ -3,43 +3,50 @@
3
3
  [![Build Status](https://travis-ci.org/mushishi78/attire.svg?branch=master)](https://travis-ci.org/mushishi78/attire)
4
4
  [![Gem Version](https://badge.fury.io/rb/attire.svg)](http://badge.fury.io/rb/attire)
5
5
 
6
- Mixins to remove some boiler plate in defining classes.
6
+ Helper to remove some boiler plate in defining classes.
7
7
 
8
- **N.B. This is the README for version 2.0. This release is written for Ruby 2.0 or higher. For previous versions please consult the tagged releases.**
8
+ ## Usage
9
9
 
10
- ## Initializer
10
+ The `attire` method defines an `initialize` method where all it's parameters are stored as instance variables that can be retrieve with private getters. So a class defined like this:
11
11
 
12
- `Attire::Initializer` extends a subsequent initialize method so that all it's parameters are assigned to instance variables and they can all be reached from private getters. For example:
12
+ ```ruby
13
+ class Measurement
14
+ def initialize(value:, units: :grams)
15
+ @value = value
16
+ @units = units
17
+ end
13
18
 
14
- ``` ruby
15
- require 'attire'
19
+ def to_s
20
+ "#{value} (#{units})"
21
+ end
16
22
 
17
- class MyClass
18
- extend Attire::Initializer
23
+ private
19
24
 
20
- def initialize(foo:, bar: 24)
21
- @bar = bar * 2
22
- end
25
+ attr_reader :value, :units
26
+ end
27
+ ```
23
28
 
24
- def result
25
- foo + bar
29
+ Can be shortened to:
30
+
31
+ ```ruby
32
+ require 'attire'
33
+
34
+ class Measurement
35
+ attire 'value:, units: :grams'
36
+
37
+ def to_s
38
+ "#{value} (#{units})"
26
39
  end
27
40
  end
28
-
29
- my_instance = MyClass.new(foo: 50)
30
- my_instance.result # 98
31
41
  ```
32
42
 
33
- ## MethodObject
43
+ ### Method Objects
34
44
 
35
- `Attire::MethodObject` does the same as `Attire::Initializer` but it also adds a singleton method that creates an instance and then calls an instance method of the same name. This is useful for objects that are designed to do one task. For example:
45
+ Sometimes it's useful for objects that are designed to do only a single task to have a class method that both initializes the object and executes the task. For this purpose, `attire` allows you to set the `verb` keyword like so:
36
46
 
37
47
  ``` ruby
38
48
  class CheeseSpreader
39
- extend Attire::MethodObject.new(:spread)
40
-
41
- def initialize(cheese, cracker: Jacobs.new)
42
- end
49
+ attire 'cheese, cracker: Jacobs.new', verb: :spread
43
50
 
44
51
  def spread
45
52
  cracker.spreads << cheese
data/lib/attire.rb CHANGED
@@ -1,2 +1,32 @@
1
- require 'attire/initializer'
2
- require 'attire/method_object'
1
+ module Attire
2
+ def attire(param_str, method: :initialize, verb: nil, &after)
3
+ names = eval("->(#{param_str}){}").parameters.map(&:last)
4
+ def_init(param_str, method, names)
5
+ add_getters(names)
6
+ define_method(:__after) { instance_exec(&after) if after }
7
+ def_verb(verb) if verb
8
+ end
9
+
10
+ private
11
+
12
+ def def_init(param_str, method, names)
13
+ class_eval "def #{method}(#{param_str})\n#{ivars(names)}\n__after\nend"
14
+ end
15
+
16
+ def ivars(names)
17
+ names.map { |name| "@#{name} = #{name}\n" }.join
18
+ end
19
+
20
+ def add_getters(names)
21
+ attr_reader(*names)
22
+ private(*names)
23
+ end
24
+
25
+ def def_verb(verb)
26
+ define_singleton_method(verb) { |*a, **k, &b| new(*a, **k, &b).send(verb) }
27
+ end
28
+ end
29
+
30
+ class Module
31
+ include Attire
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attire
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max White
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-10 00:00:00.000000000 Z
11
+ date: 2015-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -30,20 +30,6 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.1.0
33
- - !ruby/object:Gem::Dependency
34
- name: contracts
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- type: :development
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: '0'
47
33
  description:
48
34
  email: mushishi78@gmail.com
49
35
  executables: []
@@ -53,8 +39,6 @@ files:
53
39
  - LICENSE.txt
54
40
  - README.md
55
41
  - lib/attire.rb
56
- - lib/attire/initializer.rb
57
- - lib/attire/method_object.rb
58
42
  homepage: https://github.com/mushishi78/attire
59
43
  licenses:
60
44
  - MIT
@@ -78,5 +62,5 @@ rubyforge_project:
78
62
  rubygems_version: 2.2.2
79
63
  signing_key:
80
64
  specification_version: 4
81
- summary: Mixins to remove some boiler plate in defining classes.
65
+ summary: Helper to remove some boiler plate in defining classes.
82
66
  test_files: []
@@ -1,51 +0,0 @@
1
- module Attire
2
- module Initializer
3
- def self.extended(base)
4
- base.class.instance_variable_set(:@_added, false)
5
- end
6
-
7
- def method_added(method_name)
8
- return super unless method_name == :initialize && !added?
9
-
10
- self.added = true
11
- method = instance_method(:initialize)
12
- super
13
-
14
- names = method.parameters.map(&:last)
15
- add_initialize(method, names)
16
- add_getters(names)
17
- end
18
-
19
- private
20
-
21
- def added?
22
- self.class.instance_variable_get(:@_added)
23
- end
24
-
25
- def added=(added)
26
- self.class.instance_variable_set(:@_added, added)
27
- end
28
-
29
- def add_initialize(method, names)
30
- initialize_line = initialize_line(method)
31
- set_ivars = names.map { |name| "@#{name} = #{name}\n" }.join
32
-
33
- initializer = Module.new do
34
- class_eval "#{initialize_line}#{set_ivars}super\nend\n"
35
- end
36
- prepend initializer
37
- end
38
-
39
- def initialize_line(method)
40
- file, endline = *method.source_location
41
- File.readlines(file)[0..endline].reverse.each do |line|
42
- return line if line.include?('initialize')
43
- end
44
- end
45
-
46
- def add_getters(names)
47
- attr_reader(*names)
48
- private(*names)
49
- end
50
- end
51
- end
@@ -1,12 +0,0 @@
1
- require_relative 'initializer'
2
-
3
- module Attire
4
- module MethodObject
5
- def self.new(verb)
6
- Module.new do
7
- define_singleton_method(:extended) { |base| base.extend Initializer }
8
- define_method(verb) { |*a, **k, &b| new(*a, **k, &b).send(verb) }
9
- end
10
- end
11
- end
12
- end