Class: SetBuilder::Trait

Inherits:
Object
  • Object
show all
Defined in:
lib/set_builder/trait.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, part_of_speech, *args, &block) ⇒ Trait

Returns a new instance of Trait.



10
11
12
13
14
15
16
17
18
19
# File 'lib/set_builder/trait.rb', line 10

def initialize(name, part_of_speech, *args, &block)
  case name
  when Hash
    @name, @direct_object_type = name.first[0].to_s, name.first[1]
  else
    @name = name.to_s
  end
  @part_of_speech, @block = part_of_speech, block
  @modifiers = (args||[]).collect {|modifier| Modifier[modifier]}
end

Instance Attribute Details

#direct_object_typeObject (readonly)

Returns the value of attribute direct_object_type.



23
24
25
# File 'lib/set_builder/trait.rb', line 23

def direct_object_type
  @direct_object_type
end

#modifiersObject (readonly)

Returns the value of attribute modifiers.



23
24
25
# File 'lib/set_builder/trait.rb', line 23

def modifiers
  @modifiers
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/set_builder/trait.rb', line 23

def name
  @name
end

#part_of_speechObject (readonly)

Returns the value of attribute part_of_speech.



23
24
25
# File 'lib/set_builder/trait.rb', line 23

def part_of_speech
  @part_of_speech
end

Instance Method Details

#apply(*args) ⇒ Object



67
68
69
# File 'lib/set_builder/trait.rb', line 67

def apply(*args)
  SetBuilder::Constraint.new(self, *args, &@block)
end

#noun?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/set_builder/trait.rb', line 34

def noun?
  (self.part_of_speech == :noun)
end

#requires_direct_object?Boolean Also known as: direct_object_required?

Returns:

  • (Boolean)


27
28
29
# File 'lib/set_builder/trait.rb', line 27

def requires_direct_object?
  !@direct_object_type.nil?
end

#to_jsonObject



57
58
59
60
61
62
63
# File 'lib/set_builder/trait.rb', line 57

def to_json
  array = []
  array << (requires_direct_object? ? [name, @direct_object_type] : name)
  array << part_of_speech
  array << modifiers.collect{|klass| Modifier.name(klass)} unless modifiers.empty?
  array.to_json
end

#to_s(negative = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/set_builder/trait.rb', line 40

def to_s(negative=false)
  case part_of_speech
  when :active
    negative ? "who have not #{name}" : "who #{name}"
  when :perfect
    negative ? "who have not #{name}" : "who have #{name}"
  when :passive
    negative ? "who were not #{name}" : "who were #{name}"
  when :reflexive
    negative ? "who are not #{name}" : "who are #{name}"
  when :noun
    "whose #{name}"
  end
end