Class: SetBuilder::Constraint
- Inherits:
-
Object
- Object
- SetBuilder::Constraint
- Defined in:
- lib/set_builder/constraint.rb
Instance Attribute Summary collapse
-
#direct_object ⇒ Object
readonly
Returns the value of attribute direct_object.
-
#modifiers ⇒ Object
readonly
Returns the value of attribute modifiers.
-
#negative ⇒ Object
readonly
Returns the value of attribute negative.
-
#trait ⇒ Object
readonly
Returns the value of attribute trait.
Instance Method Summary collapse
-
#initialize(trait, *args, &block) ⇒ Constraint
constructor
Sample constraints:.
- #negate(value) ⇒ Object
- #perform(scope) ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(trait, *args, &block) ⇒ Constraint
Sample constraints:
[:awesome],
[:attended, "school"],
[:died],
[:name, {:is => "Jerome"}]]
16 17 18 19 20 |
# File 'lib/set_builder/constraint.rb', line 16 def initialize(trait, *args, &block) @trait, @block = trait, block @direct_object = args.shift if trait.requires_direct_object? @modifiers = trait.modifiers.collect {|modifier_type| modifier_type.new(args.shift)} end |
Instance Attribute Details
#direct_object ⇒ Object (readonly)
Returns the value of attribute direct_object.
24 25 26 |
# File 'lib/set_builder/constraint.rb', line 24 def direct_object @direct_object end |
#modifiers ⇒ Object (readonly)
Returns the value of attribute modifiers.
24 25 26 |
# File 'lib/set_builder/constraint.rb', line 24 def modifiers @modifiers end |
#negative ⇒ Object (readonly)
Returns the value of attribute negative.
24 25 26 |
# File 'lib/set_builder/constraint.rb', line 24 def negative @negative end |
#trait ⇒ Object (readonly)
Returns the value of attribute trait.
24 25 26 |
# File 'lib/set_builder/constraint.rb', line 24 def trait @trait end |
Instance Method Details
#negate(value) ⇒ Object
52 53 54 55 56 |
# File 'lib/set_builder/constraint.rb', line 52 def negate(value) @negative = value @negative = false if trait.noun? self end |
#perform(scope) ⇒ Object
60 61 62 |
# File 'lib/set_builder/constraint.rb', line 60 def perform(scope) scope.scoped(@block.call(self, scope)) end |
#to_s ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/set_builder/constraint.rb', line 40 def to_s # p "ValueMap.to_s(#{direct_object_type} (#{direct_object_type.class}), #{direct_object} (#{direct_object.class}))" @description ||= begin description = trait.to_s(@negative) description << " #{ValueMap.to_s(direct_object_type, direct_object)}" if direct_object_required? description << " #{modifiers.collect{|m| m.to_s(@negative)}.join(" ")}" unless modifiers.empty? description end end |
#valid? ⇒ Boolean
34 35 36 |
# File 'lib/set_builder/constraint.rb', line 34 def valid? (!direct_object_required? or !direct_object.nil?) and modifiers.all?(&:valid?) end |