Class: CFF::File

Inherits:
Object
  • Object
show all
Defined in:
lib/cff/file.rb

Overview

File provides direct access to a CFF Model, with the addition of some filesystem utilities.

Constant Summary collapse

YAML_HEADER =

:nodoc:

"---\n".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param) ⇒ File

:call-seq:

new(title) -> File
new(model) -> File

Create a new File. Either a pre-existing Model can be passed in or, as with Model itself, a title can be supplied to initalize a new File.

All methods provided by Model are also available directly on File objects.



33
34
35
36
37
# File 'lib/cff/file.rb', line 33

def initialize(param)
  param = Model.new(param) unless param.is_a?(Model)

  @model = param
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

:nodoc:



66
67
68
# File 'lib/cff/file.rb', line 66

def method_missing(name, *args) # :nodoc:
  @model.respond_to?(name) ? @model.send(name, *args) : super
end

Class Method Details

.read(file) ⇒ Object

:call-seq:

read(file) -> File

Read a file and parse it for subsequent manipulation.



43
44
45
# File 'lib/cff/file.rb', line 43

def self.read(file)
  new(YAML.load_file(file))
end

.write(file, cff) ⇒ Object

:call-seq:

write(file, model)
write(file, yaml)

Write the supplied model or yaml string to ‘file`.



52
53
54
55
56
# File 'lib/cff/file.rb', line 52

def self.write(file, cff)
  cff = cff.to_yaml unless cff.is_a?(String)

  ::File.write(file, cff[YAML_HEADER.length...-1])
end

Instance Method Details

#respond_to_missing?(name, *all) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


70
71
72
# File 'lib/cff/file.rb', line 70

def respond_to_missing?(name, *all) # :nodoc:
  @model.respond_to?(name, *all)
end

#write(file) ⇒ Object

:call-seq:

write(file)

Write this CFF File to ‘file`.



62
63
64
# File 'lib/cff/file.rb', line 62

def write(file)
  File.write(file, @model)
end