Class: FixtureOverlord::Mock

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/fixture_overlord/mock.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Mock

Returns a new instance of Mock.



13
14
15
# File 'lib/fixture_overlord/mock.rb', line 13

def initialize(hash)
  super(hash.merge(generate_id))
end

Class Method Details

.setup(hash) ⇒ Object



9
10
11
# File 'lib/fixture_overlord/mock.rb', line 9

def self.setup(hash)
  new(hash)
end

Instance Method Details

#change(options = {}) ⇒ Object Also known as: add

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


85
86
87
88
# File 'lib/fixture_overlord/mock.rb', line 85

def change(options = {})
  options.each { |k,v| writer(k,v) }
  self
end

#child(options) ⇒ Object Also known as: has_many, one_to_many

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.



44
45
46
47
48
# File 'lib/fixture_overlord/mock.rb', line 44

def child(options)
  associations(options) do |k,v|
    writer(k,[v])
  end
end

#parent(options) ⇒ Object Also known as: belongs_to, many_to_one, has_one, one_to_one

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.



66
67
68
69
70
# File 'lib/fixture_overlord/mock.rb', line 66

def parent(options)
  associations(options) do |k,v|
    writer(k,v)
  end
end

#remove(key) ⇒ Object Also known as: delete

remove an attribute from the class

Examples

e.g.

blog.remove(:name)
blog.name == nil


100
101
102
# File 'lib/fixture_overlord/mock.rb', line 100

def remove(key)
  writer(key)
end

#to_attributesObject

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' }


26
27
28
# File 'lib/fixture_overlord/mock.rb', line 26

def to_attributes
  Hashish[self.to_h].symbolize_keys
end