Class: LicenseFinder::YmlToSql

Inherits:
Object
  • Object
show all
Defined in:
lib/license_finder/yml_to_sql.rb

Defined Under Namespace

Modules: Sql

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ YmlToSql

Returns a new instance of YmlToSql.



34
35
36
# File 'lib/license_finder/yml_to_sql.rb', line 34

def initialize(attrs)
  @legacy_attrs = attrs
end

Instance Attribute Details

#legacy_attrsObject (readonly)

Returns the value of attribute legacy_attrs.



38
39
40
# File 'lib/license_finder/yml_to_sql.rb', line 38

def legacy_attrs
  @legacy_attrs
end

Class Method Details

.convert_all(all_legacy_attrs) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/license_finder/yml_to_sql.rb', line 14

def self.convert_all(all_legacy_attrs)
  converters = all_legacy_attrs.map do |attrs|
    new(attrs)
  end
  converters.each(&:convert)
  converters.each(&:associate_children)
end

.convert_if_requiredObject



3
4
5
6
7
8
# File 'lib/license_finder/yml_to_sql.rb', line 3

def self.convert_if_required
  if needs_conversion?
    convert_all(load_yml)
    remove_yml
  end
end

.load_ymlObject



10
11
12
# File 'lib/license_finder/yml_to_sql.rb', line 10

def self.load_yml
  YAML.load File.read(yml_path)
end

.needs_conversion?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/license_finder/yml_to_sql.rb', line 22

def self.needs_conversion?
  File.exists?(yml_path)
end

.remove_ymlObject



26
27
28
# File 'lib/license_finder/yml_to_sql.rb', line 26

def self.remove_yml
  File.delete(yml_path)
end

.yml_pathObject



30
31
32
# File 'lib/license_finder/yml_to_sql.rb', line 30

def self.yml_path
  LicenseFinder.config.dependencies_yaml
end

Instance Method Details

#associate_bundler_groupsObject



55
56
57
58
59
# File 'lib/license_finder/yml_to_sql.rb', line 55

def associate_bundler_groups
  find_bundler_groups.each do |group|
    @dep.add_bundler_group(group)
  end
end

#associate_childrenObject



49
50
51
52
53
# File 'lib/license_finder/yml_to_sql.rb', line 49

def associate_children
  find_children.each do |child|
    @dep.add_child(child)
  end
end

#convertObject



40
41
42
43
44
45
46
47
# File 'lib/license_finder/yml_to_sql.rb', line 40

def convert
  @dep = create_dependency
  @dep.license = create_license
  @dep.approval = create_approval
  @dep.manual = non_bundler_source?
  associate_bundler_groups
  @dep.save
end

#create_approvalObject



73
74
75
# File 'lib/license_finder/yml_to_sql.rb', line 73

def create_approval
  Sql::Approval.convert(legacy_attrs)
end

#create_dependencyObject



65
66
67
# File 'lib/license_finder/yml_to_sql.rb', line 65

def create_dependency
  Sql::Dependency.convert(legacy_attrs)
end

#create_licenseObject



69
70
71
# File 'lib/license_finder/yml_to_sql.rb', line 69

def create_license
  LicenseAlias.find_or_create(name: legacy_attrs['license'])
end

#find_bundler_groupsObject



81
82
83
84
85
# File 'lib/license_finder/yml_to_sql.rb', line 81

def find_bundler_groups
  (legacy_attrs['bundler_groups'] || []).map do |name|
    Sql::BundlerGroup.find_or_create(name: name.to_s)
  end
end

#find_childrenObject



77
78
79
# File 'lib/license_finder/yml_to_sql.rb', line 77

def find_children
  Sql::Dependency.where(name: legacy_attrs['children'])
end

#non_bundler_source?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/license_finder/yml_to_sql.rb', line 61

def non_bundler_source?
  @legacy_attrs['source'] == "bundle" ? false : true
end