Class: Hyraft::Rule::Connectors::CableCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/hyraft/rule/connectors/cable_command.rb

Class Method Summary collapse

Class Method Details

.generate_content(plural_name, singular_name, class_name, concrete_class_name, storage_class_name, storage_file, attributes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hyraft/rule/connectors/cable_command.rb', line 52

def self.generate_content(plural_name, singular_name, class_name, concrete_class_name, storage_class_name, storage_file, attributes)
  create_params = attributes.map { |a| a }.join(", ")
  create_args = attributes.map { |a| a }.join(", ")
  update_params = attributes.map { |a| a }.join(", ")
  update_args = attributes.map { |a| a }.join(", ")
  
  template = <<~RUBY
    pathway 'base-adapters/out-data/#{storage_file}'
    pathway 'circuit/engine/#{singular_name}'
    pathway 'setup/database/sequel_connection'

    class #{class_name}Cable
      private_class_method :new
      
      GATEWAY = #{storage_class_name}Storage.new(SequelConnection.db)
      ARTICLES = #{concrete_class_name}.new(GATEWAY)
      
      def self.create_#{singular_name}(#{create_params})
        ARTICLES.create(#{create_args})
      end
      
      def self.get_#{singular_name}(id)
        ARTICLES.get(id)
      end
      
      def self.list_#{plural_name}
        ARTICLES.list
      end
      
      def self.update_#{singular_name}(id, #{update_params})
        ARTICLES.update(id, #{update_args})
      end
      
      def self.delete_#{singular_name}(id)
        ARTICLES.delete(id)
      end
    end

    # #{class_name}Cable.create_#{singular_name}(#{attributes.map { |a| "\"value\"" }.join(", ")})
    # #{class_name}Cable.list_#{plural_name}
    # #{class_name}Cable.get_#{singular_name}(1)
    # #{class_name}Cable.update_#{singular_name}(1, #{attributes.map { |a| "\"new value\"" }.join(", ")})
    # #{class_name}Cable.delete_#{singular_name}(1)
  RUBY
  
  template
end

.show_usageObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hyraft/rule/connectors/cable_command.rb', line 100

def self.show_usage
  puts "Usage: hyraft-rule cable <resource_name> [attributes...] [target_dir]"
  puts ""
  puts "Examples:"
  puts "  hyraft-rule cable articles title content"
  puts "  hyraft-rule cable users name email password"
  puts "  hyraft-rule cable products name price stock"
  puts "  hyraft-rule cable comments body author_id"
  puts ""
  puts "This creates: connectors/cable/<resource_name>_cable.rb"
end

.start(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hyraft/rule/connectors/cable_command.rb', line 10

def self.start(args)
  resource_name = args[0]
  return show_usage unless resource_name

  plural_name = resource_name.downcase
  attributes = []

  if args.length > 1
    last_arg = args[-1]
    if last_arg.include?('/') || last_arg == '.' || last_arg == '..' || last_arg.start_with?('./')
      target_dir = last_arg
      attributes = args[1..-2] if args.length > 2
    else
      target_dir = "."
      attributes = args[1..-1]
    end
  else
    target_dir = "."
  end

  singular_name = plural_name.chomp('s')
  class_name = plural_name.capitalize
  
  concrete_class_name = "#{class_name}Concrete"
  storage_class_name = plural_name.capitalize
  storage_file = "#{plural_name}_storage"

  filename = "#{plural_name}_cable.rb"
  cable_dir = File.join(target_dir, "connectors", "cable")
  full_path = File.join(cable_dir, filename)

  FileUtils.mkdir_p(cable_dir)
  
  content = generate_content(plural_name, singular_name, class_name, concrete_class_name, storage_class_name, storage_file, attributes)
  
  File.write(full_path, content)

  puts "\e[94m✓ Created cable: #{full_path}\e[0m"
  puts "\e[38;5;214mResource: #{plural_name}"
  puts "Attributes: #{attributes.join(', ')}\e[0m" if attributes.any?
end