Class: Blueprints::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/blueprints/context.rb

Overview

Class that blueprint files are evaluated against. Has methods for setting and retrieving attributes and dependencies. Allows defining new blueprints and namespaces.

Constant Summary collapse

@@chain =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Context

Initializes new context with passed parent, attributes, dependencies, file and namespace. Attributes and dependencies are automatically merged with parents' attributes and dependencies. File and namespace are automatically set to parent counterparts unless they are explicitly changed.

Parameters:

  • options (Hash) (defaults to: {})

    Options for new context.

Options Hash (options):

  • :attributes (Hash) — default: {}

    List of attributes, merged with parent attributes.

  • :dependencies (Array<String, Symbol>) — default: []

    List of dependencies, merged with parent dependencies.

  • :file (Pathname)

    File this context is evaluated in. Should be passed for top level contexts only.

  • :namespace (Blueprints::Namespace)

    Namespace that new blueprints and namespaces should be children of.

  • :parent (Blueprints::Context)

    Parent context that is used to retrieve unchanged values.


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blueprints/context.rb', line 18

def initialize(options = {})
  options.assert_valid_keys(:dependencies, :attributes, :file, :parent, :namespace)
  @dependencies = (options[:dependencies] || []).collect(&:to_sym)
  @attributes   = options[:attributes] || {}
  @file         = options[:file]
  @namespace    = options[:namespace]

  if parent = options[:parent]
    @attributes.reverse_merge!(parent.attributes)
    @dependencies = (parent.dependencies + @dependencies).uniq
    @file         ||= parent.file
    @namespace    ||= parent.namespace
  end
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies


7
8
9
# File 'lib/blueprints/context.rb', line 7

def dependencies
  @dependencies
end

#fileObject (readonly)

Returns the value of attribute file


7
8
9
# File 'lib/blueprints/context.rb', line 7

def file
  @file
end

Class Method Details

.currentBlueprints::Context

Return current context.

Returns:


146
147
148
# File 'lib/blueprints/context.rb', line 146

def self.current
  @@chain.last
end

.eval_within_context(new_options, &block) ⇒ Object

Creates child context and sets it as current. Evaluates block and file within child context if any are passed.

Parameters:

  • new_options (Hash)

    Options for child context.


152
153
154
155
156
157
158
159
160
# File 'lib/blueprints/context.rb', line 152

def self.eval_within_context(new_options, &block)
  @@chain << context = new(new_options)

  file = new_options[:file]
  context.instance_eval(File.read(file), file) if file
  context.instance_eval(&block) if block

  @@chain.pop
end

Instance Method Details

#==(context) ⇒ true, false

Checks if two contexts are equal by comparing attributes, dependencies, namespace and file

Parameters:

Returns:

  • (true, false)

    Whether contexts are equal or not.


36
37
38
# File 'lib/blueprints/context.rb', line 36

def ==(context)
  @dependencies == context.dependencies and @attributes == context.attributes and @file == context.file and @namespace == context.namespace
end

#attributes(new_attributes, &block) ⇒ Blueprints::Context #attributesHash

Overloads:

  • #attributes(new_attributes, &block) ⇒ Blueprints::Context

    Yields and returns child context that has new attributes set.

    Examples:

    Define blueprint with attributes

    attributes(:name => 'User').blueprint(:user) do
      User.blueprint attributes
    end

    Define multiple blueprints with same attributes

    attributes(:name => 'User') do
      blueprint(:user1) do
        User.blueprint attributes
      end
    
      blueprint(:user2) do
        User.blueprint attributes
      end
    end

    Parameters:

    • new_attributes (Hash)

      Attributes for child context.

    Returns:

  • #attributesHash

    Returns attributes of context.

    Returns:

    • (Hash)

      Attributes of context.


95
96
97
98
99
100
101
# File 'lib/blueprints/context.rb', line 95

def attributes(new_attributes = nil, &block)
  if new_attributes
    with_context(:attributes => new_attributes, &block)
  else
    @attributes
  end
end

#blueprint(name = nil, &block) ⇒ Blueprints::Blueprint

Defines a new blueprint by name and block passed.

Examples:

Define blueprint.

blueprint :user do
  User.blueprint :name => 'User'
end

Parameters:

  • name (#to_sym, Hash) (defaults to: nil)

    Name of buildable. If hash is passed then first key is assumed name, and value(s) of that key are assumed as dependencies.

Returns:


47
48
49
# File 'lib/blueprints/context.rb', line 47

def blueprint(name = nil, &block)
  Blueprint.new(name, self, &block)
end

#d(name, options = {}) ⇒ Object #d(name, instance_variable_name, options = {}) ⇒ Object

Initializes new Blueprints::Dependency object.


132
133
134
# File 'lib/blueprints/context.rb', line 132

def d(*args)
  Dependency.new(*args)
end

#depends_on(*new_dependencies, &block) ⇒ Blueprints::Context

Yields and returns child context that has dependencies set.

Examples:

Define blueprint with dependencies

depends_on(:user, :admin).blueprint(:user_and_admin)

Define multiple blueprints with same dependencies.

depends_on :user, :admin do
  blueprint :user_and_admin
  blueprint :admin_and_user
end

Parameters:

  • new_dependencies (Array<Symbol, String>)

    Dependencies for child context.

Returns:


113
114
115
# File 'lib/blueprints/context.rb', line 113

def depends_on(*new_dependencies, &block)
  with_context(:dependencies => new_dependencies, &block)
end

#find(path) ⇒ Blueprints::Buildable Also known as: []

Finds blueprint/namespace by it's path

Parameters:

  • path (String)

    Path to child. Path parts should be joined with '.' symbol.

Returns:


139
140
141
# File 'lib/blueprints/context.rb', line 139

def find(path)
  @namespace[path]
end

#namespace(name, &block) ⇒ Blueprints::Namespace #namespaceBlueprints::Namespace

Overloads:

  • #namespace(name, &block) ⇒ Blueprints::Namespace

    Defines new namespace by name, and evaluates block against it.

    Examples:

    Define namespace and blueprint in it.

    namespace :banned do
      blueprint :user do
        User.blueprint :name => 'User'
      end
    end

    Parameters:

    • name (String, Symbol)

      Name of namespace.

    Returns:

  • #namespaceBlueprints::Namespace

    Returns namespace for this context.

    Returns:


64
65
66
67
68
69
70
71
72
# File 'lib/blueprints/context.rb', line 64

def namespace(name = nil, &block)
  if name
    Namespace.new(name, self).tap do |namespace|
      with_context(:namespace => namespace, &block)
    end
  else
    @namespace
  end
end

#with_context(options, &block) ⇒ Blueprints::Context

Yields and returns child context that has new options set.

Parameters:

  • options (Hash)

    Options for new context.

Returns:


120
121
122
# File 'lib/blueprints/context.rb', line 120

def with_context(options, &block)
  Context.eval_within_context(options.merge(:parent => self), &block)
end