Module: AeEasy::Core

Defined in:
lib/ae_easy/core.rb,
lib/ae_easy/core/mock.rb,
lib/ae_easy/core/config.rb,
lib/ae_easy/core/helper.rb,
lib/ae_easy/core/plugin.rb,
lib/ae_easy/core/version.rb,
lib/ae_easy/core/exception.rb,
lib/ae_easy/core/mock/fake_db.rb,
lib/ae_easy/core/helper/cookie.rb,
lib/ae_easy/core/plugin/parser.rb,
lib/ae_easy/core/plugin/seeder.rb,
lib/ae_easy/core/plugin/executor.rb,
lib/ae_easy/core/mock/fake_parser.rb,
lib/ae_easy/core/mock/fake_seeder.rb,
lib/ae_easy/core/smart_collection.rb,
lib/ae_easy/core/mock/fake_executor.rb,
lib/ae_easy/core/plugin/config_behavior.rb,
lib/ae_easy/core/plugin/initialize_hook.rb,
lib/ae_easy/core/plugin/parser_behavior.rb,
lib/ae_easy/core/plugin/seeder_behavior.rb,
lib/ae_easy/core/plugin/collection_vault.rb,
lib/ae_easy/core/exception/outdated_error.rb,
lib/ae_easy/core/plugin/executor_behavior.rb,
lib/ae_easy/core/plugin/context_integrator.rb

Defined Under Namespace

Modules: Exception, Helper, Mock, Plugin Classes: Config, SmartCollection

Constant Summary collapse

VERSION =

Gem version

"0.1.6"

Class Method Summary collapse

Class Method Details

.all_scripts(dir, opts = {}) {|path| ... } ⇒ Object

Execute an action for all scripts within a directory.

Parameters:

  • dir (String)

    Directory containing `.rb` scripts.

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

    ({}) Configuration options.

Options Hash (opts):

  • :except (Array) — default: nil

    Literal file collection excluded from process.

Yield Parameters:

  • path (String)

    Script file path.



30
31
32
33
34
35
# File 'lib/ae_easy/core.rb', line 30

def all_scripts dir, opts = {}, &block
  excluded_files = (opts[:except] || []).map{|f|File.expand_path File.join(dir, f)}
  files = Dir[File.join(File.expand_path(dir), '*.rb')] - excluded_files
  block ||= proc{}
  files.sort.each &block
end

.analyze_compatibility(origin, fragment) ⇒ Hash

Generate a compatibility report from a origin and a fragment as a hash.

Examples:

Analyze when uncompatible `fragment` because of `origin` missing fields.

AeEasy::Core.analyze_compatibility [1,2,3,4,5], [1,2,6]
# => {missing: [6], new: [3,4,5], is_compatible: false}

Analyze when compatible.

AeEasy::Core.analyze_compatibility [1,2,3,4,5], [1,2,3]
# => {missing: [], new: [4,5], is_compatible: true}

Parameters:

  • origin (Array)

    Item collection to represent the universe.

  • fragment (Array)

    Item collection to compare againt origin.

Returns:

  • (Hash)
    • `:missing [Array]` (`[]`) Methods on `fragment` only.

    • `:new [Array]` (`[]`) Methods on `origin` only.

    • `:is_compatible [Boolean]` true when all `fragment`'s methods are present on `origin`.



197
198
199
200
201
202
203
204
# File 'lib/ae_easy/core.rb', line 197

def analyze_compatibility origin, fragment
  intersection = origin & fragment
  {
    missing: fragment - intersection,
    new: origin - intersection,
    is_compatible: (intersection.count == fragment.count)
  }
end

.deep_clone(hash, should_clone = false) ⇒ Hash

Deep clone a hash while keeping it's values object references.

Parameters:

  • hash (Hash)

    Hash to clone.

  • should_clone (Boolean) (defaults to: false)

    (false) Clone values when true.

Returns:

  • (Hash)

    Hash clone.



247
248
249
250
251
252
253
254
# File 'lib/ae_easy/core.rb', line 247

def deep_clone hash, should_clone = false
  target = {}
  hash.each do |key, value|
    value = value.is_a?(Hash) ? deep_clone(value, should_clone) : (should_clone ? value.clone : value)
    target[key] = value
  end
  target
end

.deep_stringify_keys(hash, should_clone = true) ⇒ Hash

Deep stringify keys from a hash.

Parameters:

  • hash (Hash)

    Source hash to stringify keys.

  • should_clone (Boolean) (defaults to: true)

    (true) Target a hash clone to avoid affecting the same hash object.

Returns:

  • (Hash)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ae_easy/core.rb', line 212

