Class: Famili::Father
- Inherits:
-
Object
- Object
- Famili::Father
- Defined in:
- lib/famili/father.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#mother ⇒ Object
readonly
Returns the value of attribute mother.
Instance Method Summary collapse
- #build(opts = {}) {|instance| ... } ⇒ Object
- #build_brothers(num, opts = {}, &block) ⇒ Object
- #build_hash(opts = {}) ⇒ Object
- #create(opts = {}, &block) ⇒ Object
- #create_brothers(num, opts = {}, &block) ⇒ Object
-
#initialize(mother, attributes) ⇒ Father
constructor
A new instance of Father.
- #produce_brothers(num, opts = {}, init_block, &block) ⇒ Object
- #scoped(attributes = {}) ⇒ Object
Constructor Details
#initialize(mother, attributes) ⇒ Father
Returns a new instance of Father.
8 9 10 11 |
# File 'lib/famili/father.rb', line 8 def initialize(mother, attributes) @mother = mother @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/famili/father.rb', line 5 def attributes @attributes end |
#mother ⇒ Object (readonly)
Returns the value of attribute mother.
6 7 8 |
# File 'lib/famili/father.rb', line 6 def mother @mother end |
Instance Method Details
#build(opts = {}) {|instance| ... } ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/famili/father.rb', line 20 def build(opts = {}) attributes = merge(opts) instance = @mother.born(Famili::Child.new(@mother, attributes)) yield instance if block_given? @mother.before_save(instance) instance end |
#build_brothers(num, opts = {}, &block) ⇒ Object
47 48 49 |
# File 'lib/famili/father.rb', line 47 def build_brothers(num, opts = {}, &block) produce_brothers(num, opts, block) { |brother_opts, &init_block| build(brother_opts, &init_block) } end |
#build_hash(opts = {}) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/famili/father.rb', line 13 def build_hash(opts = {}) attributes = build(opts).attributes.symbolize_keys attributes.delete(:updated_at) attributes.delete(:created_at) attributes end |
#create(opts = {}, &block) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/famili/father.rb', line 28 def create(opts = {}, &block) instance = build(opts, &block) @mother.save(instance) @mother.after_create(instance) instance end |
#create_brothers(num, opts = {}, &block) ⇒ Object
51 52 53 |
# File 'lib/famili/father.rb', line 51 def create_brothers(num, opts = {}, &block) produce_brothers(num, opts, block) { |brother_opts, &init_block| create(brother_opts, &init_block) } end |
#produce_brothers(num, opts = {}, init_block, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/famili/father.rb', line 35 def produce_brothers(num, opts={}, init_block, &block) brothers = [] if init_block && init_block.arity == 2 num.times { |i| brothers << block.call(opts) { |o| init_block.call(o, i) } } else num.times { brothers << block.call(opts, &init_block) } end brothers end |
#scoped(attributes = {}) ⇒ Object
55 56 57 |
# File 'lib/famili/father.rb', line 55 def scoped(attributes = {}) self.class.new(@mother, merge(attributes)) end |