Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
al6x committed Oct 30, 2011
1 parent 3f0da4e commit 951c6da
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 90 deletions.
5 changes: 2 additions & 3 deletions Rakefile
@@ -1,10 +1,9 @@
require 'rake_ext'

project(
project \
name: "micon",
gem: true,
summary: "Silent killer of dependencies and configs",

author: "Alexey Petrushin",
homepage: "http://alexeypetrushin.github.com/micon"
)
homepage: "http://alexeypetrushin.github.com/micon"
2 changes: 1 addition & 1 deletion docs/basics.rb
Expand Up @@ -44,7 +44,7 @@

class Application
# Whiring the `:logger` component and application together.
inject logger: :logger
inject :logger

# Now You can use `:logger` as if it's an usual class member.
def run
Expand Down
2 changes: 1 addition & 1 deletion docs/ultima2.rb
Expand Up @@ -32,7 +32,7 @@
# to manually configure and create it.
class Ultima
# Making `:router` and `:request` components available as attributes.
inject router: :router, request: :request
inject :router, :request

# The main method of our framework, here we create request,
# decoding url and calling controller to generate some output.
Expand Down
3 changes: 0 additions & 3 deletions fake_gem

This file was deleted.

4 changes: 2 additions & 2 deletions lib/micon/config.rb
Expand Up @@ -7,7 +7,7 @@ def initialize micon, name
def load
files = []
files.push *config_paths.collect{|path| find_file(path, $LOAD_PATH)}
files.push *runtime_config_paths.collect{|path| find_file(path, [micon.runtime_path])} if micon.runtime_path?
files.push *runtime_config_paths.collect{|path| find_file(path, [micon.runtime_path])}

config = {}
files.compact.each do |f|
Expand Down Expand Up @@ -41,7 +41,7 @@ def runtime_config_paths