def deep_stringify_keys hash, should_clone = true
  return hash unless hash.is_a? Hash
  pair_collection = hash.map{|k,v| [k.to_s,v]}
  target = should_clone ? {} : hash
  target.clear
  pair_collection.each do |pair|
    key, value = pair
    if value.is_a? Array
      array = []
      value.each do |item|
        array << deep_stringify_keys(item, should_clone)
      end
      target[key] = array
      next
    end
    target[key] = deep_stringify_keys(value, should_clone)
  end
  target
end

.deep_stringify_keys!(hash) ⇒ Hash

Deep stringify all keys on hash object.

Parameters:

  • hash (Hash)

    Hash to stringify keys.

Returns:

  • (Hash)


237
238
239
# File 'lib/ae_easy/core.rb', line 237

def deep_stringify_keys! hash
  deep_stringify_keys hash, false
end

.expose_to(object, env) ⇒ Object

Expose an environment into an object instance as methods.

Examples:

class Foo
  def hello_person
    'Hello person!'
  end
end

env = {
  hello_world: lambda{return 'Hello world!'},
  hello_sky: proc{return 'Hello sky!'}
}
my_object = Foo.new
AeEasy::Core.expose_to my_object, env

puts my_object.hello_world
# => 'Hello world!'
puts my_object.hello_sky
# => 'Hello sky!'
puts my_object.hello_person
# => 'Hello person!'

Parameters:

  • object

    Object instance to expose env into.

  • env (Array)

    Hash with methods name as keys and blocks as actions.

Returns:

  • `object`



97
98
99
100
101
102
103
# File 'lib/ae_easy/core.rb', line 97

def expose_to object, env
  metaclass = class << object; self; end
  env.each do |key, block|
    metaclass.send(:define_method, key, block)
  end
  object
end

.instance_methods_from(object, class_only = false) ⇒ Array

Retrieve instance methods from an object.

Examples:

class Foo
  def hello_world
    'Hello world!'
  end

  def hello_person
    'Hello person!'
  end
end

my_object = Foo.new
AeEasy::Core.instance_methods_from my_object
# => [:hello_world, :hello_person]

Parameters:

  • object

    Object with instance methods.

  • class_only (false) (defaults to: false)

    Will get class only methods when `true`.

Returns:

  • (Array)


126
127
128
# File 'lib/ae_easy/core.rb', line 126

def instance_methods_from object, class_only = false
  object.methods(!class_only) - Object.new.methods(!class_only)
end

.mock_instance_methods(origin, target) ⇒ Object

Mock instances methods from the origin into target object.

Examples:

class Boo
  attr_accessor :message
  def initialize
    message = 'Hello world!'
  end

  def hello_world
    message
  end
end

class Foo
  def hello_person
    'Hello person!'
  end
end

origin = Boo.new
target = Foo.new
AeEasy::Core.mock_instance_methods origin target

puts target.hello_world
# => 'Hello world!'
puts target.hello_person
# => 'Hello person!'

origin.message = 'Hello world again!'
puts target.hello_world
# => 'Hello world again!'

Parameters:

  • origin

    Object with instance methods to mock.

  • target

    Object instance to mock methods into.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ae_easy/core.rb', line 165

def mock_instance_methods origin, target
  # Get instance unique methods
  method_list = instance_methods_from origin
  method_list.delete :context_binding if method_list.include? :context_binding

  # Build env reflecting origin unique methods
  env = {}
  method_list.each do |method|
    env[method] = lambda{|*args|origin.send(method, *args)}
  end

  # Mock origin unique methods into target
  expose_to target, env
end

.require_all(dir, opts = {}) ⇒ Object

Require all scripts within a directory.

Parameters:

  • dir (String)

    Directory containing `.rb` scripts.

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

    ({}) Configuration options.

Options Hash (opts):

  • :except (Array) — default: nil

    Literal file collection excluded from process.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ae_easy/core.rb', line 42

def require_all dir, opts = {}
  dir_list = real_dir_list = options = nil
  real_except = (opts[:except] || []).map{|f| "#{f}.rb"}
  options = opts.merge except: real_except
  $LOAD_PATH.each do |load_path|
    dir_list = Dir.glob File.join(load_path, dir)
    dir_list.each do |real_dir|
      next unless File.directory? real_dir
      all_scripts(real_dir, options) {|file| require file}
    end
  end
end

.require_relative_all(dir, opts = {}) ⇒ Object

Require all relative scripts paths within a directory.

Parameters:

  • dir (String)

    Directory containing `.rb` scripts.

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

    ({}) Configuration options.

Options Hash (opts):

  • :except (Array) — default: nil

    Literal file collection excluded from process.



60
61
62
63
64
65
66
67
68
# File 'lib/ae_easy/core.rb', line 60

def require_relative_all dir, opts = {}
  real_except = (opts[:except] || []).map{|f| "#{f}.rb"}
  options = opts.merge except: real_except
  dir_list = Dir.glob dir
  dir_list.each do |relative_dir|
    real_dir = File.expand_path relative_dir
    all_scripts(real_dir, options) {|file| require file}
  end
end