Class: Bluff::Builder::Definition
- Inherits:
-
Object
- Object
- Bluff::Builder::Definition
- Defined in:
- lib/bluff/builder.rb
Instance Method Summary collapse
- #attributes ⇒ Object (also: #params)
- #attributes=(hash) ⇒ Object (also: #params=)
- #default(attribute, value) ⇒ Object
- #defaults(hash) ⇒ Object
- #execute(&block) ⇒ Object
-
#initialize(target, attributes = {}) ⇒ Definition
constructor
A new instance of Definition.
- #requires(attributes) ⇒ Object
Constructor Details
#initialize(target, attributes = {}) ⇒ Definition
Returns a new instance of Definition.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/bluff/builder.rb', line 12 def initialize(target, attributes = {}) @target = target @attributes = attributes if @attributes.is_a?(Hash) @attributes = @attributes.with_indifferent_access else raise ArgumentError, 'Bluff argument must be a single hash' end end |
Instance Method Details
#attributes ⇒ Object Also known as: params
23 24 25 |
# File 'lib/bluff/builder.rb', line 23 def attributes @attributes end |
#attributes=(hash) ⇒ Object Also known as: params=
27 28 29 |
# File 'lib/bluff/builder.rb', line 27 def attributes=(hash) @attributes = hash end |
#default(attribute, value) ⇒ Object
49 50 51 |
# File 'lib/bluff/builder.rb', line 49 def default(attribute, value) @attributes[attribute] = value if @attributes[attribute].blank? end |
#defaults(hash) ⇒ Object
53 54 55 |
# File 'lib/bluff/builder.rb', line 53 def defaults(hash) hash.each {|key, value| default(key, value)} end |
#execute(&block) ⇒ Object
35 36 37 |
# File 'lib/bluff/builder.rb', line 35 def execute(&block) self.instance_exec(@attributes, &block) end |
#requires(attributes) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/bluff/builder.rb', line 39 def requires(attributes) missing = [] Array.wrap(attributes).each do |attribute| missing << attribute unless provided?(attribute) end raise ArgumentError, "#{missing.to_sentence} cannot be bluffed for #{@target}" unless missing.empty? end |