module ActiveFacts::Generators::RubyTraits::Role

Public Instance Methods

as_binary(role_name, role_player, mandatory = nil, one_to_one = nil, readings = nil, other_role_name = nil, other_method_name = nil) click to toggle source
# File lib/activefacts/generators/traits/ruby.rb, line 109
def as_binary(role_name, role_player, mandatory = nil, one_to_one = nil, readings = nil, other_role_name = nil, other_method_name = nil)
  ruby_role_name = ":"+role_name.words.snakecase

  # Find whether we need the name of the other role player, and whether it's defined yet:
  implied_role_name = role_player.name.gsub(/ /,'').sub(/^[a-z]/) {|i| i.upcase}
  if role_name.camelcase != implied_role_name
    # Only use Class name if it's not implied by the rolename
    role_reference = ":class => "+role_player.ruby_type_reference
  end

  other_role_name = ":counterpart => :"+other_role_name.gsub(/ /,'_') if other_role_name

  if vr = role_value_constraint
    value_restriction = ":restrict => #{vr}"
  end

  options = [
      ruby_role_name,
      role_reference,
      mandatory ? ":mandatory => true" : nil,
      readings,
      other_role_name,
      value_restriction
    ].compact

  debugger if ruby_role_name == 'astronomicalobject'

  line = "    #{one_to_one ? "one_to_one" : "has_one" } #{options*', '}  "
  if other_method_name
    line += " "*(48-line.length) if line.length < 48
    line += "\# See #{role_player.name.gsub(/ /,'')}.#{other_method_name}"
  end
  line+"\n"
end
preferred_role_name(is_for = nil, &name_builder) click to toggle source
# File lib/activefacts/generators/traits/ruby.rb, line 61
def preferred_role_name(is_for = nil, &name_builder)

  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.words.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
ruby_role_definition() click to toggle source
# File lib/activefacts/generators/traits/ruby.rb, line 144
def ruby_role_definition
  oo_role_definition
end