Class: Jeny::Command::Snippets

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/jeny/command/snippets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support

#edit_changed_files, simplify_path

Constructor Details

#initialize(config, data, asset, from) ⇒ Snippets

Returns a new instance of Snippets.



6
7
8
9
10
11
# File 'lib/jeny/command/snippets.rb', line 6

def initialize(config, data, asset, from)
  @config = config
  @data = { asset => Caser.for_hash(data) }
  @asset = asset
  @from = from
end

Instance Attribute Details

#assetObject (readonly)

Returns the value of attribute asset.



12
13
14
# File 'lib/jeny/command/snippets.rb', line 12

def asset
  @asset
end

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/jeny/command/snippets.rb', line 12

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/jeny/command/snippets.rb', line 12

def data
  @data
end

#fromObject (readonly)

Returns the value of attribute from.



12
13
14
# File 'lib/jeny/command/snippets.rb', line 12

def from
  @from
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jeny/command/snippets.rb', line 14

def call
  puts
  sm, state = config.state_manager, OpenStruct.new
  sm.stash(state) if config.sm_stash?

  changed = []
  from.each do |source|
    parent = source
    parent = source.parent until parent.directory?
    _call(source, parent, changed)
  end

  sm.commit(changed.map(&:first), state) if config.sm_commit?

  edit_changed_files(changed)
rescue
  sm.reset(changed.map(&:first), state)
  raise
ensure
  sm.unstash(state) if config.sm_stash?
end

#snippet_it(source, parent) ⇒ Object



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
# File 'lib/jeny/command/snippets.rb', line 54

def snippet_it(source, parent)
  target, target_content = nil
  if source.ext =~ /\.?jeny/
    file = File::Full.new(source, config)
    if file.has_jeny_context?
      ctx = file.instantiate_context(data)
      if ctx
        target_content = file.instantiate(ctx)
        target = target_for(source, ctx)
        target.parent.mkdir_p
        target.write(target_content)
        puts "snippets #{simplify_path(target)}"
      end
    end
  else
    file = File::WithBlocks.new(source, config)
    if file.has_jeny_blocks?
      target_content = file.instantiate(data)
      target = target_for(source)
      target.write(target_content)
      puts "snippets #{simplify_path(target)}"
    end
  end
  target ? [target, target_content] : nil
rescue => ex
  msg = "Error in `#{simplify_path(source)}`: #{ex.message}"
  raise Error, msg, ex.backtrace
end