Class: Hitsuji

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

Overview

The Hitsuji class is the interface to this module, and it contains all the functions you need. Examples using this class can be found in the descriptions of the class and instance methods below.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHitsuji

Creates a new system where all operations are performed.

Example

my_system = Hitsuji.new


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

def initialize
  @struct = []
  @metadata = {
    date_created: `date`,
    date_edited: `date`
  }
end

Class Method Details

.item(name, value) ⇒ Object

Creates a new item, the equivalent of a variable in the system.

Attributes

  • name - the name of the new item, which is written as a symbol.

  • value - the contents of the new item, whether that be a string, variable or any other object.

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 'bar')            # a new item


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

def self.item(name, value)
  Item.new(name, value)
end

.linker(name, objs) ⇒ Object

Creates a new linker, which is simply put a grouping of items and other linkers.

Attributes

  • name - the name of the new linker, which is written as a symbol.

  • value - the contents of the new linker, whether that be an item or another linker.

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 'bar')            # a new item
my_item2 = Hitsuji.item(:qux, 'quux')          # a second item
items = [:foo, :qux]
my_linker = Hitsuji.linker(:baz, items)        # a new linker


54
55
56
# File 'lib/hitsuji.rb', line 54

def self.linker(name, objs)
  Linker.new(name, objs)
end

.operation(name, input, block) ⇒ Object

Creates a new operation, which is an equation performed on multiple items contained within a linker. This linker can contain more linkers from which more items will be progressively taken. An operation can then be used as part of a linker, and the result can be used in another operation.

Attributes

  • name - the name of the new linker, which is written as a symbol.

  • input - the contents of the new linker, whether that be an item or another linker.

  • block - a block as a string to perform the operation

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 1)                # a new item
my_item2 = Hitsuji.item(:qux, 2)               # a second item
items = [:foo, :qux]
my_linker = Hitsuji.linker(:baz, items)        # a new linker
my_op = Hitsuji.operation(:op, my_linker, %{
  |arg1, arg2| arg1 + arg2
}) # => :foo + :qux => 1 + 2 => 3              # a new operation


80
81
82
# File 'lib/hitsuji.rb', line 80

def self.operation(name, input, block)
  Operation.new(name, input, block)
end

Instance Method Details

#bind(*obj) ⇒ Object

‘Binds’ the inputted items to the system, allowing for the continous updating of the values within a system. This continuous updating forms the main principle of Hitsuji. Once bound, an object can only be edited using the Hitsuji.find, Hitsuji.edit and Hitsuji.remove methods. It can never share a name with any other bound object. Once bound, the name of an object becomes uneditable, but the value still keeps its read-write capabilites.

Attributes

  • obj - the items, linkers and operation you want to bind

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 1)                # a new item
my_item2 = Hitsuji.item(:qux, 2)               # a second item
items = [:foo, :qux]
my_linker = Hitsuji.linker(:baz, items)        # a new linker
my_system.bind(my_item, my_item2, my_linker)   # binds items + linker


103
104
105
106
107
# File 'lib/hitsuji.rb', line 103

def bind(*obj)
  @struct.concat obj
  Control.update @struct
  @metadata[:date_edited] = `date`
end

#edit(query, value) ⇒ Object

Finds a bound object in the system by name, edits the object if it exists, and then returns the original object.

Attributes

  • query - the name of the object to edit

  • value - the new value to assign to this object

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.hitsuji')            # imports 'oldfile.txt'
my_item = my_system.edit(:foo, 'bar')          # changes an item


175
176
177
# File 'lib/hitsuji.rb', line 175

def edit(query, value)
  Control.get(query, @struct, value, false)
end

#export(directory) ⇒ Object

Exports the current state of the system to a file. This process *does not* export unbound items, linkers or operations! Creating new items doesn’t automatically bind them to the system, so therefore the exported file only contains objects bound with Hitsuji.bind. The Hitsuji file must end with “.hitsuji”.

Attributes

  • directory - the path of the file to export the system (the extension of the file doesn’t matter)

Example

my_system = Hitsuji.new                        # a new system
my_item = Hitsuji.item(:foo, 1)                # a new item
my_system.bind(my_item)                        # binds item
my_system.export('newfile.hitsuji')            # exports to 'newfile.txt'


126
127
128
# File 'lib/hitsuji.rb', line 126

def export(directory)
  Transfer.export(directory, @struct, @metadata)
end

#find(query) ⇒ Object

Finds a bound object in the system by name, and returns the object if it exists.

Attributes

  • query - the name of the object to search for

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.hitsuji')            # imports 'oldfile.txt'
my_item = my_system.find(:foo)                 # finds an item


158
159
160
# File 'lib/hitsuji.rb', line 158

def find(query)
  Control.get(query, @struct, nil, false)
end

#import(directory) ⇒ Object

Imports a file into a system, *overwriting anything already bound to the system*. The Hitsuji file must end with “.hitsuji”.

Attributes

  • directory - the path of the file you want to import from

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.hitsuji')            # imports 'oldfile.txt'


141
142
143
144
# File 'lib/hitsuji.rb', line 141

def import(directory)
  @struct, @metadata = Transfer.import(directory)
  Control.update @struct
end

#remove(query) ⇒ Object

Finds a bound object in the system by name, removes it if it exists, and then returns the original object.

Attributes

  • query - the name of the object to remove

Example

my_system = Hitsuji.new                        # a new system
my_system.import('oldfile.hitsuji')            # imports 'oldfile.txt'
my_item = my_system.remove(:foo)               # removes an item


191
192
193
# File 'lib/hitsuji.rb', line 191

def remove(query)
  Control.get(query, @struct, nil, true)
end