def find_file path, directories
files = directories.collect{|dir| "#{dir}#{path}"}.select{|f| File.exist? f}
raise "multiple configs for :#{name} component" if files.size > 1
raise %(multiple configs for :#{name} component ('#{files.join("', '")}')) if files.size > 1
files.first
end
end
22 changes: 15 additions & 7 deletions lib/micon/core.rb
Expand Up @@ -5,7 +5,7 @@ class Micon::Core

attr_accessor :custom_scopes

def activate sname, container, &block
def activate sname, container = {}, &block
raise_without_self "Only custom scopes can be activated!" if sname == :application or sname == :instance
raise "container should have type of Hash but has #{container.class.name}" unless container.is_a? Hash

Expand Down Expand Up @@ -237,7 +237,7 @@ def deinitialize!
end

# `runtime_path` is used to search for component configurations, it may be `app/runtime` for example..
def runtime_path; @runtime_path || raise(":runtime_path not defined!") end
def runtime_path; @runtime_path ||= File.expand_path('.') end
def runtime_path= runtime_path
runtime_path, force = runtime_path
raise "some components has been already initialized before You set :runtime_path!" unless empty? or force
Expand All @@ -248,14 +248,26 @@ def runtime_path?; !!@runtime_path end
# `mode` used to search for component configuration, examples:
# - `app/runtime/logger.production.yml`
# - `app/runtime/production/logger.yml`
def mode; @mode || raise(":mode not defined!") end
def mode; @mode ||= :development end
def mode= mode
mode, force = mode
raise "some components has been already initialized before You set :mode!" unless empty? or force
@mode = mode
end
def mode?; !!@mode end

def development?; mode == :development end
def production?; mode == :production end
def test?; mode == :test end

def development █ block.call if development? end
def production █ block.call if production? end
def test █ block.call if test? end

def raise_without_self message
raise RuntimeError, message, caller.select{|path| path !~ /\/lib\/micon\//}
end

protected
def autoload_component_definition key, bang = true
begin
Expand Down Expand Up @@ -359,10 +371,6 @@ def name_hack namespace
end
end

def raise_without_self message
raise RuntimeError, message, caller.select{|path| path !~ /\/lib\/micon\//}
end

# Generates helper methods, so you can use `micon.logger` instead of `micon[:logger]`
def method_missing m, *args, &block
super if args.size > 1 or block
Expand Down
20 changes: 12 additions & 8 deletions lib/micon/support.rb
Expand Up @@ -8,15 +8,19 @@ def register_as *args
end

Module.class_eval do
# Usage: `inject logger: :logger`.
def inject attributes
::MICON.raise_without_self "Invalid argument!" unless attributes.is_a? Hash
attributes.each do |name, specificator|
::MICON.raise_without_self "Attribute name should be a Symbol!" unless name.is_a? Symbol
# Usage: `inject :logger` or `inject logger: :logger`.
def inject *attributes
options = attributes.last.is_a?(Hash) ? attributes.pop : {}
attributes.each{|name| options[name] = name}

define_method(name){::MICON[specificator]}
define_method("#{name}="){|value| ::MICON[specificator] = value}
define_method("#{name}?"){::MICON.include? specificator}
options.each do |attr_name, component_name|
unless attr_name.is_a? Symbol
::MICON.raise_without_self "attribute name #{attr_name} should be a Symbol!"
end

define_method(attr_name){::MICON[component_name]}
define_method("#{attr_name}="){|component| ::MICON[component_name] = component}
define_method("#{attr_name}?"){::MICON.include? component_name}
end
end
end
58 changes: 58 additions & 0 deletions old/managed_spec.rb
@@ -0,0 +1,58 @@
require 'spec_helper'

describe "Managed" do
before{self.micon = Micon::Core.new}
after{remove_constants :Tmp}

it "scope" do
class Tmp
register_as :an_object
end

scope = micon.metadata[:an_object]
initializer, dependencies = micon.metadata.initializers[:an_object]
scope.should == :application
initializer.call.should be_a(Tmp)
end

it "injection" do
class Tmp
inject object: :an_object

class << self
inject object: :an_object
end
end

the_object = "An Object"
micon.register(:an_object){the_object}

Tmp.object.should == the_object
Tmp.new.object.should == the_object
end

it "outjection" do
class Tmp
class << self
inject object: :an_object
end
end

the_object = "An Object"
micon.register :an_object

-> {Tmp.object}.should raise_error(/no initializer/)
Tmp.object = the_object
Tmp.object.should == the_object
end

it "empty?" do
class Tmp
register_as :an_object
end

micon.should be_empty
micon[:an_object]
micon.should_not be_empty
end
end
4 changes: 2 additions & 2 deletions old/v1/examples/web_framework1.rb
Expand Up @@ -46,7 +46,7 @@ def decode request;
# don't register it as component.
class PagesController
# We need access to :logger and :request, let's inject them
inject logger: :logger, request: :request
inject :logger, :request

def index
# Here we can use injected component
Expand All @@ -69,7 +69,7 @@ def to_s; @url end
# When the server receive web request, it calls the :call method of our RackAdapter
class RackAdapter
# Injecting components
inject request: :request, controller: :controller
inject :request, :controller

def call env
# We need to tell Micon that the :request scope is started, so it will know
Expand Down
1 change: 0 additions & 1 deletion old/v1/examples/web_framework2.rb
Expand Up @@ -37,7 +37,6 @@ def micon; MICON end
# Note, that there are also logger.production.yml, Micon is smart
# and will merge configs in the following order:
# logger.yml <- logger.<env>.yml <- <runtime_path>/config/logger.yml
# (If you define :environment and :runtime_path variables).

# Let's pretend that there's a Web Server and run our application,
# You should see something like this in the console:
Expand Down
2 changes: 1 addition & 1 deletion old/v1/examples/web_framework2/lib/pages_controller.rb
Expand Up @@ -2,7 +2,7 @@
# don't register it as component.
class PagesController
# We need access to :logger and :request, let's inject them
inject logger: :logger, request: :request
inject :logger, :request

def index
# Here we can use injected component
Expand Down
2 changes: 1 addition & 1 deletion old/v1/examples/web_framework2/lib/rack_adapter.rb
Expand Up @@ -2,7 +2,7 @@
# When the server receive web request, it calls the :call method of our RackAdapter
class RackAdapter
# Injecting components
inject request: :request, controller: :controller
inject :request, :controller

def call env
# We need to tell Micon that the :request scope is started, so it will know
Expand Down
5 changes: 2 additions & 3 deletions old/v1/readme.md
Expand Up @@ -61,7 +61,7 @@ micon.register :controller, scope: :request
# don't register it as component.
class PagesController
# We need access to :logger and :request, let's inject them
inject logger: :logger, request: :request
inject :logger, :request

def index
# Here we can use injected component
Expand All @@ -84,7 +84,7 @@ micon.register :request, scope: :request
# When the server receive web request, it calls the :call method of our RackAdapter
class RackAdapter
# Injecting components
inject request: :request, controller: :controller
inject :request, :controller

def call env
# We need to tell Micon that the :request scope is started, so it will know
Expand Down Expand Up @@ -161,7 +161,6 @@ autoload_path lib_dir
# Note, that there are also logger.production.yml, Micon is smart
# and will merge configs in the following order:
# logger.yml <- logger.<env>.yml <- <runtime_path>/config/logger.yml
# (If you define :environment and :runtime_path variables).

# Let's pretend that there's a Web Server and run our application,
# You should see something like this in the console:
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -47,7 +47,7 @@ micon.register(:logger){Logger.new STDOUT}

class Application
# Whiring the `:logger` component and application together.
inject logger: :logger
inject :logger

# Now You can use `:logger` as if it's an usual class member.
def run
Expand Down
40 changes: 40 additions & 0 deletions spec/helpers_spec.rb
@@ -0,0 +1,40 @@
require 'spec_helper'

describe "Helpers" do
before{self.micon = Micon::Core.new}
after{remove_constants :Tmp}

it "register_as" do
class Tmp
micon.should_receive :register do |name, &initializer|
name.should == :an_object
initializer.call.class.should == Tmp
end

register_as :an_object
end
end

it "inject" do
class Tmp
inject :an_object
end
tmp = Tmp.new

micon.should_receive(:[]).with :an_object
tmp.an_object

micon.should_receive(:[]=).with :an_object, 'An Object'
tmp.an_object = 'An Object'

micon.should_receive(:include?).with :an_object
tmp.an_object?

# Another form.
class Tmp
inject other: :other_object
end
micon.should_receive(:[]).with :other_object
tmp.other
end
end
51 changes: 0 additions & 51 deletions spec/managed_spec.rb

This file was deleted.

0 comments on commit 951c6da

Please sign in to comment.