Class: KManager::Resources::FileResource

Inherits:
BaseResource show all
Includes:
KLog::Logging
Defined in:
lib/k_manager/resources/file_resource.rb

Overview

A file resource represents content that is loaded via a file.

File resources have the benefit that file watchers can watch them locally and reload when these resources on change.

Direct Known Subclasses

RubyFileResource, UnknownFileResource

Constant Summary collapse

KNOWN_EXTENSIONS =
{
  '.rb' => :ruby,
  '.csv' => :csv,
  '.json' => :json,
  '.yaml' => :yaml
}.freeze

Constants inherited from BaseResource

BaseResource::ACTIONS

Instance Attribute Summary

Attributes inherited from BaseResource

#area, #content, #content_type, #documents, #namespace, #status, #uri

Instance Method Summary collapse

Methods inherited from BaseResource

#activated?, #alive?, #attach_document, #content_loaded?, #default_content_type, #documents_loaded?, #documents_preloaded?, #documents_registered?, #fire_action, #fire_next_action, #host, #load_document, #new_document, #preload_document, #register_document, #scheme, valid_action?

Constructor Details

#initialize(uri, **opts) ⇒ FileResource

Returns a new instance of FileResource.



19
20
21
22
23
# File 'lib/k_manager/resources/file_resource.rb', line 19

def initialize(uri, **opts)
  warn('URI::File type expected for File Resource') unless uri.is_a?(URI::File)
  super(uri, **opts)
  log_any_messages unless valid?
end

Instance Method Details

#attribute_values(prefix = nil) ⇒ Object

Currently in base def register_document

KManager::Resources::ResourceDocumentFactory.create_documents(self)

end



42
43
44
45
46
47
48
# File 'lib/k_manager/resources/file_resource.rb', line 42

def attribute_values(prefix = nil)
  result = super(prefix)
  result["#{prefix}path".to_sym]          = resource_path
  result["#{prefix}relative_path".to_sym] = resource_relative_path
  result["#{prefix}exist".to_sym]         = resource_valid?
  result
end

#debugObject



50
51
52
53
54
55
56
57
# File 'lib/k_manager/resources/file_resource.rb', line 50

def debug
  super do
    log.kv 'infer_key'        , infer_key , 20
    log.kv 'file'             , source_path        , 20
    log.kv 'resource_path'    , resource_path      , 20
    log.kv 'resource_valid?'  , resource_valid?    , 20
  end
end

#default_schemeObject



33
34
35
# File 'lib/k_manager/resources/file_resource.rb', line 33

def default_scheme
  :file
end

#infer_keyObject

Infer key is the file name without the extension stored in dash-case



26
27
28
29
30
31
# File 'lib/k_manager/resources/file_resource.rb', line 26

def infer_key
  file_name = Pathname.new(source_path).basename.sub_ext('').to_s

  # Handlebars::Helpers::StringFormatting::Snake.new.parse(file_name)
  Cmdlet::Case::Snake.new.call(file_name)
end

#recreate(resource) ⇒ Object

TODO: Write tests



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/k_manager/resources/file_resource.rb', line 67

def recreate(resource)
  raise 'Recreate only works for resources of the same type' unless resource.is_a?(self.class)

  # area: resource.area,
  # content_type: resource.content_type,
  opts = {
    area: resource.area,
    namespace: resource.namespace
  }

  resource.class.new(resource.uri, **opts)
end

#resource_pathObject



80
81
82
# File 'lib/k_manager/resources/file_resource.rb', line 80

def resource_path
  @resource_path ||= absolute_path(source_path)
end

#resource_relative_path(from_path = Dir.pwd) ⇒ Object



84
85
86
# File 'lib/k_manager/resources/file_resource.rb', line 84

def resource_relative_path(from_path = Dir.pwd)
  Pathname.new(resource_path).relative_path_from(from_path).to_s
end

#resource_valid?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/k_manager/resources/file_resource.rb', line 88

def resource_valid?
  File.exist?(resource_path)
end

#source_pathObject

Source path - aka Full path to file

example: /Users/davidcruwys/dev/kgems/k_dsl/spec/factories/dsls/common-auth/admin_user.rb



62
63
64
# File 'lib/k_manager/resources/file_resource.rb', line 62

def source_path
  uri.path
end