Skip to content

Commit

Permalink
Set ruby code in readme to be highlighted on github
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Bera committed Jun 21, 2014
1 parent d262670 commit 81ac3d0
Showing 1 changed file with 59 additions and 45 deletions.
104 changes: 59 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Installation

The usual stuff. Either

gem 'jerry'
```ruby
gem 'jerry'
```

then

Expand All @@ -27,37 +29,45 @@ Usage

Let's say you have the following code:

class Window; end
class Door; end

class House
attr_reader :window, :door
def initialize(window, door)
@window = window
@door = door
end
end
```ruby
class Window; end
class Door; end

class House
attr_reader :window, :door

def initialize(window, door)
@window = window
@door = door
end
end
```

First, require jerry:

require 'jerry'
```ruby
require 'jerry'
```

Then, define a config class. This class tells jerry how to construct your application.

class MyConfig < Jerry::Config
component(:window) { Window.new }
component(:door) { Door.new }
component(:house) { House.new rig(:window), rig(:door) }
end
```ruby
class MyConfig < Jerry::Config
component(:window) { Window.new }
component(:door) { Door.new }
component(:house) { House.new rig(:window), rig(:door) }
end
```

The `component` method defines a component. Usually you'll want a component per class. The `rig` method tells jerry to
build a component.

Finally, when you want to build your application, ask jerry to do it for you.

jerry = Jerry.new MyConfig.new
house = jerry.rig :house
```ruby
jerry = Jerry.new MyConfig.new
house = jerry.rig :house
```

Scopes
------
Expand All @@ -66,21 +76,23 @@ The `component` method lets you specify a scope. The scope can either be `:singl
`:single`. If you set the scope to `:single` only one instance of the component will be created. If you set it to
`:instance`, a new instance of the component will be created each time you call `rig`.

class MyConfig < Jerry::Config
component(:window, scope: :instance) { Window.new }
component(:door, scope: :single) { Door.new }
end
jerry = Jerry.new MyConfig.new

window_a = jerry.rig :window
window_b = jerry.rig :window
window_a.object_id == window_b.object_id
#=> false

door_a = jerry.rig :door
door_b = jerry.rig :door
door_a.object_id == door_b.object_id
#=> true
```ruby
class MyConfig < Jerry::Config
component(:window, scope: :instance) { Window.new }
component(:door, scope: :single) { Door.new }
end
jerry = Jerry.new MyConfig.new

window_a = jerry.rig :window
window_b = jerry.rig :window
window_a.object_id == window_b.object_id
#=> false

door_a = jerry.rig :door
door_b = jerry.rig :door
door_a.object_id == door_b.object_id
#=> true
```

Multiple configs
----------------
Expand All @@ -91,13 +103,15 @@ configs to `Jerry.new` or you can use `jerry << SomeConfig.new` to add configs t
If two configs define the same component, the config that was inserted last will have priority. With `Jerry.new`, the
later arguments have priority.

class ConfA < Jerry::Config
component(:thing) { "from a" }
end
class ConfB < Jerry::Config
component(:thing) { "from b" }
end
jerry = Jerry.new ConfA.new, ConfB.new

jerry.rig :thing
#=> "from b"
```ruby
class ConfA < Jerry::Config
component(:thing) { "from a" }
end
class ConfB < Jerry::Config
component(:thing) { "from b" }
end
jerry = Jerry.new ConfA.new, ConfB.new

jerry.rig :thing
#=> "from b"
```

0 comments on commit 81ac3d0

Please sign in to comment.