Class: Cog::Seed

Inherits:
Object
  • Object
show all
Includes:
Generator
Defined in:
lib/cog/seed.rb,
lib/cog/seed/var.rb,
lib/cog/seed/feature.rb

Overview

Template for a class in a target language

Defined Under Namespace

Classes: Feature, Var

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Generator

#embed, #gcontext, #stamp

Methods included from Generator::LanguageMethods

#end_all_scopes, #include_guard_begin, #named_scope_begin, #scope_begin, #scope_end, #use_named_scope, #warning

Methods included from Generator::Filters

#call_filter, #comment

Methods included from Generator::FileMethods

#copy_file_if_missing, #files_are_same?, #get_template, #touch_directory, #touch_file

Constructor Details

#initialize(name) ⇒ Seed

Returns a new instance of Seed.

Parameters:

  • name (String)

    name of the class



22
23
24
25
# File 'lib/cog/seed.rb', line 22

def initialize(name)
  @name = name.to_s.camelize.to_ident
  @features = [] # [Feature]
end

Instance Attribute Details

#header_pathString? (readonly)

Returns path to the header file.

Returns:

  • (String, nil)

    path to the header file



15
16
17
# File 'lib/cog/seed.rb', line 15

def header_path
  @header_path
end

#in_scopeString? (readonly)

Returns name of the scope in which classes generated by this seed will be found.

Returns:

  • (String, nil)

    name of the scope in which classes generated by this seed will be found



18
19
20
# File 'lib/cog/seed.rb', line 18

def in_scope
  @in_scope
end

#nameString (readonly)

Returns name of the class.

Returns:

  • (String)

    name of the class



12
13
14
# File 'lib/cog/seed.rb', line 12

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object

Sort by name



63
64
65
# File 'lib/cog/seed.rb', line 63

def <=>(other)
  @name <=> other
end

#featuresArray<Feature>

Returns a sorted list of features.

Returns:



34
35
36
# File 'lib/cog/seed.rb', line 34

def features
  @features.sort
end

#guardString

Returns begin the include guard.

Returns:

  • (String)

    begin the include guard



28
29
30
31
# File 'lib/cog/seed.rb', line 28

def guard
  x = [@in_scope, @name].compact.collect &:upcase
  include_guard_begin "__COG_SPROUT__#{x.join '_'}_H__"
end

#in_header?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/cog/seed.rb', line 58

def in_header?
  @in_header
end

#stamp_class(path, opt = {}) ⇒ Object

Render the class in the currently active language

Parameters:

  • path (String)

    file system path without the extension, relative to the project root. The extension will be determined based on the currently active language

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :language (String)

    key for the language to use. The language must define a seed extension



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cog/seed.rb', line 41

def stamp_class(path, opt={})
  Cog.activate_language opt[:language] do
    l = Cog.active_language
    raise Errors::ActiveLanguageDoesNotSupportSeeds.new :language => l if l.nil? || l.seed_extension.nil?
    
    @in_header = false
    @header_path = if l.seed_header
      "#{path}.#{l.seed_header}"
    end
    stamp "cog/#{l.key}/seed.#{l.seed_extension}", "#{path}.#{l.seed_extension}"
    if l.seed_header
      @in_header = true
      stamp "cog/#{l.key}/seed.#{l.seed_header}", @header_path
    end
  end
end