Class: Spider::App::AppSpec

Inherits:
Object show all
Defined in:
lib/spiderfw/app.rb

Overview

The AppSpec class represents an app’s .spec file The AppSpec attributes are:

  • :app_id [String] sunique identifier for the app

  • :name [String] descriptive name

  • :description [String]

  • :git_repo [String] URL of git repository for the app

  • :git_repo_rw [String] URL of read/write git repository for the app

  • :authors [Array]

  • :depends [Array] Apps this app depends on

  • :depends_optional [Array] Optional dependencies

  • :load_after [Array] Apps that must be loaded before this one (if present)

  • :gems [Array] Gems this app depends on

  • :gems_optional [Array] Optional gem dependencies

  • :version [Gem::Version] Current app version

  • :app_server [String] URL for the app server of this app

  • :auto_update [TrueClass|FalseClass] true by default; set to false if this version can’t be auto-updated

Constant Summary collapse

@@attributes =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#branch(val = nil) ⇒ Object

The git branch used for the app



495
496
497
# File 'lib/spiderfw/app.rb', line 495

def branch
  @branch
end

Class Method Details

.array_attribute(name, options = {}) ⇒ nil

Helper method to define an Array attribute on the AppSpec class

Returns:

  • (nil)


466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/spiderfw/app.rb', line 466

def self.array_attribute(name, options={})
    @@attributes << name
    str = <<END_OF_EVAL
    def #{name}(*vals)
        @#{name} = vals unless vals.empty?
        @#{name} ||= []
        @#{name}
    end
END_OF_EVAL
    class_eval(str)
    nil
end

.attribute(name, options = {}) ⇒ nil

Helper method to define an attribute on the AppSpec class

Returns:

  • (nil)


446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/spiderfw/app.rb', line 446

def self.attribute(name, options={})
    @@attributes << name
    str = <<END_OF_EVAL
    def #{name}(val=nil)
        @#{name} = val if val
        @#{name} ||= #{options[:default].inspect}
        @#{name}
    end
    alias :#{name}= :#{name}
END_OF_EVAL
    if options[:default].is_a?(TrueClass) || options[:default].is_a?(FalseClass)
        str += "\nalias :#{name}? :#{name}\n"
    end
    class_eval(str)
    nil
end

.eval(text, path = nil) ⇒ AppSpec

Returns a new AppSpec instance, evaluating the given code

Parameters:

  • text (String)

    code to evaluate

  • path (String) (defaults to: nil)

    path to the code

Returns:



546
547
548
# File 'lib/spiderfw/app.rb', line 546

def self.eval(text, path=nil)
    self.new.eval(text, path)
end

.load(spec_path) ⇒ AppSpec

Returns a new AppSpec instance, loading from a .spec file

Parameters:

Returns:



530
531
532
# File 'lib/spiderfw/app.rb', line 530

def self.load(spec_path)
    self.new.load(spec_path)
end

.parse_hash(h) ⇒ AppSpec

Constructs a new AppSpec instance from an Hash of attributes

Parameters:

  • h (Hash)

Returns:



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/spiderfw/app.rb', line 571

def self.parse_hash(h)
    spec = self.new
    h.each do |key, value|
        unless spec.respond_to?(:"#{key}")
            Spider.output("Bad spec key #{key} in:", :ERROR)
            Spider.output(h.inspect, :ERROR)
            next
        end
        if value.is_a?(Array)
            spec.send(:"#{key}", *value)
        else
            spec.send(:"#{key}", value)
        end
    end
    spec
end

Instance Method Details

#author(val = nil) ⇒ String

Sets or retrieves the first AppSpec author

Returns:



513
514
515
516
517
# File 'lib/spiderfw/app.rb', line 513

def author(val = nil)
    @authors = [val] if val
    @authors ||= []
    @authors[0]
end

#eval(text, path = nil) ⇒ AppSpec

Evals the given code in the AppSpec’s context

Returns:



537
538
539
540
# File 'lib/spiderfw/app.rb', line 537

def eval(text, path=nil)
    self.instance_eval(text)
    self
end

#gems_listArray

Returns an Array of gem names for gems this AppSpec depends on

Returns:

  • (Array)


597
598
599
# File 'lib/spiderfw/app.rb', line 597

def gems_list
    self.gems.map{ |g| g.is_a?(Array) ? g.first : g }
end

#gems_optional_listArray

Returns an Array of optional gem names

Returns:

  • (Array)


603
604
605
# File 'lib/spiderfw/app.rb', line 603

def gems_optional_list
    self.gems_optional.map{ |g| g.is_a?(Array) ? g.first : g }
end

#get_runtime_dependenciesArray

Returns an array of apps needed at runtime

Returns:

  • (Array)


590
591
592
593
# File 'lib/spiderfw/app.rb', line 590

def get_runtime_dependencies
    return self.load_after unless @load_after.blank?
    return self.depends + self.depends_optional
end

#id(val = nil) ⇒ String

Sets or retrieves the AppSpec id

Returns:



499
500
501
# File 'lib/spiderfw/app.rb', line 499

def id(val=nil)
    self.app_id(val)
end

#load(spec_path) ⇒ self

Loads attributes from a .spec file

Parameters:

Returns:

  • (self)


522
523
524
525
# File 'lib/spiderfw/app.rb', line 522

def load(spec_path)
    self.eval(File.read(spec_path), spec_path)
    self
end

#to_hHash

Returns all attributes as an Hash

Returns:

  • (Hash)


552
553
554
555
556
557
558
559
# File 'lib/spiderfw/app.rb', line 552

def to_h
    h = {}
    @@attributes.each do |a|
        h[a] = send(a)
    end
    h[:branch] = @branch unless @branch.blank?
    h
end

#to_json(opts = nil) ⇒ String

Returns the Hash (as in #to_hash) as JSON

Parameters:

  • opts (Hash) (defaults to: nil)

    JSON options

Returns:



564
565
566
# File 'lib/spiderfw/app.rb', line 564

def to_json(opts=nil)
    to_h.to_json
end

#version(val = nil) ⇒ Gem::Version

Sets or retrieves the AppSpec version

Parameters:

  • val (String) (defaults to: nil)

Returns:

  • (Gem::Version)


506
507
508
509
# File 'lib/spiderfw/app.rb', line 506

def version(val=nil)
    @version = Gem::Version.new(val) if val
    @version
end