class FixtureOverlord::Mock

Public Class Methods

new(hash) click to toggle source
Calls superclass method
# File lib/fixture_overlord/mock.rb, line 13
def initialize(hash)
  super(hash.merge(generate_id))
end
setup(hash) click to toggle source
# File lib/fixture_overlord/mock.rb, line 9
def self.setup(hash)
  new(hash)
end

Public Instance Methods

add(options = {})
Alias for: change
belongs_to(options)
Alias for: parent
change(options = {}) click to toggle source

change a key/value or add one

Examples

e.g.

games(:donkey_kong).change(name: 'Jumpman Jr.').mock
games(:donkey_kong).add(leader: 'Jacob').mock
# File lib/fixture_overlord/mock.rb, line 85
def change(options = {})
  options.each { |k,v| writer(k,v) }
  self
end
Also aliased as: add, merge
child(options) click to toggle source

add a child association

Examples

e.g. (has_many)

blog.child(posts: post)
blog.posts.first.title

There are 4 methods aliased to this one to provide the developer ActiveRecord & a Sequel (ORM) like interface.

# File lib/fixture_overlord/mock.rb, line 44
def child(options)
  associations(options) do |k,v|
    writer(k,[v])
  end
end
Also aliased as: has_many, one_to_many
delete(key)
Alias for: remove
has_many(options)
Alias for: child
has_one(options)
Alias for: parent
many_to_one(options)
Alias for: parent
merge(options = {})
Alias for: change
one_to_many(options)
Alias for: child
one_to_one(options)
Alias for: parent
parent(options) click to toggle source

add a parent association

Examples

e.g. (belongs_to)

post.parent(blog: blog)
post.blog.name

There are 4 methods aliased to this one to provide the developer ActiveRecord & a Sequel (ORM) like interface.

# File lib/fixture_overlord/mock.rb, line 66
def parent(options)
  associations(options) do |k,v|
    writer(k,v)
  end
end
remove(key) click to toggle source

remove an attribute from the class

Examples

e.g.

blog.remove(:name)
blog.name == nil
# File lib/fixture_overlord/mock.rb, line 101
def remove(key)
  writer(key)
end
Also aliased as: delete
to_attributes() click to toggle source

Converts attributes back to a hash (Hashish). Beacuse this is still a Hashish Hash, we can covert it back to a mock.

Examples

e.g.

blog.to_attributes => { title: 'Blog' }
# File lib/fixture_overlord/mock.rb, line 26
def to_attributes
  Hashish[self.to_h].symbolize_keys
end

Private Instance Methods

associations(options) { |k, v| ... } click to toggle source
# File lib/fixture_overlord/mock.rb, line 125
def associations(options, &block)
  options.each do |k,v|
    v = Mock.setup(v) if v.is_a?(Hash) || v.is_a?(Hashish)
    yield k, v
  end
end
build_model_base() click to toggle source
# File lib/fixture_overlord/mock.rb, line 121
def build_model_base
  @build_model_base ||= Helpers.to_model(yaml_filename)
end
generate_id() click to toggle source

generate a unique id and return the hash

# File lib/fixture_overlord/mock.rb, line 111
def generate_id
  { id: ::SecureRandom.random_number(99999) }
end
writer(key, value = nil) click to toggle source

assign a value to a hash key

# File lib/fixture_overlord/mock.rb, line 117
def writer(key, value = nil)
  self.send("#{key}=", value)
end