Module: ActiveFacts::Generators::ScalaTraits::Role
- Defined in:
- lib/activefacts/generators/traits/scala.rb
Instance Method Summary collapse
Instance Method Details
#scala_preferred_role_name(is_for = nil, &name_builder) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/activefacts/generators/traits/scala.rb', line 64 def scala_preferred_role_name(is_for = nil, &name_builder) # REVISIT: Modify this to suit Scala if fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance) # Subtype and Supertype roles default to TitleCase names, and have no role_name to worry about: return (name_builder || proc {|names| names.titlecase}).call(object_type.name.words) end name_builder ||= proc {|names| names.map(&:downcase)*'_' } # Make snake_case by default # Handle an objectified unary role: if is_for && fact_type.entity_type == is_for && fact_type.all_role.size == 1 return name_builder.call(object_type.name.words) end # trace "Looking for preferred_role_name of #{describe_fact_type(fact_type, self)}" reading = fact_type.preferred_reading preferred_role_ref = reading.role_sequence.all_role_ref.detect{|reading_rr| reading_rr.role == self } if fact_type.all_role.size == 1 return name_builder.call( role_name ? role_name.snakewords : reading.text.gsub(/ *\{0\} */,' ').gsub(/[- ]+/,'_').words ) end if role_name && role_name != "" role_words = [role_name] else role_words = [] la = preferred_role_ref.leading_adjective role_words += la.words.snakewords if la && la != "" role_words += object_type.name.words.snakewords ta = preferred_role_ref.trailing_adjective role_words += ta.words.snakewords if ta && ta != "" end # n = role_words.map{|w| w.gsub(/([a-z])([A-Z]+)/,'\1_\2').downcase}*"_" n = role_words*'_' # trace "\tresult=#{n}" return name_builder.call(n.gsub(' ','_').split(/_/)) end |
#scala_role_definition ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/activefacts/generators/traits/scala.rb', line 113 def scala_role_definition return if fact_type.entity_type if fact_type.all_role.size == 1 scala_role_name = scala_preferred_role_name.words.camelcase return " val #{scala_role_name}: Boolean" elsif fact_type.all_role.size != 2 # Shouldn't come here, except perhaps for an invalid model return # ternaries and higher are always objectified end return if fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance) other_role = fact_type.all_role.select{|r| r != self}[0] other_role_name = other_role.scala_preferred_role_name scala_role_name = other_role_name.words.camelcase other_type_name = other_role.object_type.name.words.titlecase if is_functional if is_mandatory # Define a getter for a mandatory value: " val #{scala_role_name}: #{other_type_name}" if !fact_type.is_existential " def #{scala_role_name}_=(_value: #{other_type_name}) = { #{scala_role_name} = _value }" end else # Define a getter for an optional value: # REVISIT: The role number here depends on the metamodel ordering of the fact type roles. # This likely should follow the role order of the preferred reading, from which the fact name is derived. # The code here collows the order of definition of the roles in the fact type, # which might not be the same as the order of the preferred reading: fact_name = fact_type.scala_name.titlecase role_number = fact_type.all_role_in_order.index(other_role)+1 " def #{scala_role_name}: Option[#{other_type_name}] = {\n" + " constellation.getBinaryFact(metaModel.#{fact_name.words.camelcase}, this).map(x => {\n" + " x.head.asInstanceOf[FBMModel.BinaryFact].rolePlayers._#{role_number}.asInstanceOf[#{other_type_name}]\n" + " })\n" + " }\n" + if !fact_type.is_existential # Define a setter for an optional value: " def #{scala_role_name}_=(value: Option[#{other_type_name}]) = {\n" + " value match {\n" + " case None =>\n" + " case Some(m) => constellation.assertBinaryFact(#{fact_name.words.titlecase}(this, m))\n" + " }\n" + " }" else '' end end elsif other_role.object_type.fact_type # An objectified fact type <<"END" def all#{scala_role_name.words.titlecase}(implicit constellation: Constellation): Seq[#{other_type_name}] = { constellation.getObjectifiedFact(metaModel.#{scala_role_name.words.camelcase}, this).getOrElse(Nil).flatMap(x => x match { case o: #{other_type_name} => Some(o) case _ => None }) } END else <<"END" /* def all#{scala_role_name.words.titlecase}(implicit constellation: Constellation): Seq[#{other_type_name}] = { constellation.getFact(metaModel.#{scala_role_name.words.camelcase}, this).getOrElse(Nil).flatMap(x => x match { # REVISIT: This is incorrect; we want to return the other role player in the fact case o: #{other_type_name} => Some(o) case _ => None }) } */ END end end |