Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 36 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,132 +1,76 @@
# ActiveMethod

Refactor your obscure method to a method object with `ActiveMethod`
Method is an object in Ruby, but we need **a real method object**. Welcome to `ActiveMethod`!

## Installation
- **Clean scope** without module
- **High maintainability** with tiny method objects
- Make wrting OO **easy**, then writing OO **everywhere**

Install the gem and add to the application's Gemfile by executing:
![](./hero.png)

$ bundle add active_method
## Installation

If bundler is not being used to manage dependencies, install the gem by executing:
```bash
$ bundle add active_method
```

$ gem install active_method
## API

## Usage
**It just do what a method do, and nothing else!**

Refactor `Foo#bar` to `Bar` with `ActiveMethod`
### Define method object

```ruby
class Foo
def bar(a, b, c: , d:)
puts "a: #{a}"
puts "b: #{b}"
puts "c: #{c}"
puts "d: #{d}"

buzz
end
class User
include ActiveMethod

def buzs
puts 'buzz'
end
active_method :foo
active_method :bar, MyBar
active_method :build_one, class_method: true
end

module Util
active_method :parse_url, module_function: true
end
```

Refactor to:
### Declaring arguments

```ruby
class Bar < ActiveMethod::Base
class Foo < ActiveMethod::Base
argument :a
argument :b, default: 2
keyword_argument :c
keyword_argument :d, default: 4

def call
puts "a: #{a}"
puts "b: #{b}"
puts "c: #{c}"
puts "d: #{d}"

foo.buzz
puts "a: #{a}, b: #{b}, c: #{c}, d: #{d}"
end
end

class Foo
include ActiveMethod

active_method :bar
end

Bar.call(1)
# => a: 1
# => b: 2
# => c: nil
# => d: 4
# => buzz

Bar.call(1, 3)
# => a: 1
# => b: 3
# => c: nil
# => d: 4
# => buzz

Bar.call(1, 3, c: 6)
# => a: 1
# => b: 3
# => c: 6
# => d: 4
# => buzz

Bar.call(1, 3, c: 4, d: 5)
# => a: 1
# => b: 3
# => c: 4
# => d: 5
# => buzz
User.new.foo('aa', 3, c: 'cc', d: 5)
#=> a: aa, b: 3, c: cc, d: 5
```

### Work on module
### Get the owner of the method

```ruby
class Hi < ActiveMethod::Base
argument :name

def call
"Hi, #{name}"
end
end

module Say
class User
include ActiveMethod

active_method :hi, module_function: true
attr_accessor :name
active_method :hi
end

Say.hi('John')
# => "Hi, John"
```

### Work as a class method

```ruby
class BuildARobot < ActiveMethod::Base
argument :name

class Hi < ActiveMethod::Base
def call
"#{robot_factory} build a robot called #{name}"
puts "Hi, I'm #{user.name}"
end
end

class RobotFactory
include ActiveMethod

active_method :build_a_robot, class_method: true
end

RobotFactory.build_a_robot
# => "RobotFactory build a robot called B-2"
user = User.new
user.name = 'ActiveMethod'
user.hi
#=> Hi, I'm ActiveMethod
```

## Development
Expand Down
Binary file added hero.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.