<commit_before># NanoStore for RubyMotion

Wrapper for NanoStore, a lightweight schema-less key-value document database based on sqlite, in RubyMotion.

Status: Work in progress. API subject to change.

## Installation

Add BubbleWrap, NanoStore and NanoStoreInMotion as a git submodule of your RubyMotion project:

    git clone https://github.com/mattetti/BubbleWrap.git vendor/BubbleWrap
    git clone https://github.com/tciuro/NanoStore.git vendor/NanoStore
    git clone https://github.com/siuying/NanoStoreInMotion.git vendor/NanoStoreInMotion

Add the lib path and NanoStore pod to your project 'Rakefile'

```ruby
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'motion-cocoapods'
Motion::Project::App.setup do |app|
  app.name = 'myapp'
  app.files += Dir.glob(File.join(app.project_dir, 'vendor/BubbleWrap/lib/**/*.rb'))
  app.files += Dir.glob(File.join(app.project_dir, 'vendor/NanoStoreInMotion/lib/**/*.rb'))
  app.pods do
    dependency 'NanoStore'
  end
  
  # You may want to make sure nano store is loaded before your models
  app.files_dependencies("app/models/my_model_class.rb" => [
    "vendor/NanoStoreInMotion/lib/nano_store.rb",
    "vendor/NanoStoreInMotion/lib/nano_store/model.rb",
    "vendor/NanoStoreInMotion/lib/nano_store/store_extension.rb"
  ])
  
end
```

Now, you can use NanoStore in your app.

## Basic Usage

### Set default storage type

````ruby
NanoStore.shared_store = NanoStore.store(:memory) # memory only db
NanoStore.shared_store = NanoStore.store(:file, documents_path + "/nano.db") # persist the data
````

### Define Model

````ruby
class User < NanoStore::Model
  attribute :name
  attribute :age
  attribute :created_at
end
````

### Create

````ruby
# Initialize a new object and save it
user = User.new(:name => "Bob", :age => 16, :created_at => Time.now)
user.save

# Create a new object directly<commit_msg>add README on data type<commit_after># NanoStore for RubyMotion

Wrapper for NanoStore, a lightweight schema-less key-value document database based on sqlite, in RubyMotion.

Status: Work in progress. API subject to change.

## Installation

Add BubbleWrap, NanoStore and NanoStoreInMotion as a git submodule of your RubyMotion project:

    git clone https://github.com/mattetti/BubbleWrap.git vendor/BubbleWrap
    git clone https://github.com/tciuro/NanoStore.git vendor/NanoStore
    git clone https://github.com/siuying/NanoStoreInMotion.git vendor/NanoStoreInMotion

Add the lib path and NanoStore pod to your project 'Rakefile'

```ruby
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'motion-cocoapods'
Motion::Project::App.setup do |app|
  app.name = 'myapp'
  app.files += Dir.glob(File.join(app.project_dir, 'vendor/BubbleWrap/lib/**/*.rb'))
  app.files += Dir.glob(File.join(app.project_dir, 'vendor/NanoStoreInMotion/lib/**/*.rb'))
  app.pods do
    dependency 'NanoStore'
  end
  
  # You may want to make sure nano store is loaded before your models
  app.files_dependencies("app/models/my_model_class.rb" => [
    "vendor/NanoStoreInMotion/lib/nano_store.rb",
    "vendor/NanoStoreInMotion/lib/nano_store/model.rb",
    "vendor/NanoStoreInMotion/lib/nano_store/store_extension.rb"
  ])
  
end
```

Now, you can use NanoStore in your app.

## Basic Usage

### Set default storage type

````ruby
NanoStore.shared_store = NanoStore.store(:memory) # memory only db
NanoStore.shared_store = NanoStore.store(:file, documents_path + "/nano.db") # persist the data
````

### Define Model

````ruby
class User < NanoStore::Model
  attribute :name
  attribute :age
  attribute :created_at
end
````

A key (UUID) that identifies the object will be added automatically.

Attributes must be serializable, which means that only the following data types are allowed: 

- NSArray
- NSDictionary
- NSString
- NSData (*)
- NSDate
- NSNumber

#### Note

(*) The data type NSData is allowed, but it will be excluded from the indexing process.

### Create

````ruby
# Initialize a new object and save it
user = User.new(:name => "Bob", :age => 16, :created_at => Time.now)
user.save
user.key # => "<KEY>" (automatically generated UUID)

# Create a new object directly
{'fraction_non_alphanumeric': 0.12056567593480345, 'fraction_numerical': 0.0009587727708533077, 'mean_word_length': 4.412451361867705, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 13, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1283180', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1325, 'n_words': 385}