Class: Scaffoldhub::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffoldhub/specification.rb

Constant Summary collapse

@@files =
[]
@@errors =
[]
@@tags =
[]
@@gems =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Specification

Returns a new instance of Specification.



126
127
128
129
130
# File 'lib/scaffoldhub/specification.rb', line 126

def initialize(&block)
  @context_stack = []
  @context_options = {}
  instance_eval(&block) if block_given?
end

Class Method Details

.add_file(src, dest, rename, type) ⇒ Object



55
56
57
# File 'lib/scaffoldhub/specification.rb', line 55

def add_file(src, dest, rename, type)
  @@files << { :type => type, :src => src, :rename => rename, :dest => dest }
end

.add_tag(keyword) ⇒ Object



59
60
61
# File 'lib/scaffoldhub/specification.rb', line 59

def add_tag(keyword)
  @@tags << keyword
end

.adjusted_base_urlObject



78
79
80
81
82
83
84
85
86
# File 'lib/scaffoldhub/specification.rb', line 78

def adjusted_base_url
  if base_url =~ /github.com\/([^\s]+\/[^\s]+)\/(tree|blob)\/(.*)$/
    "https://github.com/#{$1}/raw/#{$3}"
  elsif base_url =~ /github.com\/([^\s^\/]+\/[^\s^\/]+)\/?$/
    "https://github.com/#{$1}/raw/master"
  else
    base_url
  end
end

.all_template_files_exist?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/scaffoldhub/specification.rb', line 115

def all_template_files_exist?
  files.all? { |file| remote_file_exists?(File.join(adjusted_base_url, file[:src])) }
end

.has_base_url?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/scaffoldhub/specification.rb', line 100

def has_base_url?
  has_string_value?(:base_url)
end

.has_description?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/scaffoldhub/specification.rb', line 96

def has_description?
  has_string_value?(:description)
end

.has_name?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/scaffoldhub/specification.rb', line 92

def has_name?
  has_string_value?(:name)
end

.has_screenshot?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/scaffoldhub/specification.rb', line 111

def has_screenshot?
  has_string_value?(:screenshot) && remote_file_exists?(File.join(adjusted_base_url, screenshot))
end

.has_string_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
# File 'lib/scaffoldhub/specification.rb', line 104

def has_string_value?(value)
  val = send(value)
  valid = (val && val != '')
  errors.push("Error: missing scaffold #{value}.") unless valid
  valid
end

.remote_file_exists?(url) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
# File 'lib/scaffoldhub/specification.rb', line 119

def remote_file_exists?(url)
  valid = RemoteFile.new(url).exists?
  errors.push("Error: unable to access remote URL #{url}") unless valid
  valid
end

.to_yamlObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/scaffoldhub/specification.rb', line 63

def to_yaml
  {
    :name        => name,
    :description => description,
    :base_url    => adjusted_base_url,
    :blog_post   => blog_post,
    :gems        => gems,
    :files       => files,
    :screenshot  => screenshot,
    :tags        => tags,
    :parameter_example => parameter_example,
    :post_install_message => post_install_message
  }.to_yaml
end

.valid?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/scaffoldhub/specification.rb', line 88

def valid?
  has_name? && has_description? && has_base_url? && has_screenshot? && all_template_files_exist?
end

Instance Method Details

#file(src, options = {}, type = :file) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/scaffoldhub/specification.rb', line 143

def file(src, options = {}, type = :file)
  self.class.add_file(
    join_with_parent(@context_options[:src], src),
    join_with_parent(@context_options[:dest], options[:dest]),
    options[:rename],
    type
  )
end

#gem(*args) ⇒ Object



161
162
163
# File 'lib/scaffoldhub/specification.rb', line 161

def gem(*args)
  @@gems << args
end

#metadataObject



139
140
141
# File 'lib/scaffoldhub/specification.rb', line 139

def 
  yield if block_given?
end

#tag(keyword) ⇒ Object



157
158
159
# File 'lib/scaffoldhub/specification.rb', line 157

def tag(keyword)
  self.class.add_tag(keyword)
end

#template(src, options = {}) ⇒ Object



152
153
154
155
# File 'lib/scaffoldhub/specification.rb', line 152

def template(src, options = {})
  raise ':dest option is required for templates' unless options[:dest]
  file(src, options, :template)
end

#with_options(options, &block) ⇒ Object



132
133
134
135
136
137
# File 'lib/scaffoldhub/specification.rb', line 132

def with_options(options, &block)
  @context_stack.push(@context_options)
  @context_options = options_relative_to_parent(@context_options, options)
  yield if block_given?
  @context_options = @context_stack.pop
end